跳至主要内容

iOS Universal Link 开发

原文链接
参考链接



背景

在iOS 9以前,我们从外部启动App都是通过URL Scheme实现跳转的。这种方式虽然可自定程度很高,能够巧妙地实现很多跳转,但弊端也很明显:我们只能通过scheme://example这种格式的链接来实现跳转,而且现在苹果还对这种方式的跳转加了一个提示框:“是否打开XXX”。对于对Web和原生App交互的场景需求量很大的产品来说,这样的跳转方式显然是步骤冗杂的,用户体验并不好。
iOS 9以后,Universal Link的出现解决了这个问题。它所提供的直接、顺畅、无缝衔接的跳转能够让用户体验提升一个级别。用户可以点击开发者指定的类似于https://example.com/t的URL直接唤醒App,而不需要在浏览器打开再点击其他按钮。
在你的App中添加这个功能很简单:
  • 苹果开发者网站中打开需要使用Universal Link功能的App中的Associated Domains
  • 上传apple-app-site-association到服务器根目录下
  • 在AppDelegate中实现相应的方法

配置

首先,我们要在苹果开发者网站中开启App的Associated Domains功能。
Account -> Certificates, Identifiers & Profiles -> App IDs -> YourApp -> Edit中把Associated Domains设置为Enable
然后我们需要配置一下工程文件,找到Capabilities -> Associated Domains
打开此功能并把你需要跳转的domain加进去,格式为applinks:www.example.com

Paste_Image.png

部署

注意

首先你的服务器必须得支持SSL
接下来,我们需要上传一个json文件到我们的服务器。json文件以apple-app-site-association命名

注意

