一.准备工作
1.设置APP Schemes值, schemes作为app的url 格式为schemes://
2.相互添加白名单, 白名单为设置的schemes值
<key>LSApplicationQueriesSchemes</key>
<array>
<string> your schemes </string>
</array>
发送方:
BOOL judge = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"schemes://"]];
if (judge) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"schemes://success=1&count=100"]];
} else {
NSLog(@"未安装应用");
}
接收方
XXXXXXXX----(错误,9.0以后废弃的方法)接收方:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return YES;
}
正确的接收方法(先添加通知再发送通知,在接受过程中可能会存在衔接受到了,但是控制器还没有创建的问题):
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
NSString *receText = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@ %@",receText, url.absoluteString);
NSArray *arr = [receText componentsSeparatedByString:@"="];
NSString *str = [arr firstObject];
if ([str isEqualToString:@"AM_goodsID"]) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:[arr lastObject] forKey:@"goodId"];
// 延迟1秒执行先添加通知再发送
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[[NSNotificationCenter defaultCenter] postNotificationName:@"APPMASTERTOPRODETAIL" object:nil userInfo:dic];
});
}
return YES;
}
虽然跳转成功了,但是还会有一系列的问题,例如tabbar隐藏,navigationbar是否隐藏,颜色变化. 当进入到另一个app时,需要获取当前显示的控制器,下面是获取当前屏幕显示的viewcontroller的方法(网上普遍流传另一种方法,但是不能获取tabbar的控制器)
- (UIViewController *)getCurrentVC{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
//app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
id nextResponder = nil;
UIViewController *appRootVC=window.rootViewController;
// 如果是present上来的appRootVC.presentedViewController 不为nil
if (appRootVC.presentedViewController) {
nextResponder = appRootVC.presentedViewController;
}else{
UIView *frontView = [[window subviews] objectAtIndex:0];
nextResponder = [frontView nextResponder];
}
if ([nextResponder isKindOfClass:[UITabBarController class]]){
UITabBarController * tabbar = (UITabBarController *)nextResponder;
UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
// UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
result=nav.childViewControllers.lastObject;
}else if ([nextResponder isKindOfClass:[UINavigationController class]]){
UIViewController * nav = (UIViewController *)nextResponder;
result = nav.childViewControllers.lastObject;
}else{
result = nextResponder;
}
return result;
}
调用过程中会出现tabbar隐藏与否的问题,可以通过判断是否为根视图来设置
UIViewController *controller = [self getCurrentVC];
controller.hidesBottomBarWhenPushed = YES;
[controller.navigationController pushViewController:detail animated:YES];
for (UINavigationController *contr in self.viewControllers) {
if (contr.childViewControllers.firstObject == controller) {
controller.hidesBottomBarWhenPushed = NO;
}
应用间通信的所有的坑都在这儿了