一.準備工作
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;
}
應用間通信的所有的坑都在這兒了