【问题标题】:How to setup Associated Domains / Universal Links如何设置关联域/通用链接
【发布时间】:2021-06-26 12:17:33
【问题描述】:

我试图在访问 www.example.com/success 时将用户重定向到已安装的应用程序,并在访问主页时显示横幅 www.example.com

在我当前的植入中,网址不会重定向和打开应用程序,网站也不会显示横幅。

我正在关注此处的文档https://developer.apple.com/documentation/safariservices/supporting_associated_domains

我的网站是一个简单的create-react-app,其主页由 Firebase 托管。

我做了以下事情:

  1. 将关联域添加到签名和功能 - applinks:website.com/success
  2. apple-app-site-association文件添加到/public/
  3. 修改firebase.json文件
  4. index.html 中文件的链接

我用来验证的内容:

https://branch.io/resources/aasa-validator/

https://search.developer.apple.com/appsearch-validation-tool

通过将www.example.com/success 链接添加到笔记并打开来进行测试,它始终在 safari 中打开。我也试过重新安装应用并重启手机。

Apple API 验证

我已将带有关联域的新应用版本上传到商店

Branch.io

{
    "applinks": {
        "apps": [],
        "details": []
    }
}

访问www.example.com/apple-app-site-association

{ "activitycontinuation": { "apps": [ "team.com.example.com" ] }, “applinks”:{“apps”:[],“详细信息”:[{“appID”: "team.com.example.com", "paths": [ "/", "/", "/success", "/success/",] }, "webcredentials": { "应用”: [ “team.com.example.com”] } }

apple-app-site-association

{
  "activitycontinuation": {
      "apps": [
        "team.com.example.com"
      ]
  },
  "applinks": {
      "apps": [],
      "details": [
            {
                "appID": "team.com.example.com",
                "paths": [
                  "/",
                  "/*", 
                  "/success",
                  "/success/*", 
                ]
            },
      ]
  },
  "webcredentials": {
      "apps": [
        "team.com.example.com"
      ]
  }
}

Index.html

<link rel="apple-app-site-association file" href="%PUBLIC_URL%/apple-app-site-association">
<link rel="apple-app-site-association file" href="/apple-app-site-association">
<meta name="App" content="app-id=XXX, app-argument=https://apps.apple.com/US/app/APP/idXXX, affiliate- data=optionalAffiliateData">

Firebase.json

{
  "hosting": {
    "public": "public",
    "headers": [
      {
        "source": "/apple-app-site-association",
        "headers": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ]
      }
    ],
    "appAssociation": "NONE"
  }
}

设备控制台

在安装应用程序时,我在 Xcode 中监视设备控制台,通过 swdc 进程日志查找与关联域或请求相关的任何内容。这是我找到的一些;

获取企业管理的关联域数据时出错。如果这 设备不是企业管理的,这是正常的:错误 Domain=SWCErrorDomain Code=1701 "获取关联域数据失败 来自 ManagedConfiguration 框架。” UserInfo={NSDebugDescription=获取关联域数据失败 ManagedConfiguration 框架。,Line=298,Function=}

Entry { s = applinks, a = , d = au....ub....com, ua = unspecified, sa = 已批准 } 需要更新其 JSON,因为应用 PI 已更改

https://developer.apple.com/library/archive/qa/qa1916/_index.html

【问题讨论】:

  • “我试图在访问 www.example.com/app 时将用户重定向到已安装的应用程序”...您的字面意思是 example.com 还是您只是避免发布您的真实域?因为您无法关联您不拥有的域(没有写入权限)...
  • @A.Roe,我理解正确吗?您想添加指向您网站的链接,然后如果用户将在具有现有本机应用程序的设备上并单击它,则本机应用程序会上升,如果不遵循网站路线。对吗?
  • @Alladinian 是的,我知道,我只是在隐藏我的域名
  • @AmerllicA 不,当用户打开例如 www.example.com/app 的 URL 时,会改为打开应用程序。如果他们访问 www.example.com,则应显示应用横幅。
  • @A.Roe,另一个问题,这个www.example.com 属于你吗?根据你的描述,我猜,你想通过子路由为/App 的任何网站提高App。正确的?还是只是关于您自己的网站?

标签: ios reactjs firebase ios-universal-links applinks


【解决方案1】:

首先在您的设备日志和 JSON 格式中,您似乎应该遵循此处显示的示例:https://developer.apple.com/documentation/safariservices/supporting_associated_domains

在您的日志中,它可能会显示来自其他应用程序的日志,这些应用程序也具有过时的 API 格式。不过这应该不是什么大问题。

{
  "applinks": {
      "details": [
           {
             "appIDs": [ "ABCDE12345.com.example.app", "ABCDE12345.com.example.app2" ],
             "components": [
               {
                  "#": "no_universal_links",
                  "exclude": true,
                  "comment": "Matches any URL whose fragment equals no_universal_links and instructs the system not to open it as a universal link"
               },
               {
                  "/": "/buy/*",
                  "comment": "Matches any URL whose path starts with /buy/"
               },
               {
                  "/": "/help/website/*",
                  "exclude": true,
                  "comment": "Matches any URL whose path starts with /help/website/ and instructs the system not to open it as a universal link"
               },
               {
                  "/": "/help/*",
                  "?": { "articleNumber": "????" },
                  "comment": "Matches any URL whose path starts with /help/ and which has a query item with name 'articleNumber' and a value of exactly 4 characters"
               }
             ]
           }
       ]
   },
   "webcredentials": {
      "apps": [ "ABCDE12345.com.example.app" ]
   },
    "appclips": {
        "apps": ["ABCED12345.com.example.MyApp.Clip"]
    }
}

其次按照Apple的故障排除步骤和您的问题我猜您的问题可能是第7步

  • 您的应用从以下任一情况返回 false UIApplicationDelegate 协议方法: 应用程序(:continueUserActivity:restorationHandler:), 应用程序(:willFinishLaunchingWithOptions:), 应用程序(_:didFInishLaunchingWithOptions:)。

    这可能会发生,如果你 解析了传递给这些方法的 URL,并且您实现了 确定您的应用无法使用此 URL 的逻辑。

您尚未发布处理 URL 的方式。您需要按照其他答案中的建议实施 UIUserActivityRestoring 方法才能使其正常工作。这是一个 SwiftUI 版本

ContentView()   
    .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
        
        print("Continue activity \(userActivity)")
        guard let url = userActivity.webpageURL else {
            return
        }
        
        print("User wants to open URL: \(url)")
        // TODO same handling as done in onOpenURL()

    }

同时添加

.onOpenURL { url in
    print("URL OPENED")
    print(url) // parse the url to get someAction to determine what the app needs do
    if url.relativeString == "example://success" {

    }
}

https://developer.apple.com/library/archive/qa/qa1916/_index.html

【讨论】:

    【解决方案2】:

    apple-app-site-association中需要有一个appID:

    {
      "applinks": {
        "apps": [],
        "details": [
        {
          "appID": "XXXXXXXXXX.com.example.UniveralLinks",
          "paths": ["*"]
        }
        ]
      }
    }
    

    applinks 标签决定了哪些应用与网站相关联。将应用程序值保留为空数组。在 details 标记内是一组用于链接 appID 和 URL 路径的字典。为简单起见,您使用 * 通配符将该网站的所有链接与 UniversalLinks 应用程序相关联。您可以将路径值限制为特定文件夹或文件名。 appID 由您的团队 ID 和应用程序的捆绑包 ID 组成,但您需要为自己的帐户使用标识符。 在您创建 Apple 开发者帐户时,Apple 为您分配了一个团队 ID。你可以在 Apple 开发者中心找到它。登录网站,点击 Membership,然后在 Membership Information 部分查找 Team ID。

    现在您有了 apple-app-site-association,它必须上传到网络服务器。 您必须对网站具有“写入权限”才能执行此操作。确保您正确设置了 apple-app-site-association 文件(在您的 Web 服务器的根目录或 .well-known/ 中,并且没有重定向)。

    您还必须为通用链接添加到 iOS 应用程序的适当权利。 如果您使用的是 Xcode:

    1. 选择项目。
    2. 选择目标。
    3. 选择“功能”选项卡。
    4. 滚动至关联域并将其打开。

    然后你可以添加例如applinks:www.mywebsite.in 在 `Capabilities -> Associated Domains 下。

    或者在您的权利文件中,您可以添加如下内容:

    <key>com.apple.developer.associated-domains</key>
    <array>   
        ...
        <string>applinks:www.mywebsite.in</string>
        ...
    </array>
    

    处理通用链接: 既然应用程序和网站正式相互了解,应用程序所需要的只是在调用链接时处理链接的代码。

    func application(
      _ application: UIApplication,
      continue userActivity: NSUserActivity,
      restorationHandler: @escaping ([UIUserActivityRestoring]?
    ) -> Void) -> Bool {
      
      // 1
      guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let url = userActivity.webpageURL,
        let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
          return false
      }
      
      // 2 // dig out component for deepLinking reference. 
      if let component = ItemHandler.sharedInstance.items
        .filter({ $0.path == components.path}).first {
        //handle navigation to page indicated by component
        return true
      }
      
      // 3
      if let webpageUrl = URL(string: "http://universal-links-final.example.com") {
        application.open(webpageUrl)
        return false
      }
      
      return false
    }
    

    每当用户点击与应用相关的通用链接时,iOS 都会调用此方法。以下是每个步骤的作用: 首先,您验证传入的 userActivity 是否具有预期的特征。最终,您想要获取活动的路径组件。否则,您返回 false 以指示应用程序无法处理该活动。 使用该路径,您可以查找与其匹配的已知视图控制器。如果你找到了,你会为它展示视图控制器并返回 true。 如果找不到与路径匹配的视图控制器,则指示应用程序打开 URL,这将使用默认的系统应用程序——很可能是 Safari。您还可以在此处返回 false 以指示应用无法处理此用户活动。

    【讨论】:

    • 谢谢,我已经编辑了我的apple-app-site-association 文件,并且已经按照您提到的步骤进行操作。不幸的是仍然没有运气
    • func ‘continue userActivity’ 被命中了吗?
    • 不,因为应用程序没有打开所以它不会正确
    • 也得到错误 - 在范围内找不到“ItemHandler”
    • 这看起来像是一个不错的故障排除指南,它有点旧(考虑到这一点)但似乎通过了。 developer.apple.com/library/archive/qa/qa1916/_index.html
    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2015-12-20
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    相关资源
    最近更新 更多