简体   繁体   English

如何设置关联域/通用链接

[英]How to setup Associated Domains / Universal Links

I am trying to redirect the user to the installed app when visiting www.example.com/success as well as show a banner when visiting the homepage www.example.com .我试图在访问www.example.com/success时将用户重定向到已安装的应用程序,并在访问主页www.example.com时显示横幅。

With my current implantation the url DOES NOT redirect and open the app and neither does the website display a banner.在我目前的植入中,url 不会重定向和打开应用程序,网站也不会显示横幅。

I am following the documentation found here https://developer.apple.com/documentation/safariservices/supporting_associated_domains我正在关注此处找到的文档https://developer.apple.com/documentation/safariservices/supporting_associated_domains

My website is a simple create-react-app with a homepage hosted with Firebase.我的网站是一个简单的create-react-app ,其主页由 Firebase 托管。

I have done the following:我做了以下事情:

  1. Add Associated Domains to Signing and Capabilities - applinks:website.com/success将关联域添加到签名和功能 - applinks:website.com/success
  2. Add apple-app-site-association file to /public/apple-app-site-association文件添加到 /public/
  3. Amend firebase.json file修改firebase.json文件
  4. Link to files in index.html链接到 index.html 中的文件

What I am using to validate:我用来验证的内容:

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

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

Testing by adding www.example.com/success link to notes and opening, It always opens in safari.通过将www.example.com/success链接添加到注释并打开进行测试,它始终在 safari 中打开。 I Have also tried reinstalling the app and restarting the phone.我也试过重新安装应用程序并重新启动手机。

Apple API Validation苹果 API 验证

I have uploaded a new app version with associated domains to the store我已将带有关联域的新应用版本上传到商店

在此处输入图像描述

Branch.io科.io

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

When visiting www.example.com/apple-app-site-association访问www.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" ] } } { "activitycontinuation": { "apps": [ "team.com.example.com" ] }, "applinks": { "apps": [], "details": [ { "appID": "team.com. example.com”,“路径”:[“/”,“/ ”,“/success”,“/success/ ”,]},“webcredentials”:{“apps”:[“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索引.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 Firebase.json

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

Device Console设备控制台

When installing the app I monitor the device console in Xcode looking through swdc process logs for anything related to associated domains or requests.安装应用程序时,我在 Xcode 中监视设备控制台,查看与关联域或请求相关的任何内容的swdc进程日志。 Here's some I found;这是我找到的一些;

Error getting enterprise-managed associated domains data.获取企业管理的关联域数据时出错。 If this device is not enterprise-managed, this is normal: Error Domain=SWCErrorDomain Code=1701 "Failed to get associated domain data from ManagedConfiguration framework."如果此设备不是企业管理的,这是正常的:Error Domain=SWCErrorDomain Code=1701 "Failed to get associated domain data from ManagedConfiguration framework." UserInfo={NSDebugDescription=Failed to get associated domain data from ManagedConfiguration framework., Line=298, Function=} UserInfo={NSDebugDescription=无法从 ManagedConfiguration 框架获取关联的域数据。, Line=298, Function=}

Entry { s = applinks, a =, d = au….ub….com, ua = unspecified, sa = approved } needs its JSON updated because the app PI changed条目 { s = applinks, a =, d = au....ub....com, ua = unspecified, sa = approved } 需要更新其 JSON,因为应用 PI 已更改

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

There needs to be an appID in the apple-app-site-association : apple-app-site-association中需要有一个 appID:

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

The applinks tag determines which apps are associated with the web site. applinks 标签确定哪些应用与 web 站点相关联。 Leave the apps value as an empty array.将应用程序值保留为空数组。 Inside the details tag is an array of dictionaries for linking appIDs and URL paths.在 details 标签内是一个字典数组,用于链接 appID 和 URL 路径。 For simplicity, you use the * wildcard character to associate all of this web site's links with the UniversalLinks app.为简单起见,您使用 * 通配符将 web 站点的所有链接与 UniversalLinks 应用程序相关联。 You can limit the paths value to specific folders or file names.您可以将路径值限制为特定文件夹或文件名。 The appID consists of your team ID combined with the app's bundle ID, but you'll need to use the identifier for your own account. appID 由您的团队 ID 和应用程序的捆绑包 ID 组成,但您需要为自己的帐户使用标识符。 Apple assigned you a team ID when you created your Apple developer account.在您创建 Apple 开发者帐户时,Apple 为您分配了一个团队 ID。 You can find it in the Apple developer center.你可以在 Apple 开发者中心找到它。 Log into the web site, click on Membership, then look for Team ID in the Membership Information section.登录 web 站点,单击 Membership,然后在 Membership Information 部分中查找 Team ID。

Now you have the apple-app-site-association it must be uploaded to the web server.现在您有了 apple-app-site-association,它必须上传到 web 服务器。 You must have “write access” to the web site to do this.必须对 web 站点具有“写入权限”才能执行此操作。 Be sure you set up the apple-app-site-association file correctly (either at the root or in .well-known/ on your web server, and no redirects).确保您正确设置了apple-app-site-association文件(在 web 服务器上的根目录或 .well .well-known/中,并且没有重定向)。

You'll also have to add the appropriate entitlements for universal linking to the iOS app.您还必须添加适当的权利,以便通用链接到 iOS 应用程序。 If you're using Xcode:如果您使用的是 Xcode:

  1. Select the project. Select 项目。
  2. Select the target. Select 目标。
  3. Select the Capabilities tab. Select 功能选项卡。
  4. Scroll to and turn ON Associated Domains.滚动到并打开关联域。

Then you can add eg applinks:www.mywebsite.in under `Capabilities -> Associated Domains.然后您可以在 `Capabilities -> Associated Domains 下添加例如applinks:www.mywebsite.in

Or in your entitlements file, you can add something like the following:或者在您的权利文件中,您可以添加如下内容:

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

Handling Universal Links: Now that the app and the web site are officially aware of each other, all the app needs is code to handle the link when it's called.处理通用链接:既然应用程序和 web 站点正式相互了解,应用程序所需要的只是在调用链接时处理链接的代码。

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 calls this method whenever the user taps a universal link related to the app.每当用户点击与应用程序相关的通用链接时,iOS 都会调用此方法。 Here's what each step does: First, you verify that the passed-in userActivity has expected characteristics.下面是每个步骤的作用: 首先,验证传入的 userActivity 是否具有预期的特征。 Ultimately, you want to get the path component for the activity.最终,您想要获取活动的路径组件。 Otherwise, you return false to indicate that the app can't handle the activity.否则,您返回 false 以指示应用程序无法处理该活动。 Using the path, you look for a known view controller that matches it.使用该路径,您可以查找与其匹配的已知视图 controller。 If you find one, you present the view controller for it and return true.如果找到一个,则为它呈现视图 controller 并返回 true。 If you can't find a view controller that matches the path, you instruct the application to open the URL, which will use the default system app instead — most likely Safari.如果找不到与路径匹配的视图 controller,则指示应用程序打开 URL,这将使用默认系统应用程序 - 很可能是 Z50DE9BC68C93DC32D8C7C905934771。 You also return false here to indicate that the app can't handle this user activity.您还在此处返回 false 以指示应用程序无法处理此用户活动。

First at your device logs and your JSON format it seems your should be following the example shown here: https://developer.apple.com/documentation/safariservices/supporting_associated_domains首先在您的设备日志和 JSON 格式中,您似乎应该遵循此处显示的示例: https://developer.apple.com/documentation/safariservices/supporting_associated_domains

In your logs it may shows logs from other apps who also have the outdated API format.在您的日志中,它可能会显示来自其他应用程序的日志,这些应用程序也具有过时的 API 格式。 However this should not be so much of an issue.但是,这应该不是什么大问题。

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

Secondly following the steps in Apple's troubleshooting and your problem I am guessing your problem might be Step 7其次按照Apple的故障排除步骤和你的问题我猜你的问题可能是第7步

  • Your app returned false from one of the following UIApplicationDelegate protocol methods: application( :continueUserActivity:restorationHandler:), application( :willFinishLaunchingWithOptions:), application(_:didFInishLaunchingWithOptions:).您的应用从以下 UIApplicationDelegate 协议方法之一返回 false:application( :continueUserActivity:restorationHandler:)、application( :willFinishLaunchingWithOptions:)、application(_:didFInishLaunchingWithOptions:)。

    This can happen if you parsed the URL that is passed into these methods, and you implemented logic to determine that your app can not use this URL.如果您解析了传递给这些方法的 URL,并且您实施了逻辑来确定您的应用程序不能使用此 URL,则可能会发生这种情况。

You have not posted how you handle the URL.您尚未发布如何处理 URL。 You need to implement the UIUserActivityRestoring method as suggested in the other answer for it to work.您需要按照其他答案中的建议实现UIUserActivityRestoring方法才能正常工作。 Here is a SwiftUI version这是 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()

    }

Also add还添加

.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 https://developer.apple.com/library/archive/qa/qa1916/_index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有通配符的 iOS 关联域(通用链接)不起作用 - iOS Associated Domains (Universal Links) with Wildcards not working 虚幻引擎/ IOS-关联域/通用链接 - Unreal Engine / IOS - Associated Domains / Universal links Xamarin iOS通用链接-无法设置复选框启用关联域 - Xamarin iOS Universal Links - can not set checkbox Enable Associated Domains 将applinks中的字符*添加为“applinks:*。codestuffs.com”时,在iOS 9 Universal Links中出现问题,该内容位于“关联域”部分内 - Have trouble with iOS 9 Universal Links when adding character * in applinks as 'applinks:*.codestuffs.com' which is inside Associated Domains section ios 通用链接 - 来自多个域 - ios Universal Links - from multiple domains 如何在 iOS 应用程序中支持通用链接并为其设置服务器? - How to support Universal Links in iOS App and setup server for it? 如何在MyApp.entitlements中为com.apple.developer.associated-domains设置值? - how to setup the value for com.apple.developer.associated-domains in MyApp.entitlements? 如何调试通用链接 - How debug Universal links 如何设置通用链接 - How to setup universal linking 如何将通用链接连接到 UIViewController? - How to connect Universal Links to UIViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM