Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT 5/2014, as this seems to be a popular question I've added much detail to the answer:</p> <p><strong>Android:</strong></p> <p>For Android, refer to <a href="https://stackoverflow.com/questions/5596261/intent-filter-to-launch-my-activity-when-custom-uri-is-clicked">Intent Filter to Launch My Activity when custom URI is clicked</a>.</p> <p>You use an intent-filter:</p> <pre><code>&lt;intent-filter&gt; &lt;action android:name="android.intent.action.VIEW" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;category android:name="android.intent.category.BROWSABLE" /&gt; &lt;data android:scheme="myapp" /&gt; &lt;/intent-filter&gt; </code></pre> <p>this is attached to the Activity that you want launched. For example:</p> <pre><code>&lt;activity android:name="com.MyCompany.MyApp.MainActivity" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.VIEW" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;category android:name="android.intent.category.BROWSABLE" /&gt; &lt;data android:scheme="myapp" android:host="com.MyCompany.MyApp" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; </code></pre> <p>Then, in your activity, if not running, the activity will be launched with the URI passed in the Intent. </p> <pre><code>Intent intent = getIntent(); Uri openUri = intent.getData(); </code></pre> <p>If already running, onNewIntent() will be called in your activity, again with the URI in the intent.</p> <p>Lastly, if you instead want to handle the custom protocol in UIWebView's hosted within your native app, you can use:</p> <pre><code>myWebView.setWebViewClient(new WebViewClient() { public Boolean shouldOverrideUrlLoading(WebView view, String url) { // inspect the url for your protocol } }); </code></pre> <p><strong>iOS:</strong></p> <p>For iOS, refer to <a href="https://stackoverflow.com/questions/3612460/lauching-app-with-url-via-uiapplicationdelegates-handleopenurl-working-under">Lauching App with URL (via UIApplicationDelegate&#39;s handleOpenURL) working under iOS 4, but not under iOS 3.2</a>.</p> <p>Define your URL scheme via Info.plist keys similar to:</p> <pre><code>&lt;key&gt;CFBundleURLTypes&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;CFBundleURLName&lt;/key&gt; &lt;string&gt;com.yourcompany.myapp&lt;/string&gt; &lt;/dict&gt; &lt;dict&gt; &lt;key&gt;CFBundleURLSchemes&lt;/key&gt; &lt;array&gt; &lt;string&gt;myapp&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/array&gt; </code></pre> <p>Then define a handler function to get called in your app delegate</p> <pre><code>- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // parse and validate the URL } </code></pre> <p>If you want to handle the custom protocol in <strong>UIWebViews</strong> hosted within your native app, you can use the UIWebViewDelegate method:</p> <pre><code>- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *urlPath = [request URL]; if (navigationType == UIWebViewNavigationTypeLinkClicked) { // inspect the [URL scheme], validate if ([[urlPath scheme] hasPrefix:@"myapp"]) { ... } } } </code></pre> <p>}</p> <p>For <strong>WKWebView</strong> (iOS8+), you can instead use a WKNavigationDelegate and this method:</p> <pre><code>- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSURL *urlPath = navigationAction.request.URL; if (navigationAction.navigationType == WKNavigationTypeLinkActivated) { // inspect the [URL scheme], validate if ([[urlPath scheme] hasPrefix:@"myapp"]) { // ... handle the request decisionHandler(WKNavigationActionPolicyCancel); return; } } //Pass back to the decision handler decisionHandler(WKNavigationActionPolicyAllow); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload