【iOS】app启动入口
2016-07-19 本文已影响966人
24c41111e47b
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
说明:当应用程序启动时执行,应用程序启动入口。只在应用程序启动时执行一次。application参数用来获取应用程序的状态、变量等,值得注意的是字典参数:(NSDictionary *)launchOptions,该参数存储程序启动的原因。
启动原因
-
用户直接启动
lauchOptions内无数据 -
其他应用程序通过openURL:启动
lauchOptions字典内存储:
@{UIApplicationLaunchOptionsURLKey:启动URL(NSURL),
UIApplicationLaunchOptionsSourceApplicationKey:启动源程序的bundleID(NSString)} -
本地通知启动
lauchOptions字典内存储:
@{UIApplicationLaunchOptionsLocalNotificationKey:启动应用程序的本地通知对象(UILocalNotification)} - 远程通知启动
@{UIApplicationLaunchOptionsRemoteNotificationKey:启动应用程序的的远程通知信息userInfo(NSDictionary)} - 其他的key还有
UIApplicationLaunchOptionsAnnotationKey,
UIApplicationLaunchOptionsLocationKey,
UIApplicationLaunchOptionsNewsstandDownloadsKey
应用方法
支付宝外部应用跳转处理
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
/
如:跳转支付宝app支付时,app被kill,支付完成后,从支付宝app跳转回app(即重新启动),
return NO 不调用application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options,
直接返回首页
*/
if (launchOptions) {
NSString *appIDF = [launchOptions objectForKey:UIApplicationLaunchOptionsSourceApplicationKey];
if (appIDF && [appIDF isEqualToString:@"com.alipay.iphoneclient"]) {
return NO;
//return YES;//需要处理返回的URL返回YES
}
return YES;
}
当程序入口函数返回YES时调用下面方法:
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
NSString *urlStr = [urlabsoluteString];
// 支付宝app进app
if(sourceApplication && [sourceApplication isEqualToString:@"com.alipay.iphoneclient"]){
[[AlipaySDK defaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {
NSDictionary *dict = @{ALIPayDidCompleteURLKey:urlStr};
[[NSNotificationCenter defaultCenter] postNotificationName:ALIPayFromZFBDidCompleteNotification object:resultDic userInfo:dict];
}];
}
returnYES;
}
在支付类初始化方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processAliPayResult:)
name:ALIPayFromZFBDidCompleteNotification object:nil];