- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. }
每一個iOS app的入口都是app
application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
當app啓動完成,準備運行時,application會調用本身的這個代理方法。ide
一個app除了點擊圖標進行啓動,還有一些其餘方式調用一個app。區別不一樣啓動方式,就須要用到launchOptions. 與userInfo 字典類似,-application:didFinishLaunchingWithOptions: 能夠經過launchOptions獲取啓動信息的key。ui
經過URL啓動this
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app://..."]];
http://URL 會調用Safari, mailto://URL 會打開郵箱, tel://URL 會撥打電話。atom
這種狀況下launchOptions 爲UIApplicationLaunchOptionsURLKeyspa
app經過URL調用時,還能夠有一些系統信息。 當經過UIDocumentInteractionController 或者 AirDrop 調用時,launchedOptions 會被設置爲:代理
UIApplicationLaunchOptionsSourceApplicationKey: key值是一個NSString,表示要調用你app的app bundle IDrest
UIApplicationLaunchOptionsAnnotationKey: key能夠存儲property-list 對象。code
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Document" withExtension:@"pdf"]; if (fileURL) { UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; documentInteractionController.annotation = @{@"foo": @"bar"}; [documentInteractionController setDelegate:self]; [documentInteractionController presentPreviewAnimated:YES]; }
推送消息調用orm
遠程推送
遠程推送消息調用時,launch option 會是 UIApplicationLaunchOptionsRemoteNotificationKey,
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // ... if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) { [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]]; } }
本地推送
本地推送launch options 會是 UIApplicationLaunchOptionsLocalNotificationKey
@import AVFoundation; @interface AppDelegate () @property (readwrite, nonatomic, assign) SystemSoundID localNotificationSound; @end
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if (application.applicationState == UIApplicationStateActive) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil]; if (!self.localNotificationSound) { NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"Sosumi" withExtension:@"wav"]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_localNotificationSound); } AudioServicesPlaySystemSound(self.localNotificationSound); [alertView show]; } } - (void)applicationWillTerminate:(UIApplication *)application { if (self.localNotificationSound) { AudioServicesDisposeSystemSoundID(self.localNotificationSound); } }
位置事件調用
當由手機位置變更,調用app時,launch option 會是 UIApplicationLaunchOptionsLocationKey, key 是一個NSNumber 包含一個Boolean。
@import CoreLocation; @interface AppDelegate () <CLLocationManagerDelegate> @property (readwrite, nonatomic, strong) CLLocationManager *locationManager; @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // ... if (![CLLocationManager locationServicesEnabled]) { [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Location Services Disabled", nil) message:NSLocalizedString(@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil] show]; } else { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; [self.locationManager startMonitoringSignificantLocationChanges]; } if (launchOptions[UIApplicationLaunchOptionsLocationKey]) { [self.locationManager startUpdatingLocation]; } }
Newsstand
launch option 會是 UIApplicationLaunchOptionsNewsstandDownloadsKey, 提示用戶有新的Newsstand能夠下載
藍牙調用
iOS 7 中藍牙分主從關係,因此對應的launch option key有兩種
UIApplicationLaunchOptionsBluetoothCentralsKey
UIApplicationLaunchOptionsBluetoothPeripheralsKey
@import CoreBluetooth; @interface AppDelegate () <CBCentralManagerDelegate> @property (readwrite, nonatomic, strong) CBCentralManager *centralManager; @end
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:(launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey] ?: [[NSUUID UUID] UUIDString])}]; if (self.centralManager.state == CBCentralManagerStatePoweredOn) { static NSString * const UID = @"7C13BAA0-A5D4-4624-9397-15BF67161B1C"; // generated with `$ uuidgen` NSArray *services = @[[CBUUID UUIDWithString:UID]]; NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}; [self.centralManager scanForPeripheralsWithServices:services options:scanOptions]; }