iOS App Launch Option

来源:互联网 发布:淘宝找不到自己的店铺 编辑:程序博客网 时间:2024/05/22 06:39

iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有此程序启动的原因。

launchOptions中的可能键值见UIApplication Class Reference的Launch Options Keys节 。

  • 若用户直接启动,lauchOptions内无数据;
  • 若由其他应用程序通过openURL:启动,则UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL);
  • 若由其他应用启动,则UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString);
  • 若由本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification);
  • 若由远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo(NSDictionary);
  • 其他key还有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,
    UIApplicationLaunchOptionsNewsstandDownloadsKey。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    if (!launchOptions) {        NSLog(@"用户直接启动");        [self alertMessage:@"用户直接启动"];    }else{        NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];        if(url){            NSLog(@"由其他应用程序通过openURL:%@启动",[url absoluteString]);            [self alertMessage:@"由其他应用程序通过openURL"];        }        NSString *bundleId = [launchOptions objectForKey:UIApplicationLaunchOptionsSourceApplicationKey];        if(bundleId){            NSLog(@"由其他应用启动,那个应用的bundle ID = %@",bundleId);            [self alertMessage:@"由其他应用启动"];        }        UILocalNotification * localNotify = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];        if(localNotify){            NSLog(@"由本地通知启动,通知内容为%@",localNotify.alertBody);            [self alertMessage:@"由本地通知启动"];        }        NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];        if(userInfo){            NSLog(@"由远程通知启动,远程通知的userinfor不为空");            [self alertMessage:@"由远程通知启动"];        }    }}
0 0
原创粉丝点击