Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle http url with my app (if installed)

I would like to handle a url like: http://www.something.com/product/3/ in this way:

  • If you have the App installed => handle it with the App.
  • Else => handle it with safari.

Is it this possible?

The idea is I can send that url via email and if the receiver has the app installed, then the app raises up and do something, and if not installed just open it via safari.

I know about the custom schemes, which work fine in the app but they obviously don't work in safari because they are not http protocol.

like image 495
jobima Avatar asked Jul 19 '13 07:07

jobima


2 Answers

1) Create a custom URL scheme in your app. Follow the link if you don't know how to create a custom URL scheme: http://www.idev101.com/code/Objective-C/custom_url_schemes.html

2) Then add following script to the URLs that you want your app to open.

<script language="javascript" type="text/javascript">
    var iOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false);
    var appUrlScheme = "myappscheme://" + document.URL;
    if (iOS) {
        window.open(appUrlScheme, "_self");
    } 
</script>

The script is self explanatory, if its an iOS device then it simply tries to open current url with your custom url scheme i.e. myappscheme://whateverurl.com'. If you app is installed on the device then iOS is going to launch your app and pass this URL tohandleOpenURLfunction, otherwise mobile safari will silently ignorewindow.open` call and your webpage will load as normal:

3) Implement handleOpenURL callback method in your AppDelegate to handle the URL:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSLog(@"url: %@", url);
    NSLog(@"query string: %@", [url query]);
    NSLog(@"host: %@", [url host]);
    NSLog(@"url path: %@", [url path]);

   //REDIRECT USER TO A VIEW CONTROLLER

    return YES;
}
like image 119
Yas Tabasam Avatar answered Oct 14 '22 21:10

Yas Tabasam


Yes you can do by using URL Scheme, First create your own custom url scheme in Info.plist file

Add a new row by going to the menu and clicking Editor > Add Item. Set up a URL Types item by adding a new item. Expand the URL Types key, expand Item 0, and add a new item, “URL schemes”. Fill in “readtext” for Item 0 of “URL schemes” and your company identifier for the “URL Identifier”.

Then parse your url for different url for same url scheme to open your app with different screens.. Here i have shown only alert, use to open your specific page as per your link.. And if the app is not installed then it will open in web browser.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    // Display text
    UIAlertView *alertView;
    NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    alertView = [[UIAlertView alloc] initWithTitle:@"Text" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    return YES;
}

application:handleOpenURL: (used for earlier iOS 4.1) application:openURL:sourceApplication:annotation: (used for later iOS 4.1).

like image 29
Krishna Kumar Avatar answered Oct 14 '22 21:10

Krishna Kumar