[英]How to open my iOS App with custom URL scheme in Swift 3?
I need to open my particular UIViewController when the following link is clicked on the Safari browser: 在Safari浏览器中单击以下链接时,我需要打开特定的UIViewController:
http://my.sampledomain.com/en/customer/account/resetpassword/?id=24&token=8fbf662617d14c10f4a11f716c1b2285 http://my.sampledomain.com/en/customer/account/resetpassword/?id=24&token=8fbf662617d14c10f4a11f716c1b2285
When this link is clicked on the browser, I need to open my application on a particular screen and retrieve the data from this url. 当在浏览器中单击此链接时,我需要在特定屏幕上打开我的应用程序,并从该URL中检索数据。 For example:
例如:
id = 24
token = 8fbf662617d14c10f4a11f716c1b2285
...and pass it to that particular UIViewController. ...并将其传递给该特定的UIViewController。
How can i do that? 我怎样才能做到这一点?
What you are describing is called Deep Linking . 您所描述的称为“ 深层链接” 。 It's a very common app feature to implement — most apps have it — and conceptually, it seems like an easy thing to build.
这是实施中非常常见的应用程序功能-大多数应用程序都具有它-从概念上讲,构建起来似乎很容易。 However, it's complicated to get right, and there are a lot of edge cases.
但是,正确处理很复杂,并且存在很多极端情况。
You basically need to accomplish two things: 您基本上需要完成两件事:
While not required, you'll also probably want to track all of this activity so you can see what is working. 虽然不是必需的,但您可能还希望跟踪所有这些活动,以便可以看到正在执行的操作。
Your existing custom URI scheme fits into this category. 您现有的自定义URI方案属于此类别。 However, Apple has decided that custom URI schemes are not a good technology, and deprecated them with iOS 9 in favor of Universal Links .
但是,Apple认为自定义URI方案不是一项好技术,因此在iOS 9中弃用了它们,转而支持Universal Links 。
Apple is right about this. 苹果对此是正确的。 Custom URI schemes have a number of problems, but these are the biggest:
自定义URI方案有很多问题,但这是最大的问题:
To work around these, it used to be possible to use a regular http://
link, and then insert a redirect on the destination page to forward the user to your custom URI scheme, thereby opening the app. 要解决这些问题,以前可以使用常规的
http://
链接,然后在目标页面上插入重定向以将用户转发到您的自定义URI方案,从而打开应用程序。 If that redirect failed, you could then redirect users to the App Store instead, seamlessly. 如果该重定向失败,则可以无缝地将用户重定向到App Store。 This is the part Apple broke in iOS 9 to drive adoption of Universal Links.
这是苹果公司在iOS 9中打破的部分,以推动通用链接的采用。
Universal Links are a better user experience, because they are http://
links by default and avoid nasty errors. 通用链接是一种更好的用户体验,因为默认情况下它们是
http://
链接,避免了讨厌的错误。 However, they are hard to set up and still don't work everywhere . 但是,它们很难设置, 仍然无法在任何地方使用 。
To ensure your users end up inside the app when they have it installed, you need to support both Universal Links and a custom URI scheme, and even then there are a lot of edge cases like Facebook and Twitter which require special handling. 为了确保用户在安装应用程序后最终进入应用程序,您需要同时支持通用链接和自定义URI方案,即使如此, Facebook和Twitter等许多边缘情况也需要特殊处理。
In this case, the user will end up on your http://
fallback URL. 在这种情况下,用户最终将进入您的
http://
后备URL。 At this point, you have two options: 此时,您有两个选择:
Most large brands prefer the second option. 大多数大品牌都喜欢第二种选择。 Smaller apps often go with the first approach, especially if they don't have a website.
较小的应用程序通常采用第一种方法,尤其是在没有网站的情况下。
To forward the user to the App Store, you can use a Javascript redirect like this: 要将用户转发到App Store,您可以使用Java重定向,如下所示:
<script type="text/javascript">
window.onload = function() {
window.location = "https://itunes.apple.com/app/id1121012049";
};
</script>
Until recently, it was possible to use a HTTP redirect for better speed, but Apple changed some behavior in Safari with iOS 10.3 , so this no longer works as well. 直到最近,仍可以使用HTTP重定向来提高速度,但是Apple更改了iOS 10.3在Safari中的某些行为 ,因此不再有效。
Unfortunately there's no native way to accomplish this last piece on either iOS or Android. 不幸的是,在iOS或Android上没有完成本最后部分的本机方法。 To make this work, you need a remote server to close the loop.
为此,您需要一个远程服务器来关闭循环。 You can build this yourself , but you really shouldn't for a lot of reasons, not the least of which being you have more important things to do.
您可以自己构建它 ,但实际上不应出于很多原因,其中最重要的一点就是您需要做更多重要的事情。
Deep linking is very complicated. 深层链接非常复杂。 Most apps today don't attempt to set it up by building an in-house system.
如今,大多数应用程序都不会尝试通过构建内部系统来进行设置。 Free hosted deep link services like Branch.io (full disclosure: they're so awesome I work with them) and Firebase Dynamic Links can handle all of this for you, and ensure you are always up to date with the latest standards and edge cases.
免费托管的深层链接服务,例如Branch.io (全面披露:它们非常出色,我可以与他们合作),Firebase Dynamic Links可以为您处理所有这些问题,并确保您始终了解最新的标准和最新案例。
See here for a video overview an employee at Branch made of everything you need to know about this. 观看此处的视频概述,您需要了解的一切都由Branch的一名员工组成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.