參考資料:app喚醒app
被喚起端需要做的工作(demoApp):
1.設置URL Scheme 只是一個app的標識 具體是什么自己定 一個Scheme對應一個app 對應的identifier是項目的build id
2.核查info.plist文件中是否也有對應的值
被喚醒端的工作就做好了.
在appdelegate控制器的這個方法里可以拿到具體的請求信息 從而可以有選擇的去判斷是否要喚醒app
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { NSLog(@"Calling Application Bundle ID: %@", sourceApplication); NSLog(@"URL scheme:%@", [url scheme]); NSLog(@"URL query: %@", [url query]); // Customer Code return YES; }
喚醒端工作:(test)
1.打開對應的scheme:
需要注意的一點事 這個url需要在scheme的尾部添加:// 比如設定的scheme是A 那么這個要打開的url則是A://
- (void)awakeOtherApp { NSString *customURL = @"openDemoApp://"; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error" message:[NSString stringWithFormat: @"No custom URL defined for %@", customURL] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } }
在對應的位置調用這個方法即可
在ios9以后,因為注重了安全問題,所以需要在info.plist文件中設置一個白名單,如果不設置的話會包以下錯誤信息:
-canOpenURL: failed for URL: "openDemoApp://" - error: "This app is not allowed to query for scheme opendemoapp"
白名單設置如下:
在info.plist文件中添加:
值就是之前設置的scheme 這個是沒有://的
h5調用app的方法可以參照上面鏈接
demo (提取碼: ysfu)