How to get a universal link to open the app instead of safari? How to get a universal link to open the app instead of safari? xcode xcode

How to get a universal link to open the app instead of safari?


After spending some time trying to configure Universal Links here is what I have learned so far:

1 - Universal Links DO NOT work with redirects or when you type your link in Safari address bar and hit enter.

2 - Type your address in Notes and click on it, if your app opens directly it means you have everything set-up correctly.

3 - Universal Links work when user performs an action on your website, to try this add a button to your website/blog and link it to the Universal Link implemented to open your app. Now open your website/blog in Safari, and click the button, it will directly open your app.

4 - When you click on 'Open' in Safari, subsequent calls won't open the app directly, for reasons mentioned in point 1.

Hope this helps :)


You have probably tapped a small button on the device status bar while using the app, that led you back to the web site. Once you do that, the system would remember that and would always launch Safari when a universal link to your app is tapped. To undo that, long-tap the link, and in the menu that pops up, choose "Open with [your app]"


In AppDelegate.m call this:

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options;{    NSLog(@"url recieved: %@", url);    NSLog(@"query string: %@", [url query]);    NSLog(@"host: %@", [url host]);    NSLog(@"url path: %@", [url path]);    NSDictionary *dict = [self parseQueryString:[url query]];    NSLog(@"query dict: %@", dict);    if ([[url host] isEqualToString:@"login"]) {        [self setupHomeScreen:NO type:@"" entityId:@""];        [self _endSession];        return YES;    } else if ([[url host] isEqualToString:@"smartoffice"]) {        NSMutableDictionary *result = [[NSMutableDictionary alloc] init];        NSRange needle = [url.absoluteString rangeOfString:@"?" options:NSCaseInsensitiveSearch];        NSString *data = nil;        if(needle.location != NSNotFound) {            NSUInteger start = needle.location + 1;            NSUInteger end = [url.absoluteString length] - start;            data = [url.absoluteString substringWithRange:NSMakeRange(start, end)];        }        for (NSString *param in [data componentsSeparatedByString:@"&"]) {            NSArray *keyvalue = [param componentsSeparatedByString:@"="];            if([keyvalue count] == 2){                [result setObject:[keyvalue objectAtIndex:1] forKey:[keyvalue objectAtIndex:0]];            }        }        NSString *entityID = ([result objectForKey:@"secondparameter"]);        NSString *type = [result objectForKey:@"parameter"];        [self setupHomeScreen:YES type:type entityId:entityID];        //[self setupHomeScreen:YES type:@"TASK" entityId:@"160315"];        return result;    } else {        NSLog(@"myApp is not installed");        [self setupHomeScreen:NO type:@"0" entityId:@"0"];    }    return NO;}

URL Type in Xcode Info

Open Xcode, got to Project Settings -> Info, and add inside ‘The URL Types” section a new URL scheme.

That is the scheme ://resource. So we go ahead and type com.myApp://

Fo me its backofficeapp://

Now use this link on Safari

backofficeapp:// // This one only redirects you to a landing page.

OR

backofficeapp://smartoffice/1?parameter=TASK&secondparameter=157536

Where

parameter = “TASK”

secondparameter = “taskID” // #iOS Mobile 2020 = 157536

This parameter is for landing on a specific screen.

Remember this URL won’t work if the app is not installed.