外部链接打开APP
1 注册协议
/** 外部web传入路径 */
@property (nonatomic, strong) NSURL *openUrl;
2 启动设置
3 回调
// 其它应用启动本应用时,回调
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
self.openUrl = nil;
BOOL result = [UMSocialSnsService handleOpenURL:url];
if (result == FALSE) {
//调用其他SDK,例如支付宝SDK等
self.openUrl = url;
}
return result;
}
4 处理
// 程序获得焦点
- (void)applicationDidBecomeActive:(UIApplication *)application {
[PKTestMessage hidHUD];
// 1.发送程序启动通知
[[NSNotificationCenter defaultCenter] postNotificationName:ApplicationDidBecomeActiveNotification object:nil];
// 2.处理外部打开
if (self.openUrl != nil) {
[[[PKShareTool alloc]init] webShareWithUrl:self.openUrl];
self.openUrl = nil;
}
}
5 字符串串联
//调用外部链接处理程序
- (void)webShareWithUrl:(NSURL*)url{
//获取链接参数
NSMutableDictionary* resDict = [self converToDictionary:url.absoluteString];
//获取导航栏控制器
PKTabBarViewController *obj = (PKTabBarViewController*)KeyWindow.rootViewController;
PKNaviViewController* nav = [KeyWindow.rootViewController childViewControllers][obj.selectedIndex];
//实现跳转
PKCreateReadilyShareController* ctrl = [[PKCreateReadilyShareController alloc]init];
[nav pushViewController:ctrl animated:YES];
PKLog(@"%@",resDict);
[self showInfo:url];
}
//转换参数为字典
- (NSMutableDictionary*)converToDictionary:(NSString*)text{
NSString * str = text;
//参数处理
NSArray* ary = [str componentsSeparatedByString:@"?"];
NSString* funcName = [[ary[0] componentsSeparatedByString:@"/"]lastObject];
NSArray* params = [ary[1] componentsSeparatedByString:@"&"];
//转换字典
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for (NSString* param in params) {
NSArray* tmp =[param componentsSeparatedByString:@"="];
[dict setObject:tmp[1] forKey:tmp[0]];
}
[dict setObject:funcName forKey:@"api"];
PKLog(@"%@ /n %@",str,dict);
return dict;
}