Anders Sewerin Johansen
Anders Sewerin Johansen

Reputation: 1656

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

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?

Upvotes: 5

Views: 6337

Answers (3)

Mihriban Minaz
Mihriban Minaz

Reputation: 3039

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;
}

Upvotes: 1

Marcus Adams
Marcus Adams

Reputation: 53830

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.)

Upvotes: 10

Adarsh V C
Adarsh V C

Reputation: 2314

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;
}

Upvotes: 2

Related Questions