iOS Url Scheme唤起App

Url Scheme

  • Url Scheme
    • ios Url Scheme配置
      • 1.选中项目,点击Info,打开URL Types。
      • 2.添加一条Url Scheme
      • 3.捕获第三方唤起请求
      • 4.通过网页唤起app
      • 5.通过openUrl开启系统app
      • 6.Url Scheme白名单


Url Scheme


Url Scheme(url 协议),顾名思义就是用于提供一个对外协议,用于第三方app或者网页唤起该应用。不同的app应该拥有不同的Scheme,避免与一些应用较为广泛的app使用相同的Scheme。通过Url Scheme不仅可以唤起应用,还能够在唤起时传递参数。


ios Url Scheme配置


1.选中项目,点击Info,打开URL Types。

iOS Url Scheme唤起App_第1张图片


2.添加一条Url Scheme

一个app可以同时拥有多个Scheme协议
Identifier:标示符可以和项目标示符相同,也可以随意填写
URL Scheme:这就是我们请求的协议
iOS Url Scheme唤起App_第2张图片
做好上面这一步后,xcode会自动在Info.plist中给你添加以下参数,你也可以直接在Info.plist中添加Url Scheme。
iOS Url Scheme唤起App_第3张图片


3.捕获第三方唤起请求

(1)在AppDelegate中添加以下方法

// 已废弃(适配),NS_DEPRECATED_IOS(4_2, 9_0)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return YES;
}
 
// 新方法,NS_AVAILABLE_IOS(9_0);
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
     
}

(2)实现-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options方法并解析传递的参数

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    NSString *urlStr = url.absoluteString;
    BOOL ret = [urlStr hasPrefix:@"TestApp://test"];
    if(ret){
    	//打印参数
        NSLog(@"url query:%@",[url query]);
        //将参数转换为字典
        NSArray *paramsArray = [[url query] componentsSeparatedByString:@"&"];
        NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
        for(NSString *param in paramsArray){
            NSArray *curParams = [param componentsSeparatedByString:@"="];
            [params setValue:[curParams lastObject] forKey:[curParams firstObject]]; 
        }
        NSLog(@"url scheme params:%@",params);
    }
    return ret;
}

(3)在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中判断是否是第三方唤起app

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if(launchOptions[UIApplicationLaunchOptionsURLKey] != nil){
        //第三方应用打开该app
    }else{
       //自己启动app
    }
    return YES;
}

4.通过网页唤起app

传递参数的方式参考http GET请求
(1)使用html页面来跳转

<html>
 
    <head>
 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
        <title>Insert title heretitle>
 
    head>
 
    <body>
 
        <a href="TestApp://test?a=100&b=99&c=88">open appa><br/>
 
    body>
 
html>

(2)在Safari中直接输入TestApp://test(Url Scheme)也可以直接唤起app


5.通过openUrl开启系统app

//打开系统设置:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];  
//打开蓝牙:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=Bluetooth"]]; 
//打开TWitter:   
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]]; 
//调用 自带mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]];
//调用 电话phone
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8008808888"]];
//调用 SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
//调用自带 浏览器 safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.hzlzh.com"]];

6.Url Scheme白名单

<key>LSApplicationQueriesSchemes</key>
 <array>
    <!-- 微信 URL Scheme 白名单-->
    <string>wechat</string>
    <string>weixin</string>
 
    <!-- 新浪微博 URL Scheme 白名单-->
    <string>sinaweibohd</string>
    <string>sinaweibo</string>
    <string>sinaweibosso</string>
    <string>weibosdk</string>
    <string>weibosdk2.5</string>
 
    <!-- QQ、Qzone URL Scheme 白名单-->
    <string>mqqapi</string>
    <string>mqq</string>
    <string>mqqOpensdkSSoLogin</string>
    <string>mqqconnect</string>
    <string>mqqopensdkdataline</string>
    <string>mqqopensdkgrouptribeshare</string>
    <string>mqqopensdkfriend</string>
    <string>mqqopensdkapi</string>
    <string>mqqopensdkapiV2</string>
    <string>mqqopensdkapiV3</string>
    <string>mqzoneopensdk</string>
    <string>wtloginmqq</string>
    <string>wtloginmqq2</string>
    <string>mqqwpa</string>
    <string>mqzone</string>
    <string>mqzonev2</string>
    <string>mqzoneshare</string>
    <string>wtloginqzone</string>
    <string>mqzonewx</string>
    <string>mqzoneopensdkapiV2</string>
    <string>mqzoneopensdkapi19</string>
    <string>mqzoneopensdkapi</string>
    <string>mqzoneopensdk</string>
 
    <!-- 支付宝  URL Scheme 白名单-->
    <string>alipay</string>
    <string>alipayshare</string>
 
</array>

你可能感兴趣的:(IOS)