简体   繁体   English

如何在Swift 3中使用自定义URL方案打开iOS App?

[英]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: 您基本上需要完成两件事:

  1. If the app is installed: open the app and route users to the correct content inside it. 如果已安装该应用程序:打开该应用程序,并将用户引导至其中的正确内容。
  2. If the app is NOT installed: forward users to the App Store so they can download it. 如果未安装该应用程序:将用户转发到App Store,以便他们可以下载它。 Ideally, also route users to the correct content inside the app after downloading (this is known as 'deferred deep linking'). 理想情况下,还应下载后将用户引导至应用程序内的正确内容(这称为“延迟深层链接”)。

While not required, you'll also probably want to track all of this activity so you can see what is working. 虽然不是必需的,但您可能还希望跟踪所有这些活动,以便可以看到正在执行的操作。

If the app is installed 如果已安装该应用程序

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方案有很多问题,但这是最大的问题:

  • There is no fallback if the app isn't installed. 如果未安装该应用程序,则没有回退。 In fact, you get an error. 实际上,您会得到一个错误。
  • They often aren't recognized as links the user can click. 它们通常不被视为用户可以单击的链接。

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方案,即使如此, FacebookTwitter等许多边缘情况也需要特殊处理。

If the app is NOT installed 如果未安装该应用程序

In this case, the user will end up on your http:// fallback URL. 在这种情况下,用户最终将进入您的http://后备URL。 At this point, you have two options: 此时,您有两个选择:

  1. Immediately forward the user directly to the App Store. 立即将用户直接转发到App Store。
  2. Send the user to your mobile website (and then use something like a smart banner to give them the option of going to the App Store). 将用户引导到您的移动网站(然后使用类似智能横幅的内容使他们可以选择进入App Store)。

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中的某些行为 ,因此不再有效。

Deferred deep linking 延迟深层连结

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. 您可以自己构建它 ,但实际上不应出于很多原因,其中最重要的一点就是您需要做更多重要的事情。

Bottom line 底线

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.

相关问题 当应用程序使用URL方案在iOS中打开我的应用程序时,如何获取呼叫者的信息 - How to get the caller's info When a app using URL Scheme to open my App in iOS iOS自定义Url Scheme在App中第一次使用Url scheme打不开 - iOS Custom Url Scheme in App does not open the first time Url scheme is used 无法通过URL方案从我的iOS应用中打开MS-Excel iOS应用中的excelSheet - Unable to open my excelSheet in MS-Excel iOS app from my iOS app via url scheme iOS Swift LinkedIn 应用程序 URL 方案不起作用 - iOS Swift LinkedIn App URL Scheme is not working 是否可以通过使用自定义 url 方案打开本机 iOS 应用程序而无需网页定向到自定义 url? - Is it possible to open an Native iOS app by using Custom url scheme without a webpage directs to custom url? 在iOS应用中使用自定义网址方案打开应用 - Opening app with custom url scheme in ios app 使用自定义 URL Scheme 在 Swift 中打开 Instagram 应用程序 - Opening Instagram App in Swift, with a custom URL Scheme 打开iOS应用程序从URL中的电子邮件(自定义URL方案) - Open iOS App from URL in E-Mail (Custom URL Scheme) DocuSign iOS应用程序自定义URL方案? - DocuSign iOS app custom URL scheme? 为 iOS 应用程序定义 SSL 自定义 URL 方案 - Defining an SSL Custom URL Scheme for an iOS App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM