URL Scheme、Universal Links两种方式都可以,但是Universal Links方式更稳定。iOS13+以上环境URL Scheme方式可能被系统拦截。

URL Scheme方式

配置Info.plist

打开App工程的Info.plist ,添加URL types

CFBundleURLTypes CFBundleURLName 一个名字,例如myapp CFBundleURLSchemes 自定义scheme,例如myapptest

AppDelegate回调

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    //解析url,路由到App内对应页面 myapptest://xxx/yyy?zzz=123
    print(url)
    return true
}

SceneDelegate回调(iOS13+)

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let context = URLContexts.first else { return }
    let url = context.url
    //解析url,路由到App内对应页面 myapptest://xxx/yyy?zzz=123
    print("SceneDelegate 收到scheme url:\(url)")
}

外部唤醒

以 Safari 为例

<!doctype html>
<html> 
    <head> 
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <title>唤醒测试</title>
    </head>
    <body> 
        <a href="myapptest://xxx/yyy?zzz=123">通过URL Scheme唤醒</a>
    </body>
</html>

Universal Links方式

请先阅读配置Apple-Universal-Links

AppDelegate回调

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
          let url = userActivity.webpageURL else {
        return false
    }
    //拿到https链接,解析参数做内部路由
    print("universal link url:\(url)")
    return true
}

SceneDelegate回调(iOS13+)

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    // Universal Links 会来到这里
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
          let webpageURL = userActivity.webpageURL else {
        return
    }
     //拿到https链接,解析参数做内部路由
    print("SceneDelegate 收到UL: \(webpageURL)")
}

外部唤醒

以 Safari 为例

<!doctype html>
<html> 
    <head> 
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <title>唤醒测试</title>
    </head>
    <body> 
        <a href="https://App工程里配置的Associated Domains//xxx/yyy?zzz=123">通过Universal Links唤醒</a>
    </body>
</html>