文件不需要添加任何后缀
json的格式是这样的:
  1. {
  2. "applinks": {
  3. "apps": [],
  4. "details": [
  5. {
  6. "appID": "TeamID.com.domain.App",
  7. "paths":[ "*" ]
  8. }
  9. ]
  10. }
  11. }
  • appID:TeamID加上Bundle ID
  • paths:支持Universal Link,也就是可以跳转的路径。*代表此域名下所有路径都支持,也可以具体制定到某个页面例如/path/page或者某个路径下所有URL例如/path/*
关于paths的配置,可以参考苹果官方文档
  • Use * to specify your entire website
  • Include a specific URL, such as /wwdc/news/, to specify a particular link
  • Append to a specific URL, such as /videos/wwdc/2015/, to specify a section of your website
  • In addition to using to match any substring, you can also use ? to match any single character. You can combine both wildcards in a single path, such as /foo//bar/201?/mypage.
最后,我们只需要把配置好的json文件上传到服务器中该域名的根目录下,言下之意,我们可以用GET请求可以获取到https://www.example.com/apple-app-association
再次强调必须是HTTPS协议
当我们的App在设备上第一次运行时,如果支持Associated Domains功能,那么iOS会自动去GET定义的Domain下的apple-app-site-association文件。
需要留意iOS会先请求https://domain.com/.well-known/apple-app-site-association如果此文件请求不到,再去请求https://domain.com/apple-app-site-association
所以如果想要避免服务器接收过多GET请求,可以直接把apple-app-site-association放在./well-known/目录下

注意

服务器上apple-app-site-association的更新不会让iOS本地的apple-app-site-association同步更新,即iOS只会在App第一次启动时请求一次,以后除非App更新或重新安装否则不会在每次打开时请求apple-app-site-association

开发

待我们服务器部署好了,用curl测试一下apple-app-site-association能够正确GET到了,那么我们就需要在工程中响应跳转事件了。
我们在AppDelegate中实现如下代理方法:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
具体实现:
  1. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
  2. if (![userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
  3. return YES;
  4. }
  5. //读取url地址
  6. NSURL *webUrl = userActivity.webpageURL;
  7. if (![webUrl.path isEqualToString:@"/show"]) {
  8. //path错误,直接从safari打开
  9. [[UIApplication sharedApplication] openURL:webUrl];
  10. return YES;
  11. }
  12. //跳转并显示内容
  13. [[NSNotificationCenter defaultCenter] postNotificationName:@"notify" object:@"hello world"];
  14. return YES;
  15. }
这里的自由度就很高了,我们可以根据传入的任何符合跳转条件的URL进行不同的操作。

测试

现在一切都已经完成了,现在我们可以在短信中点击一个URL直接跳转到我们的App。至于如何检验URL是否能够跳转,一个快捷方便的方法就是在系统原生App中(如短信、邮件等)长按URL,如果弹出的选项中有在“your app”中打开,那么证明该URL是支持跳转的。

注意

非系统原生App不一定能支持直接点击URL跳转,例如在微信中点击URL会首先在微信内的WebView打开,如果要跳转只能再通过Safari打开。

评论

此博客中的热门博文

Resolving errSecInternalComponent errors during code signing

原文链接 One code signing issue I commonly see, both here on DevForums and in my Day Job™ with DTS, is that the codesign command fails with errSecInternalComponent. This issue crops up in a wide variety of circumstances and the correct fix depends on the specific problem. This post is my attempt to clarify the potential causes of this error and help folks resolve it. If you have any questions or comments about this, please start a new thread, tagging it with Code Signing so that I see it. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Resolving errSecInternalComponent errors during code signing In some circumstances the codesign command might fail with the error errSecInternalComponent. For example: % codesign -s "Apple Development" "MyTrue" MyTrue: errSecInternalComponent This typically affects folks who are signing code in a nonstandard environm...

iOS:检测使用VPN或Proxy

参考链接: https://www.jianshu.com/p/c3b950dbf86a https://gist.github.com/PramodJoshi/4faad4c91f7dcb4eb9b06be8390c01db http://noodlecode.net/2018/04/check-if-ios-app-is-connected-to-vpn 第一种方法 需要导入框架CFNetwork 然后,这个方法是mrc的:需要添加-fno-objc-arc的flag 代码如下: + ( BOOL )getProxyStatus { NSDictionary *proxySettings = NSMakeCollectable ([( NSDictionary *) CFNetworkCopySystemProxySettings () autorelease]); NSArray *proxies = NSMakeCollectable ([( NSArray *) CFNetworkCopyProxiesForURL (( CFURLRef )[ NSURL URLWithString: @"http://www.google.com" ], ( CFDictionaryRef )proxySettings) autorelease]); NSDictionary *settings = [proxies objectAtIndex: 0 ]; NSLog ( @"host=%@" , [settings objectForKey:( NSString *)kCFProxyHostNameKey]); NSLog ( @"port=%@" , [settings objectForKey:( NSString *)kCFProxyPortNumberKey]); NSLog ( @"type=%@" , [settings objectForKey:( NSString *)kCFProxyTypeKey]); if ([[settings object...

去广告DNS设置,国内ADGuard DNS方案,手机电脑iOS去广告,保护隐私

 原文链接 之前分享过使用mac系统搭建adguard home,这几个月用下来零零散散基本上也被弃用了。主要原因是因为需要保持电脑一直开机。但是我的电脑是笔记本,存在移动各个地域的情况,也就是说只能够屏蔽电脑自身,对于手机而言不太现实。今天偶然发现dnspod推出了高级版的公共解析。dnspod背靠腾讯云,肯定是合法合规的公共解析服务,这个高级版用起来不错。 国内自己搭建解析服务是违法行为,所以这也是为什么使用dnspod的原因。 后台截图 开始使用 首先我们先进入dnspod的公共解析页面,点击开始使用。 专业版公共解析 dnspod会提供几种预设,我们选择「开发者」即可 开发者 然后你就成功的申请到自己个人使用的dns了! 更新拦截规则 我们可以将常见的广告过滤规则加入到dns中。我们在顶部选项卡中选择「拦截规则」。 拦截规则设置 打开adguard adguard 绑定iOS设备 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 绑定macOS 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 mac需要在「系统偏好设置」的「网络」中查看是否正在运行。 代理 如果没有运行需要点击「···」来启动服务。 启动服务 绑定路由器 找到自己路由器的DHCP设置,修改dns,然后记得绑定自己的ip。 修改dns 绑定ip 费用 目前有300万次/月的免费额度,但没有超出之后的价格。300万次一个人比较难用完,可以放心使用。 我个人使用iOS设备两台、智能家居、电脑两台,日均请求数大致2万/日。 判断是否搭建成功 可以通过查看日志的方式,日志大概有半小时到一小时的延迟,请耐心等待。