如何使用通配符域实现Android App链接? [英] How to implement Android App Links with wildcard domains?
问题描述
Android有有关如何实施应用链接的指南。也就是说,如果我的应用声明它处理某些网络链接,我尝试在任何其他应用程序中打开此链接,系统拦截这个并将用户直接带到我的应用程序,而不是浏览器,以便我可以直接在我的应用程序中显示相关内容。非常方便。
Android has a guide about how to implement app links. That is, if my app declares that it handles certain web links, and I try to open this link in any other app, the system intercepts this and takes the user straight to my app, instead of the browser, so that I can show relevant content straight in my app. Very handy.
指南中缺少的是两件事:
What I am missing in the guide is two things:
-
如何使用通配符域实现应用程序链接。我希望我的应用程序能够处理* .example.com的链接,即example.com的所有
子域
的链接(test.example.com,something.example。 com等);
How to implement app links with wildcard domains. I would like my app to handle links to *.example.com, that is, all links to
subdomains
of example.com (test.example.com, something.example.com etc);
如何仅对我网站上的特定路径实施应用链接。例如,我想拦截test.example.com/something,而不是test.example.com/other。第一个应该来到我的应用程序,另一个应该到我的浏览器;
How to implement app links only to specific paths on my site. For example I want to intercept test.example.com/something, but not test.example.com/other. The first one should come to my app, the other one to my browser;
相应的iOS指南显示iOS处理这两种情况(虽然通配符部分不清楚docs和我必须向Apple支持部门澄清,你需要将关联文件放在根域,而不是子域。)
The corresponding iOS guide shows that iOS handles both of these scenarios (though the wildcard part was unclear from the docs and I had to clarify with Apple Support that you need to place the association file in the root domain, not a subdomain).
Android App Links可以处理通配符域和只有一部分路径?
Can Android App Links handle wildcard domains and only a subset of paths?
推荐答案
- 更新:现在你可以处理通配符域,使用数字资产链接
- Update: now you can handle wildcard domains, using the Digital Asset Links
这里记录了整个过程: https://developer.android.com/training/app-links/verify-site-associations
The whole process is documented here: https://developer.android.com/training/app-links/verify-site-associations
总结一下,现在您可以在主机标记中使用通配符,并且必须将名为 assetlinks.json 的json文件上传到根域上的 /。众所周知的文件夹/路由。
To sum it up, now you can use a wildcard in the host tag, and you must upload a json file called assetlinks.json to the /.well-known folder/route on your root domain.
或者,如果使用通配符(例如* .example.com)声明主机名,则必须在根主机名(example.com)上发布assetlinks.json文件
Alternatively, if you declare your hostname with a wildcard (such as *.example.com), you must publish your assetlinks.json file at the root hostname (example.com)
您还需要将属性 android:autoVerify =true添加到 intent-filter 标签。
You will also need to add the attribute android:autoVerify="true" to your intent-filter tag.
以下是Android端的整个示例:
Here's the whole example on the Android side:
<application>
<activity android:name="MainActivity">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="*.example.com" />
</intent-filter>
</activity>
</application>
以下是2016年的回答:
不幸的是,似乎Android 无法处理通配符域。
Here's the previous answer from 2016:
Unfortunately it seems that Android cannot handle wildcard domains.
如果您查看数据标记的API指南( https://developer.android.com/guide/topics/manifest/data-element.html ),你可以看到他们提到该通配符可用于pathPattern和mimeType,但不适用于主机。
If you look at the API guide for the data tag (https://developer.android.com/guide/topics/manifest/data-element.html), you can see they mention that wildcard is available for pathPattern and mimeType, but not for host.
正如CommonsWare在另一篇关于该主题的帖子中解释的那样(https://stackoverflow.com/a/34068591/4160079 ,
The thing is that, as explained by CommonsWare in an other post on the subject (https://stackoverflow.com/a/34068591/4160079),
在安装时检查域名,除了通过发布带有新清单的应用程序的新版本之外,无法添加新域名。
the domains are checked at install time, and there is no means to add new domains except by shipping a new edition of the app with a new manifest.
因此,您必须手动列出所有可用的子域名,并在每个子域名启动时更新应用程序。
So you will have to manually list all the subdomains available, and update the app whenever a new subdomain is launched.
这里是如何申报多个子域名:
Here's how you declare multiple subdomains:
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:host="subdomain1.example.com" />
<data android:host="subdomain2.example.com" />
<data android:host="subdomain3.example.com" />
</intent-filter>
</activity>
- 是的,您只能处理一部分路径
- Yes, you can handle only a subset of paths
这是一样的想法,只需使用路径属性列出您想要的路径(再次参见上面的 data 标签API指南)。
It is the same idea, just list the paths you want using the path attribute (again, see data tag API guide above).
如果您使用查询字符串或路径参数,请使用 pathPrefix 。
If you use query strings or path params, prefer using pathPrefix.
如有必要,您可以在此处使用通配符,选择 pathPattern 代替。
If necessary, you can use wildcards here, by choosing pathPattern instead.
URI的路径部分必须以/开头。 path属性指定与Intent对象中的完整路径匹配的完整路径。 pathPrefix属性指定仅与Intent对象中路径的初始部分匹配的部分路径。 pathPattern属性指定与Intent对象中的完整路径匹配的完整路径,但它可以包含以下通配符:
星号('')匹配0到多次出现的序列紧接在前的角色。
星号后跟一个星号(。)匹配任意0到多个字符的序列。
The path part of a URI which must begin with a /. The path attribute specifies a complete path that is matched against the complete path in an Intent object. The pathPrefix attribute specifies a partial path that is matched against only the initial part of the path in the Intent object. The pathPattern attribute specifies a complete path that is matched against the complete path in the Intent object, but it can contain the following wildcards: An asterisk ('') matches a sequence of 0 to many occurrences of the immediately preceding character. A period followed by an asterisk (".") matches any sequence of 0 to many characters.
以下是一些示例:
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:host="subdomain1.example.com" />
<data android:host="subdomain2.example.com" />
<data android:host="subdomain3.example.com" />
<data android:path="/path1" /> <!-- matches /path1 only -->
<data android:pathPrefix="/path2" /> <!-- matches /path2, /path2/something or also /path2?key=value etc... -->
<data android:pathPattern="/wild.*" /> <!-- matches /wild, /wild3, /wilderness etc... -->
</intent-filter>
</activity>
这篇关于如何使用通配符域实现Android App链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!