如何判断handleOpenURL是app app startup还是app正在运行?

时间:2013-08-20 12:17:01

标签: ios

由于事件的顺序略有不同,这取决于这两种情况中的哪一种正在进行中,我希望能够区分它们。有什么建议吗?

3 个答案:

答案 0 :(得分:10)

您不应该使用handleOpenURL,因为它已被弃用。相反,请使用application:openURL:sourceApplication:annotation:(自iOS 4.2起可用)。

Apple's documentation为您提供有关application:openURL:sourceApplication:annotation的问题的答案:

  

如果您的应用必须启动才能打开网址,应用就会调用   应用程序:willFinishLaunchingWithOptions:和   application:didFinishLaunchingWithOptions:方法首先,然后是   这种方法。这些方法的返回值可用于防止   这种方法被称为。 (如果应用程序已在运行,   只调用此方法。)

答案 1 :(得分:2)

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

答案 2 :(得分:1)

在我的情况下,如果在应用程序在后台运行时启动应用程序,我必须再次创建导航流程,但如果第一次启动应用程序则不需要。我的实施是:

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