- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
说明:当应用程序启动时执行,应用程序启动入口。只在应用程序启动时执行一次。
application参数用来获取应用程序的状态、变量等,值得注意的是字典参数:(NSDictionary *)launchOptions,该参数存储程序启动的原因。
若用户直接启动,lauchOptions内无数据;
若由其他应用程序通过openURL:启动,则UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString);
若由本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification);
若由远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo(NSDictionary);
其他key还有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,
UIApplicationLaunchOptionsNewsstandDownloadsKey。 如果要在启动时,做出一些区分,那就需要在下面的代码做处理。 比如:应用可以被某个其它应用调起(作为该应用的子应用),要实现单点登录,那就需要在启动代码的地方做出合理的验证,并跳过登录。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSURL *url = [launchOptionsobjectForKey:UIApplicationLaunchOptionsURLKey];
if(url) {
}
NSString *bundleId = [launchOptionsobjectForKey:UIApplicationLaunchOptionsSourceApplicationKey];
if(bundleId) {
} UILocalNotification * localNotify = [launchOptionsobjectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if(localNotify) { }
NSDictionary * userInfo = [launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if(userInfo) {
}}
应用 /**
* 跳转支付宝app支付时,app被kill,支付完成后,从支付宝app跳转app,return NO 不调用application: openURL:sourceApplication:annotation:(返回app首页)
*/
if (launchOptions) {
NSString *appIDF = [launchOptionsobjectForKey:UIApplicationLaunchOptionsSourceApplicationKey];
if (appIDF && [appIDFisEqualToString:@"com.alipay.iphoneclient"]) {
returnNO;
}
}
#pragma mark--外部应用跳转到app的url处理
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSString *urlStr = [urlabsoluteString];
// 支付宝app进app
if(sourceApplication && [sourceApplicationisEqualToString:@"com.alipay.iphoneclient"]){
[[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {
NSDictionary *dict = @{ALIPayDidCompleteURLKey:urlStr};
[[NSNotificationCenterdefaultCenter ] postNotificationName:ALIPayFromZFBDidCompleteNotificationobject:resultDic userInfo:dict];
}];
}
returnYES;
}
//在支付类初始化方法
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(processAliPayResult:)name:ALIPayFromZFBDidCompleteNotificationobject:nil];