一、URL Scheme
相信大家都知道 URL,例如 http://www.jianshu.com/就是一个URL。在 :// 之前的部分就称为 URL Scheme。
也就是说 http://www.jianshu.com/ 的 URL Scheme 就是 http
URL Scheme 就是一个可以让 app 相互之间可以跳转的协议
1.打开Mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”mailto:frank@wwdcdemo.example.com“]]
2.打开电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”tel:10086”]];
3.打开SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”sms:10086”]];
4.通过自定义 URL Scheme 向应用传递参数
举例:支付宝的URL Scheme :alipay2018081200218976://safepay/?{“memo”:{“result”:””,”ResultStatus”:”6001”,”memo”:”用户中途取消”},”requestType”:”safepay”}
二、白名单
1.实际项目里用到的常见白名单。
2.自定义白名单。
白名单一般是和URL Scheme结合使用的。
比如app_A想打开app_B,app_B的URL Schemes是haha。
我们就需要在app_A里的info.plist添加白名单。
废话不多说,直接上代码(app_A里写)1
2
3
4
5
6
7
8
9
10
11
12
13
14- (void)clickBtn {
NSString *urlString = @"haha://";
// 若有中文传输需要进行转义
NSString *customURL = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// 检查自定义 URL 是否被定义,如果定义了,则使用 shared application 实例来打开 URL
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]]) {
// openURL: 方法启动应用并将 URL 传入应用,在此过程中,当前的应用进入后台
[[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];
}
}
1 | 后记: |