如何不以编程方式处理某些通用链接? [英] How to NOT handle some Universal Links programmatically?

查看:81
本文介绍了如何不以编程方式处理某些通用链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用处理通用链接,以将野生动物园用户重定向到我的应用.到目前为止,一切工作都很好,如果用户从Google点击指向我网站的链接,则我的应用程序将打开,而不是这样打开我的网站:

My iOS application handles universal links to redirect safari users to my app. So far everything is working great, if a user tap a link to my web site from Google my app is opening instead of my web site like this:

from safari https://my-web-site.com -> my-app

但是我的应用程序未实现我的网站所具有的某些功能,因此我想以编程方式拒绝某些URL,并让我的用户使用safari,而不是像这样在我的应用程序中重定向他:

But my app doesn't implement certains features that my web site does, so I would like to programmatically reject some URLs and let my users on safari instead of redirecting him in my app, like this:

from safari https://my-web-site.com -> my-app

OR

from safari https://my-web-site.com?reject -> safari

根据

重要的是要了解,如果您的应用程序使用 openURL :打开指向您网站的通用链接,则该链接不会在您的应用程序中打开.在这种情况下,iOS会识别出呼叫来自您的应用程序,因此您的应用程序不应将其视为通用链接.

It’s important to understand that if your app uses openURL: to open a universal link to your website, the link does not open in your app. In this scenario, iOS recognizes that the call originates from your app and therefore should not be handled as a universal link by your app.

不幸的是,这不是附加内容,而是停留在野生动物园中,我的用户被短暂重定向到我的应用程序,然后像这样再次重定向到野生动物园中:

Unfortunately that's not what's appending, instead of staying in safari, my user is redirected to my app for a brief moment, then redirected again to safari like this:

from safari https://my-web-site.com?reject -> my-app (briefly) -> safari

这是我的AppDelegate.swift代码:

here is my code for AppDelegate.swift:

internal func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    guard let url = userActivity.webpageURL else { return false }
    guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return false }

    let params = urlComponents.queryItems ?? []

    if params.contains(where: { $0.name == "reject" }) {
        // This line should prevent the app from opening according to Apple's documentation 
        application.open(url)
        return false
    } else {
        return true
    }
}

所以有人知道我如何才能像Apple文档所描述的那样拒绝通用链接吗?

So anyone have an idea how I could make the rejection of an Universal link the way it's described by Apple's documentation ?

注意:根据本地存储的数据,通用链接的拒绝需要由应用以编程方式完成.因此,请在苹果应用程序网站关联文件是不可选择:

note: the rejection of an universal link need to be done programmatically by the app, according to data stored locally. So adding a path exception in the apple-app-site-association file is not an option in my case :

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "XXXXXXXXX.com.my-app",
        "paths": [
          "*",
        "NOT /reject"
        ]
      }
    ]
  }
}

推荐答案

如果在paths数组中为条目加上"NOT"前缀,则可以显式阻止匹配.因此,在您的示例中,您可以执行

If you prefix an entry in the paths array with "NOT" then you can explicitly prevent a match. So in your example you could do something like

["NOT /settings/*", "NOT /activity/*", "/*"] 

可以防止带有/settings/activity前缀的路径,然后匹配其他所有内容.

which would prevent paths with /settings or /activity prefixes, and then match everything else.

参考

这篇关于如何不以编程方式处理某些通用链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