Is there a way to define some kind of handling mechanism in Android and iOS that would allow me to do intercept either of the following:
myapp:///events/3/ - or - http://myapp.com/events/3/
I'd like to 'listen' for either the protocol or the host, and open a corresponding Activity / ViewController.
I'd like too if these could be as system wide as possible. I imagine this will be more of an issue on iOS, but I'd ideally be able to click either of those two schemes, as hyperlinks, from any app. Gmail, Safari, etc.
EDIT 5/2014, as this seems to be a popular question I've added much detail to the answer:
Android:
For Android, refer to Intent Filter to Launch My Activity when custom URI is clicked.
You use an intent-filter:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" /> </intent-filter>
this is attached to the Activity that you want launched. For example:
<activity android:name="com.MyCompany.MyApp.MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" android:host="com.MyCompany.MyApp" /> </intent-filter> </activity>
Then, in your activity, if not running, the activity will be launched with the URI passed in the Intent.
Intent intent = getIntent(); Uri openUri = intent.getData();
If already running, onNewIntent() will be called in your activity, again with the URI in the intent.
Lastly, if you instead want to handle the custom protocol in UIWebView's hosted within your native app, you can use:
myWebView.setWebViewClient(new WebViewClient() { public Boolean shouldOverrideUrlLoading(WebView view, String url) { // inspect the url for your protocol } });
iOS:
For iOS, refer to Lauching App with URL (via UIApplicationDelegate's handleOpenURL) working under iOS 4, but not under iOS 3.2.
Define your URL scheme via Info.plist keys similar to:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>com.yourcompany.myapp</string> </dict> <dict> <key>CFBundleURLSchemes</key> <array> <string>myapp</string> </array> </dict> </array>
Then define a handler function to get called in your app delegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // parse and validate the URL }
If you want to handle the custom protocol in UIWebViews hosted within your native app, you can use the UIWebViewDelegate method:
- (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"]) { ... } } }
}
For WKWebView (iOS8+), you can instead use a WKNavigationDelegate and this method:
- (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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With