美文网首页
Universal Links

Universal Links

作者: tonytong | 来源:发表于2020-03-09 16:27 被阅读0次

    简介

    大家好!我是Tony,一个热爱技术,希望运用技术改变生活的的追梦男孩。本文是对Universal Link的总结,参考资料来自苹果开发文档(command+shift+0可以打开)。内容主要包含三部分:

    1. 开启Universal Links
    2. 处理Universal Links
    3. 自定义URLs

    Universal Links的能力

    让其他运用程序或者websites可以打开你的app,完成用户的某项需求。

    开启Universal Links

    开启的步骤如下:

    设置app的Associated Domains

    我的Xcode版本是11.1,在工程的target->signing&Capabilities->Capability,设置格式:
    以添加带有webcredentials:前缀的占位符域。 将占位符替换为您网站的域,并保留前缀。


    image.png
    添加apple-app-site-association文件到websites
    1. 新建文件apple-app-site-association(无后缀扩展名),添加内容如下:
    {
       "webcredentials": {
           "apps": [    "D3KQX62K1A.com.example.DemoApp",
                        "D3KQX62K1A.com.example.DemoAdminApp" ]
        }
    }
    

    apps字段中各个App的格式如下<Team Identifier>.<Bundle Identifier>
    将此文件放置在您网站的.well-known目录中,或直接放置在其根目录中。 如果使用.well-known目录,则文件的URL应与以下格式匹配:
    https://<fully qualified domain>/.well-known/apple-app-site-association。

    注意事项

    • 您必须使用带有有效证书的https://且不使用任何重定向来托管文件。
    • 在iOS 9.3.1和更高版本中,文件不得大于128 KB(未压缩)。 如果您的应用程序在iOS 8中运行,则该文件必须具有MIME类型application / pkcs7-mime,并且必须是由有效TLS证书进行CMS签名的文件。

    验证Apple App网站关联文件

    1. 如果发生以下情况,验证可能会失败,并且关联将被拒绝:
      JSON文件无效或不包含应用程序标识符。
    • 服务器返回300-499码。 这包括重定向。
    • 如果服务器返回500-599码,则系统认为该文件暂时不可用,然后重试。 默认情况下,系统每三小时重试一次,最多八次。
    1. 应用程序成功与域关联后,它将保持关联状态,直到从设备中删除该应用程序为止。 在开发过程中,每次更新关联文件以立即查看所做的更改时,请从测试设备中删除您的应用程序。

    自定义URLs

    尽管自定义URL方案是可接受的深层链接形式,但强烈建议将Universal Link作为最佳实践。
    自定义URL scheme需要完成如下步骤:

    1. 定义应用程序URL的格式
    2. 注册scheme,以便系统将适当的URL定向到您的应用程序。
    3. 处理您的应用程序收到的URL。
    定义应用程序URL的格式

    eg: myphotoapp:albumname?name=”albumname”

    注册scheme
    image.png
    处理您的应用程序收到的URL
    func application(_ application: UIApplication,
                     open url: URL,
                     options: [UIApplicationOpenURLOptionsKey : Any] = [:] ) -> Bool {
        
        // Determine who sent the URL.
        let sendingAppID = options[.sourceApplication]
        print("source application = \(sendingAppID ?? "Unknown")")
        
        // Process the URL.
        guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
            let albumPath = components.path,
            let params = components.queryItems else {
                print("Invalid URL or album path missing")
                return false
        }
        
        if let photoIndex = params.first(where: { $0.name == "index" })?.value {
            print("albumPath = \(albumPath)")
            print("photoIndex = \(photoIndex)")
            return true
        } else {
            print("Photo index missing")
            return false
        }
    }
    

    相关文章

      网友评论

          本文标题:Universal Links

          本文链接:https://www.haomeiwen.com/subject/oyspdhtx.html