写在前面
市场部的同事反应要更新iOSAPP,需要技术这边提供一些应用信息,如下图:
应用宝上需要填写的信息
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 文件
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相应信息。*** * 通过另一个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
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
100積分直接送
付費(fèi)專(zhuān)欄免費(fèi)學(xué)
大額優(yōu)惠券免費(fèi)領(lǐng)