Handle the http url with my application (if installed)

I would like to handle the url, for example: http://www.something.com/product/3/ as follows:

  • If the application is installed => processes it using the application.
  • Else => handle safari.

Is it possible?

The idea is that I can send this URL by e-mail, and if the recipient has the application installed, the application rises and does something, and if not installed, just open it via safari.

I know about user schemes that work great in an application, but they obviously don't work in safari, because they are not an http protocol.

+6
source share
3 answers

Yes, you can do it using the URL Schema . First, create your own URL scheme in the Info.plist file

Add a new line by going to the menu and choosing Editor> Add Item. Customize the Types URL element by adding a new element. Expand the URL type key, expand element 0, and add a new URL Schema element. Fill in the "readtext" for item 0 of "URL Schema" and your company identifier for "URL Identifier".

Then parse your URL for a different URL for the same URL pattern to open the application with different screens. Here I showed only a warning, I use it to open your specific page using your link .. And if the application is not installed, it will open in a 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).

+2
source

1) Create your own URL scheme in your application. Follow the link if you do not know how to create a custom URL scheme: http://www.idev101.com/code/Objective-C/custom_url_schemes.html

2) Then add the following script to the URLs you want 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-evident if it is an iOS device, then it just tries to open the 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 to myappscheme://whateverurl.com'. If you app is installed on the device then iOS is going to launch your app and pass this URL to handleOpenURL function, otherwise mobile safari will silently ignore window.open`, and your web page will load as usual:

3) Add the handleOpenURL callback method to 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; } 
+2
source

No, It is Immpossible. However, you can include both links in the mail:

0
source

Source: https://habr.com/ru/post/949800/


All Articles