【iOS開發】判斷app啟動的方式(launchOptions)
阿新 • • 發佈:2019-01-01
iOS app啟動的方式有哪些:
- 自己啟動(使用者手動點選啟動)
- urlscheme啟動(關於urlScheme的詳解點選開啟連結)
- 本地通知啟動 (自己寫的本地通知啟動,藍芽模組的啟動,地理圍欄的啟動)
- 遠端通知啟動 (後臺伺服器的推送通知)
在appdelegate.m檔案中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
系統給的一些關於launchoption的字典裡面的內容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 UIKIT_EXTERN NSString *const UIApplicationOpenSettingsURLString NS_AVAILABLE_IOS(8_0); // Keys for application:openURL:options: UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsSourceApplicationKey NS_AVAILABLE_IOS(9_0); // value is an NSString containing the bundle ID of the originating application UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsAnnotationKey NS_AVAILABLE_IOS(9_0); // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsOpenInPlaceKey NS_AVAILABLE_IOS(9_0); // value is a bool NSNumber, set to YES if the file needs to be copied before use
如何判斷呢?程式碼如下:
- (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是否存在來判斷是通過哪種方式啟動的。