1.首先要在info.plist文件中配置要喚醒的字段如testApp。
在URL Schemes中添加一個要喚醒的字段如testApp。當瀏覽器中輸入以testApp://開通的連接後,就能喚醒我們的應用。
2.處理連接喚醒應用的連接。
在協議方法裏
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullableNSString *)sourceApplication annotation:(id)annotation
處理我們需要做的邏輯如:
NSString *urlString = [NSStringstringWithFormat:@"%@",url];
//連接喚醒應用
NSString *headerString=[urlString substringToIndex:7];
NSLog(@"headerString is %@ ",headerString);
if ([headerString isEqualToString:@"testApp"]) {
NSLog(@"瀏覽器打開應用 ========");
if([urlString length]>11){
NSString *content=[urlString substringFromIndex:11];
//截取連接後面的內容 可以供我們處理跳轉頁面等
NSLog(@"content is %@ ",content);
//處理喚醒的內容
}
return YES;
}
2.後續 在IOS10.2有時收不到回調需要增加 在此回調裏處理 有新的api儘量使用新的api 以免導致一些方法使用有問題。
//add for ios10.2
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
}