应用间通信总结

一.准备工作

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;
}

应用间通信的所有的坑都在这儿了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在简单项目中,有使用到apns推送服务,许多文章有涉及到却没有讲清楚。最近做福路通项目,有使用到,做一个总结。 推...
    天空的守望者阅读 917评论 0 3
  • 前言: 以下内容是作者在实际开发中所总结的,主要列举了一些实用小技巧,也希望在实际开发中能够帮到你。 设置控件的圆...
    暗香有独阅读 1,400评论 6 33
  • 原文 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 1.UITableView的Group...
    无沣阅读 795评论 0 2
  • 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 UITableView的Group样式下顶部...
    管你爱不爱阅读 432评论 0 1
  • 1.我不知道要问什么问题 2.所问问题没有针对性,不知道怎么问问题,问的问题我想要了解的信息或我想要的答案 3.没...
    彼此间阅读 238评论 0 0