业务开发用到其他app跳转自己app到指定页面,实现如下:
-
在URL TYPES配置好scheme,约定好协议结构,如:scheme://key=vlaue
-
网页或者其他app点击协议scheme://key=vlaue跳转目标app
2.1 app跳转需要在其他app的info.plist添加LSApplicationQueriesSchemes白名单,否则会报错:This app is not allowed to query for scheme XXXX
image.png
image.png
3.目标app实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//app杀死状态下,用过scheme跳转过来的解析
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] ;
NSString *str = [url absoluteString];
if ([str hasPrefix:@"scheme"]) {
NSRange range = [str rangeOfString:@"key"];
NSString *value = [str substringFromIndex:range.location + range.length + 1];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",value);
});
}
..........
return YES;
}
#pragma mark - app在后台,被scheme调起的方法
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSString *scheme = [url scheme];
NSString *str = [url absoluteString];
if ([str hasPrefix:@"scheme"]) {
NSRange range = [str rangeOfString:@"key"];
NSString *value = [str substringFromIndex:range.location + range.length + 1];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",value);
});
}
return YES;
}
网友评论