[iOS]iOS中使用URLScheme进行App跳转

URLScheme的作用

我们都知道苹果手机中的APP都有一个沙盒,APP就是一个信息孤岛,相互是不可以进行通信的。 但是iOS的APP可以注册自己的URL Scheme,URL Scheme是为方便app之间互相调用而设计的。我们可以通过系统的OpenURL来打开该app,并可以传递一些参数。
 URL Scheme必须能唯一标识一个APP,如果你设置的URL Scheme与别的APP的URL Scheme冲突时,你的APP不一定会被启动起来。因为当你的APP在安装的时候,系统里面已经注册了你的URL Scheme。 一般情况下,是会调用先安装的app。但是iOS的系统app的URL Scheme肯定是最高的。所以我们定义URL Scheme的时候,尽量避开系统app已经定义过的URL Scheme。
URL Schemes 有两个单词:
  • URL,我们都很清楚,http://www.apple.com 就是个 URL,我们也叫它链接或网址;
  • Schemes,表示的是一个 URL 中的一个位置——最初始的位置,即 ://之前的那段字符。比如 http://www.apple.com 这个网址的 Schemes 是 http。

注册URLScheme

1.在info.plist里添加URL types

每一个项目里面都会有一个info.plist配置文件。找到info.plist,右键选择Add Row,然后选择URL types。如图所示:


2.添加URL Schemes

添加完URL types,点击展开。右键选择Add Row,添加URL Schemes:


3.设置URL Schemes

设置URL Schemes为iOSDevTip


4.设置URL Identifier

URL Identifier是自定义的 URL scheme 的名字,一般采用反转域名的方法保证该名字的唯一性,比如 com.iOSStrongDemo.www

在Info->URL Types下编辑

当然我们可以看一些生成的源码


扫描二维码关注公众号,回复: 2684786 查看本文章

添加成功启动提示

为了方便测试,我们在AppDelegate里面添加一个UIAlertView,当app被成功打开时,会提出提示:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL*)url
{
    // 接受传过来的参数
    NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"
                                           message:text
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
    [alertView show];
    return YES;
}

Safari启动自定义的URLSchemes APP

既然已经配置好URL Schemes,那么我们可以来款速测试一下,我们设置的URL Schemes是否有效。打开Safari(有点浏览器打不开,应该是浏览器内部给屏蔽了吧),在地址栏里输入:iOSDevTip://

果然成功打开:果然成功打开:

果然成功打开:



打开注册iOSDevTip的APP格式为: URL Scheme://URL identifier,直接调用URL Scheme也可打开程序, URL identifier是可选的。

URL传参格式

 // 被启动的APP处理传过来的参数
 - (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"sourceApplication: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);

    // 接受传过来的参数
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"打开啦"
                                                        message:[url query]
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];

    return YES;
}
当APP被启动是,会调用代理方法application:openURL:sourceApplication:annotation:。参数URL就是启动APP的URL,参数sourceApplication就是来源APP的Bundle ID。
我们依然通过Safari来测试,在Safari的地址栏中输入: iOSDevTip://?name=ligang&phone=13888888888

即可打开APP,看看参数是否传递过来:

URLSchemes白名单配置

iOS9系统策略更新,限制了http协议的访问,此外应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装。

受此影响,当你的应用在iOS 9中需要使用 QQ/QQ空间/支付宝/微信SDK的相关能力(分享、收藏、支付、登录等)时,需要在“Info.plist”里增加如下代码:

<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>

查找基本URLSchemes

基本 URL Schemes 的查找方法可以通过 App 中的 info.plist 来查询,
  1. 首先,在 iTunes 找到你想用 URL 打开的 App,右键选择在文件夹中显示:
  2. 然后把这个文件复制到桌面上解压(或者其它地方,总之不要直接在原文件夹解压):解压完毕后,在解压出的文件夹中,找到 .app 文件
  3. 然后选择显示包内容:找到 info.plist 这个文件,用你电脑里能打开它的 App 打开它(Mac 上用 TextWrangler 就好)。
  4. 然后查找 URLSchemes

打开系统自带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"]];
打开Safari,在地址栏里输入,"tel://8008808888",就会打开电话软件并已经拨号了"8008808888

猜你喜欢

转载自blog.csdn.net/ouyangshima/article/details/79450412