iOS开发-应用程序启动完成launchOptions详解

// 应用程序启动完成实现代理方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  
return YES;
}

应用程序启动完成时会执行上面的方法,且只会执行一次。参数launchOptions(NSDictionary *)中携带的是启动时的信息。字典中包含的键可直接进入头文件进行查找,下面总结几种常见的启动方式。

launchKeys.png

1.应用程序正常启动,也就是用户点击应用程序图标进行启动时,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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是任务栈(Task) 官方文档是这么解释的 任务是指在执行特定作业时与用户交互的一系列 Activity。 这...
    phoenixsky阅读 25,242评论 3 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,812评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,286评论 19 139
  • Black &Veatch(博莱克·威奇)这是我之前工作的公司,也是我进入社会后工作时间最长的一个公司,这里有我热...
    Catherineliao阅读 1,374评论 2 1
  • 今天难得出太阳,温度也适宜,很适合出去游玩,加上刚好休息,于是就叫上朋友出去玩了,朋友说最近油菜花开了,我们可以去...
    宅小阳阅读 238评论 0 0