ios UIApplocation 中APP启动方式

时间:2023-12-11 23:57:08

iOS app启动的方式有哪些:

  1. 自己启动(用户手动点击启动)
  2. urlscheme启动(关于urlScheme的详解)http://www.cnblogs.com/sunfuyou/p/6183064.html
  3. 本地通知启动  (自己写的本地通知启动,蓝牙模块的启动,地理围栏的启动)
  4. 远程通知启动    (后台服务器的推送通知)

//APPdelegate.m中写上这个方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//系统中自带的launchoption的字典里面的所有key

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsURLKey                   NS_AVAILABLE_IOS(3_0); // userInfo contains NSURL with launch URL

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsSourceApplicationKey     NS_AVAILABLE_IOS(3_0); // userInfo contains NSString with launch app bundle ID

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsRemoteNotificationKey    NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // userInfo contains NSDictionary with payload

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsLocalNotificationKey     NS_AVAILABLE_IOS(4_0) __TVOS_PROHIBITED; // userInfo contains a UILocalNotification

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsAnnotationKey            NS_AVAILABLE_IOS(3_2); // userInfo contains object with annotation property list

UIKIT_EXTERN NSString *const UIApplicationProtectedDataWillBecomeUnavailable    NS_AVAILABLE_IOS(4_0);

UIKIT_EXTERN NSString *const UIApplicationProtectedDataDidBecomeAvailable       NS_AVAILABLE_IOS(4_0);

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsLocationKey              NS_AVAILABLE_IOS(4_0); // app was launched in response to a CoreLocation event.

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsNewsstandDownloadsKey    NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // userInfo contains an NSArray of NKAssetDownload identifiers

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsBluetoothCentralsKey     NS_AVAILABLE_IOS(7_0); // userInfo contains an NSArray of CBCentralManager restore identifiers

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsBluetoothPeripheralsKey  NS_AVAILABLE_IOS(7_0); // userInfo contains an NSArray of CBPeripheralManager restore identifiers

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsShortcutItemKey          NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED; // userInfo contains the UIApplicationShortcutItem used to launch the app.

// Key in options dict passed to application[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsUserActivityDictionaryKey    NS_AVAILABLE_IOS(8_0); // Sub-Dictionary present in launch options when user activity is present

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsUserActivityTypeKey          NS_AVAILABLE_IOS(8_0); // Key in user activity dictionary for the activity type

判断启动代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  // Override point for customization after application launch.

  //用户自己点击启动

  if(!launchOptions)

  {

   NSLog(@"用户点击app启动");

}  else      {

    NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];

    //app 通过urlscheme启动

    if (url) {

  NSLog(@"app 通过urlscheme启动 url = %@",url);

  }

    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    //通过本地通知启动

    if(localNotification)

     {

    NSLog(@"app 通过本地通知启动 localNotification = %@",localNotification);

  }

    NSDictionary *remoteCotificationDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    //远程通知启动

    if(remoteCotificationDic)

    {

    NSLog(@"app 通过远程推送通知启动 remoteCotificationDic = %@",remoteCotificationDic);

  }

   }

  return YES;

}

//位置变动的通知

if(launchOptions[@"UIApplicationLaunchOptionsLocationKey"])

{

      //写你的重新定位的代码

}

总结:当launchOptions为空时是用户自己点击app启动,不为空时,要根据相应的key是否存在来判断是通过哪种方式启动的。