别的应用白名单设置 LSApplicationQueriesSchemes 不是必须,不设置 LSApplicationQueriesSchemes 只是不能使用判断 canOpenURL 最新的调用方法有跳转完成回调可以判断是否存在应用
NSString *urlStr = @"SchemeB://?message=schemeA";
NSURL *url = [NSURL URLWithString:urlStr];
// if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options: @{} completionHandler:^(BOOL success) {
NSLog(success ? @"存在" : @"不存在");
}];
// }else{
// NSLog(@"跳转schemeB不存在");
// }
URL types是必须的,不配置别的应用无法使用这个别名 scheme://跳过来
scheme.png
URL Schemes Array可以设置多个
- (IBAction)toSchemeBTap:(UIButton *)sender {
NSString *urlStr = @"SchemeB://?message=schemeA";
NSURL *url = [NSURL URLWithString:urlStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options: @{} completionHandler:nil];
}else{
NSLog(@"跳转schemeB不存在");
}
}
#import "AppDelegate+Scheme.h"
#import "NSString+query.h"
@implementation AppDelegate (Scheme)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
// NSLog(@"url: \n%@,options: \n %@, \n 参数: %@", url, options, [url.absoluteString getParamsWithUrlString]);
NSDictionary *dict = [url.absoluteString getParamsWithUrlString].lastObject;
NSString *fromScheme = [NSString stringWithFormat:@"%@://", dict[@"message"]];
NSURL *schemeUrl = [NSURL URLWithString: fromScheme];
if ([[UIApplication sharedApplication] canOpenURL:schemeUrl]) {
[[UIApplication sharedApplication] openURL:schemeUrl options: @{} completionHandler:nil];
}
return YES;
}
@end
#import "NSString+query.h"
@implementation NSString (query)
/**
获取url中的参数并返回
@return @[NSString:无参数url, NSDictionary:参数字典]
*/
- (NSArray*)getParamsWithUrlString {
if(self.length==0) {
NSLog(@"链接为空!");
return @[@"",@{}];
}
//先截取问号
NSArray*allElements = [self componentsSeparatedByString:@"?"];
NSMutableDictionary *params = [NSMutableDictionary dictionary];//待set的参数字典
if(allElements.count==2) {
//有参数或者?后面为空
NSString*myUrlString = allElements[0];
NSString*paramsString = allElements[1];
//获取参数对
NSArray*paramsArray = [paramsString componentsSeparatedByString:@"&"];
if(paramsArray.count>=2) {
for(NSInteger i =0; i < paramsArray.count; i++) {
NSString*singleParamString = paramsArray[i];
NSArray*singleParamSet = [singleParamString componentsSeparatedByString:@"="];
if(singleParamSet.count==2) {
NSString*key = singleParamSet[0];
NSString*value = singleParamSet[1];
if(key.length>0|| value.length>0) {
[params setObject:value.length>0?value:@""forKey:key.length>0?key:@""];
}
}
}
}else if(paramsArray.count==1) {
//无 &。url只有?后一个参数
NSString*singleParamString = paramsArray[0];
NSArray*singleParamSet = [singleParamString componentsSeparatedByString:@"="];
if(singleParamSet.count==2) {
NSString*key = singleParamSet[0];
NSString*value = singleParamSet[1];
if(key.length>0|| value.length>0) {
[params setObject:value.length>0?value:@""forKey:key.length>0?key:@""];
}
}else{
//问号后面啥也没有 xxxx? 无需处理
}
}
//整合url及参数
return@[myUrlString,params];
}else if(allElements.count>2) {
NSLog(@"链接不合法!链接包含多个\"?\"");
return @[@"",@{}];
}else{
NSLog(@"链接不包含参数!");
return@[self, @{}];
}
}
@end