如何使用通配符域实现Android App链接?

时间:2016-09-21 12:43:40

标签: android ios applinks

Android有a guide about how to implement app links.也就是说,如果我的应用声明它处理某些网络链接,并且我尝试在任何其他应用中打开此链接,系统会拦截此操作并将用户直接转到我的应用,浏览器,以便我可以直接在我的应用程序中显示相关内容。非常方便。

指南中缺少的是两件事:

  1. 如何使用通配符域实现应用程序链接。我希望我的应用程序能够处理* .example.com的链接,即所有指向example.com的subdomains的链接(test.example.com,something.example.com等);

    < / LI>
  2. 如何仅对我网站上的特定路径实施应用链接。例如,我想拦截test.example.com/something,但不是test.example.com/other。第一个应该来到我的应用程序,另一个应该到我的浏览器;

  3. The corresponding iOS guide显示iOS处理这两种情况(尽管文档中不清楚通配符部分,我不得不向Apple支持部门澄清,您需要将关联文件放在根域中,而不是子域)。

    Android应用链接可以处理通配符域,只能处理路径的子集吗?

3 个答案:

答案 0 :(得分:10)

  1. 更新:现在您可以使用数字资产链接
  2. 处理通配域

    aurilio explained it in his newer answer

    整个过程记录在此处:https://developer.android.com/training/app-links/verify-site-associations

    总结一下,现在你可以在主机标签中使用通配符,而你必须上传一个名为 > assetlinks.json 到 root 域上 /。众所周知的文件夹/路由。

      

    或者,如果使用通配符(例如* .example.com)声明主机名,则必须在根主机名(example.com)上发布assetlinks.json文件

    您还需要将属性 android:autoVerify =“true”添加到 intent-filter 标记。

    以下是Android方面的整个示例:

    <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 似乎无法处理通配域

    如果您查看 data 标记(https://developer.android.com/guide/topics/manifest/data-element.html)的API指南,您会看到他们提到通配符可用于pathPattern和mimeType,但不适用于主机。

    事情是,正如CommonsWare在关于这个主题的另一篇文章(https://stackoverflow.com/a/34068591/4160079)中所解释的那样,

      

    在安装时检查域名,除了通过发布新版本的应用程序以外,无法添加新域名。

    因此,您必须手动列出所有可用的子域,并在启动新子域时更新应用。

    以下是您声明多个子域的方式:

     <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>
    

    1. 是的,您只能处理路径的子集
    2. 这是一样的想法,只需使用路径属性列出您想要的路径(再次参见上面的 data 标签API指南)。

      如果您使用查询字符串或路径参数,请使用 pathPrefix

      如有必要,您可以在此处使用通配符,方法是选择 pathPattern

        

      URI的路径部分,必须以/开头。 path属性指定与Intent对象中的完整路径匹配的完整路径。 pathPrefix属性指定仅与Intent对象中路径的初始部分匹配的部分路径。 pathPattern属性指定与Intent对象中的完整路径匹配的完整路径,但它可以包含以下通配符:   星号('')匹配前一个字符的0到多次出现的序列。   星号后跟一个星号(“。”)匹配任意0到多个字符的序列。

      以下是一些例子:

       <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>
      

答案 1 :(得分:3)

Android无法处理通配符域(根据他们今天的文档),但是,这将回答有关包含和排除某些 / paths 的查询。

为URL实现深层链接,例如 -

http://example.com/gizmos?1234, 
http://example.com/gizmos/1234,
http://example.com/gizmos/toys/1234,
etc.

你的XML应该是这样的 -

<activity android:name="com.example.android.GizmosActivity" android:label="@string/title_gizmos" > 

    <intent-filter android:label="@string/filter_title_viewgizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" /> <!-- Accepts URIs that begin with "example://gizmos” --> 

        <data android:scheme="example" android:host="gizmos" />
    </intent-filter>
    <intent-filter android:label="@string/filter_title_viewgizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "http://example.com/gizmos” -->
        <data android:scheme="http" android:host="example.com" android:pathPrefix="/gizmos" /> 
    </intent-filter> 
</activity>

现在考虑到您能够实现这一目标,以下是限制对应用内容部分内容的访问的方式 -

<?xml version="1.0" encoding="utf-8"?>
<search-engine xmlns:android="http://schemas.android.com/apk/res/android">
    <noindex uri="http://example.com/gizmos/hidden_uri"/>
    <noindex uriPrefix="http://example.com/gizmos/hidden_prefix"/>
    <noindex uri="gizmos://hidden_path"/>
    <noindex uriPrefix="gizmos://hidden_prefix"/>
</search-engine>

和Manifest部分 -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.Gizmos">
    <application> 
        <activity android:name="com.example.android.GizmosActivity" android:label="@string/title_gizmos" >
            <intent-filter android:label="@string/filter_title_viewgizmos">
            <action android:name="android.intent.action.VIEW"/> ... 
        </activity> 
            <meta-data android:name="search-engine" android:resource="@xml/noindex"/> 
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

有关此示例的更多详细信息和说明,您可以查看 -

Android Deep Linking

希望它有所帮助,Happy Coding

答案 2 :(得分:0)

引自:https://developer.android.com/training/app-links/verify-site-associations.html

或者,如果使用通配符(例如* .example.com)声明主机名,则必须在根主机名(example.com)上发布assetlinks.json文件。例如,具有以下intent过滤器的应用程序将通过example.com的任何子名称验证(例如foo.example.com),只要assetlink.json文件发布在https:/ / example.com/ .well- known / assetlinks.json:

    2018-04-18 14:15:40,003 INFO org.apache.hadoop.hdfs.StateChange: STATE* Network topology has 0 racks and 0 datanodes

    2018-04-18 14:15:40,006 INFO org.apache.hadoop.hdfs.StateChange: STATE* UnderReplicatedBlocks has 0 blocks

    2018-04-18 14:15:40,014 INFO org.apache.hadoop.hdfs.server.blockmanagement.BlockManager: Total number of blocks            = 0

    2018-04-18 14:15:40,014 INFO org.apache.hadoop.hdfs.server.blockmanagement.BlockManager: Number of invalid blocks          = 0

    2018-04-18 14:15:40,014 INFO org.apache.hadoop.hdfs.server.blockmanagement.BlockManager: Number of under-replicated blocks = 0

    2018-04-18 14:15:40,014 INFO org.apache.hadoop.hdfs.server.blockmanagement.BlockManager: Number of  over-replicated blocks = 0

    2018-04-18 14:15:40,014 INFO org.apache.hadoop.hdfs.server.blockmanagement.BlockManager: Number of blocks being written    = 0

    2018-04-18 14:15:40,014 INFO org.apache.hadoop.hdfs.StateChange: STATE* Replication Queue initialization scan for invalid, over- and under-replicated blocks completed in 11 msec

    2018-04-18 14:15:40,028 INFO org.apache.hadoop.ipc.Server: IPC Server Responder: starting

    2018-04-18 14:15:40,028 INFO org.apache.hadoop.ipc.Server: IPC Server listener on 9000: starting

    2018-04-18 14:15:40,029 INFO org.apache.hadoop.hdfs.server.namenode.NameNode: NameNode RPC up at: localhost/127.0.0.1:9000

    2018-04-18 14:15:40,031 INFO org.apache.hadoop.hdfs.server.namenode.FSNamesystem: Starting services required for active state

    2018-04-18 14:15:40,031 INFO org.apache.hadoop.hdfs.server.namenode.FSDirectory: Initializing quota with 4 thread(s)

    2018-04-18 14:15:40,033 INFO org.apache.hadoop.hdfs.server.namenode.FSDirectory: Quota initialization completed in 2 milliseconds name space=1 storage space=0 storage types=RAM_DISK=0, SSD=0, DISK=0, ARCHIVE=0, PROVIDED=0 2018-04-18 14:15:40,037 INFO org.apache.hadoop.hdfs.server.blockmanagement.CacheReplicationMonitor: Starting CacheReplicationMonitor with interval 30000 milliseconds

> 2018-04-18 14:15:40,232 ERROR
> org.apache.hadoop.hdfs.server.namenode.NameNode: RECEIVED SIGNAL 15:
> SIGTERM
> 
> 2018-04-18 14:15:40,236 ERROR
> org.apache.hadoop.hdfs.server.namenode.NameNode: RECEIVED SIGNAL 1:
> SIGHUP
> 
> 2018-04-18 14:15:40,236 INFO
> org.apache.hadoop.hdfs.server.namenode.NameNode: SHUTDOWN_MSG: 
> /************************************************************
> SHUTDOWN_MSG: Shutting down NameNode at c0315/127.0.1.1