iOS Mac Url Scheme唤起App


Url Scheme


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


ios Url Scheme配置


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

Url Scheme配置


2.添加一条Url Scheme

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


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<UIApplicationOpenURLOptionsKey,id> *)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 here</title>
 
    </head>
 
    <body>
 
        <a href="TestApp://test?a=100&b=99&c=88">open app</a><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://admin@hzlzh.com"]];
//调用 电话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>

Mac URL拉起

mac Url scheme前面的配置和iOS一样这里就不在重复

- (void) handleUrlEvent:(NSAppleEventDescriptor*)theEvent withReplyEvent:(NSAppleEventDescriptor*)replyEvent{
    NSString *url = [[theEvent paramDescriptorForKeyword:keyDirectObject] stringValue];
    if([url hasPrefix:@"test://bind"]){
     	//处理Scheme
    }
}

只做以上处理还是无法handle所有scheme,我们还需要添加以下代码

- (void)applicationWillFinishLaunching:(NSNotification *)notification{
    [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleUrlEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值