iOS开发-应用程序启动完成launchOptions详解
// 应用程序启动完成实现代理方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
应用程序启动完成时会执行上面的方法,且只会执行一次。参数launchOptions(NSDictionary *)中携带的是启动时的信息。字典中包含的键可直接进入头文件进行查找,下面总结几种常见的启动方式。
launchKeys.png1.应用程序正常启动,也就是用户点击应用程序图标进行启动时,launchOptions为NULL
2.若通过点击屏幕上方的本地通知横幅启动程序,则字典中保存的key为UIApplicationLaunchOptionsLocalNotificationKey,值为本地通知(UILocalNotification)对象
3.若通过点击屏幕上方的本地通知横幅启动程序,则字典中保存的key为UIApplicationLaunchOptionsRemoteNotificationKey,值为远程通知(NSDictionary)信息
注意:iOS 10对通知进行了重大的更新。
4.应用间跳转启动应用程序。假设APP2跳转到APP1.
a.当APP1程序正在后台运行时,APP2跳转到APP1时,会调用下面的代理方法,获取到想要得到的参数。
// ios9之前
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return YES;
}
b.当APP1程序处于关闭状态,进程被杀死的情况下,这时通过URL启动APP会执行应用程序的代理方法,并把启动信息放在launchOptions中。此时,launchOptions字典中有两组键值对
键UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL);
键UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString);
注意:在这种APP1关闭的情况下调式是看不到效果的。下面是我测试的代码。
//
// AppDelegate.h
// APP1
//
// Created by zengchunjun on 16/11/2.
// Copyright © 2016年 zengchunjun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong)NSDictionary *launch;
@end
//
// AppDelegate.m
// APP1
//
// Created by zengchunjun on 16/11/2.
// Copyright © 2016年 zengchunjun. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"===%@",launchOptions);
self.launch = launchOptions;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
// 直接在界面上展示launchOptions中的信息
//
// ViewController.m
// APP1
//
// Created by zengchunjun on 16/11/2.
// Copyright © 2016年 zengchunjun. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSString *str = @"";
for (NSString *value in delegate.launch.allKeys) {
str = [str stringByAppendingString:[NSString stringWithFormat:@"%@-%@",value,delegate.launch[value]]];
}
self.textview.text = str;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// 注意:在跳转到APP1时,command + shift + 双击H键关闭APP1. command+S键模拟器屏幕截图
Simulator Screen Shot 2016年11月3日 10.11.17.png