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

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

问题描述

我的 iOS 应用程序处理通用链接以将 safari 用户重定向到我的应用程序.到目前为止,一切都运行良好,如果用户从 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

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

根据 Apple 文档:

请务必了解,如果您的应用使用 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.

不幸的是,这不是附加的内容,我的用户没有停留在 safari 中,而是被重定向到我的应用程序一小会儿,然后再次重定向到 safari,如下所示:

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 ?

注意:根据本地存储的数据,应用程序需要以编程方式拒绝通用链接.所以在 apple-app-site-association 文件在我的情况下不是一个选项:

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"
        ]
      }
    ]
  }
}

推荐答案

如果在路径数组中为条目添加 "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天全站免登陆