亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

iOS 之URL Scheme簡單配置

標簽:
iOS

写在前面

  • 市场部的同事反应要更新iOSAPP,需要技术这边提供一些应用信息,如下图:

webp

应用宝上需要填写的信息

  • URL Scheme是个什么鬼,有什么用,怎么配置,如何测试配置成功?
    谷歌或者百度 iOS URL Scheme,会出来很多相关的知识,我参考的是这个链接

URL Scheme的作用
我们都知道苹果手机中的APP都有一个沙盒,APP就是一个信息孤岛,相互是不可以进行通信的。但是iOS的APP可以注册自己的URL Scheme,URL Scheme是为方便app之间互相调用而设计的。我们可以通过系统的OpenURL来打开该app,并可以传递一些参数。
例如:你在Safari里输入www.alipay.com,就可以直接打开你的支付宝app,前提是你的手机装了支付宝。如果你没有装支付宝,应该显示的是支付宝下载界面,点击会跳到AppStore的支付宝下载界面。
URL Scheme必须能唯一标识一个APP,如果你设置的URL Scheme与别的APP的URL Scheme冲突时,你的APP不一定会被启动起来。因为当你的APP在安装的时候,系统里面已经注册了你的URL Scheme。
一般情况下,是会调用先安装的app。但是iOS的系统app的URL Scheme肯定是最高的。所以我们定义URL Scheme的时候,尽量避开系统app已经定义过的URL Scheme。


正题

大概分为两个步骤:

一  注册URL Scheme,即配置info.plist 文件

webp

URL identifier可以写成自己域名倒序,URL Schemes第一项尽量写成跟自己应该相关,唯一性的

二 添加代码,测试配置成功与否

  • 在Appdelegate.m文件里添加如下代码

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL*)url
{    // 接受传过来的参数
    NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"iOS8打开啦" message:text preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {
    }]];
    [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];    
    return YES;
}//iOS9以后走的是这个方法-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{    NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"iOS9打开啦" message:text preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {
    }]];
    [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];    return YES;
}```####问题:iPhone5s 8.3,测试的时候,这两个方法竟然都没走,不知何故?***
* 在手机浏览器中输入你刚才填写的URL Scheme(例如:iOSBaidu://),回车,此时会发现自动跳到你的应用,并显示alert相应信息。![注意是URLScheme,不是URL identifier](http://upload-images.jianshu.io/upload_images/2026235-aca441a9beffb696.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)![地址栏输入自己设置的URLScheme](http://upload-images.jianshu.io/upload_images/2026235-0be71cf8657a7b9d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)***
* 通过另一个APP启动注册了URL Schemes的APP

NSString *url = @"iOSDevTip://";
//    NSString *url = @"if ([[UIApplication sharedApplication]
canOpenURL:[NSURL URLWithString:url]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
else
{
NSLog(@"can not open URL scheme iOSDevTip");
} ```

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



作者:春夏love雨雪
链接:https://www.jianshu.com/p/fe15ee4774b8


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消