Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if handleOpenURL is called app startup or while app is running?

Tags:

ios

As the sequence of events is slightly different depending on which of these two scenarios is in progress, I would like to be able to tell the difference. Any suggestions?

like image 423
Anders Sewerin Johansen Avatar asked Aug 20 '13 12:08

Anders Sewerin Johansen


3 Answers

You should not use handleOpenURL since it's deprecated. Instead, use application:openURL:sourceApplication:annotation: (available since iOS 4.2).

Apple's documentation gives the answer to your question here regarding application:openURL:sourceApplication:annotation:

If your app had to be launched to open the URL, the app calls the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods first, followed by this method. The return values of those methods can be used to prevent this method from being called. (If the application is already running, only this method is called.)

like image 200
Marcus Adams Avatar answered Oct 16 '22 04:10

Marcus Adams


Did you tried this method in AppDelegate?

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"Launched with URL: %@", url.absoluteString);

    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_APP_OPENED_FROM_LINK object:[userDict objectAtIndex:0]];

    return YES;
}
like image 31
Adarsh V C Avatar answered Oct 16 '22 06:10

Adarsh V C


In my case i had to create the navigation flow again if the app is launched when the app is running at background, but if the app is launched for the first time there was no need for that. My implementation was:

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.appIsLaunchedFromZeroToOpenURL = false;
    //some other code 
    NSURL *applicationOpenURL = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
    if (applicationOpenURL) {
        self.appIsLaunchedFromZeroToOpenURL = true;
    }
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (self.appIsLaunchedFromZeroToOpenURL == false) {
        //app should reload the navigation then navigate to url if the app is not launched for the first time
       [self createNavigationFromZero];
    }
    [self openURL:url];//navigate to the url
    self.appIsLaunchedFromZeroToOpenURL = false;
}
like image 1
Mihriban Minaz Avatar answered Oct 16 '22 04:10

Mihriban Minaz