与 AppDelegate 和 SceneDelegate 的深层链接

She*_*ts 10 deep-linking ios appdelegate swift uiscenedelegate

我正在尝试实现深层链接来导航到应用程序上的帖子,这是一个较旧的项目,所以我必须添加该类SceneDelegate。仅当应用程序处于活动状态或在后台时,深层链接实现才有效。如果应用程序尚未加载,深层链接将不起作用。我看过很多关于此的帖子和教程,但没有找到原因,有人遇到过类似的问题吗?

在类中AppDelegate,我添加了实现来处理以下函数的链接:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {}
Run Code Online (Sandbox Code Playgroud)

SceneDelegate我实现处理以下函数中的链接:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {}
Run Code Online (Sandbox Code Playgroud)

这些函数的实现如下所示:

let navigator = Navigator()
navigator.getDesination(for: url)


func getDesination(for url: URL){
    let destination = Destination(for: url)
    let ivc = InstantiateViewController()
    switch destination {
    case .post(let postID):
        ivc.openPostVC(id: postID, showComment: true, commentID: nil)
    case .user(let userID):
        ivc.openProfileVC(userID: userID)
    default:
        break
    }
}

enum Destination {
    case post(Int)
        
    case user(Int)
            
    case feed(String)
            
    case store
            
    case safari
            
    init(for url: URL){
        if(url.pathComponents[1] == "p"){
            self = .post(Int(url.pathComponents[2])!)
        } else if(url.pathComponents[1] == "user") {
            self = .user(Int(url.pathComponents[2])!)
        } else if(url.pathComponents[1] == "store") {
            self = .store
        } else if(url.pathComponents[1] == "s") {
            self = .feed(url.pathComponents[2])
        } else {
            self = .safari
        }
    }
}


func openProfileVC(userID: Int){
    let service = UserPool.shared.request(for: userID)
                    
    let storyboard = UIStoryboard(name: "Profile", bundle: nil)
    let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileView") as! ProfileViewController
    profileVC.userService = service
    profileVC.shouldNavigateToHome = true
    profileVC.shouldNavigateToHomeAction = {
        self.loadMainStoryboard()
    }
        
    let navigationVC = UINavigationController(rootViewController: profileVC)
    navigationVC.view.backgroundColor = .white
    
    if #available(iOS 13.0, *) {
        guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {return}
        sceneDelegate.window?.rootViewController = navigationVC
        sceneDelegate.window?.makeKeyAndVisible()
    } else {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        appDelegate.window?.rootViewController = navigationVC
        appDelegate.window?.makeKeyAndVisible()
    }
}
Run Code Online (Sandbox Code Playgroud)

网站 app-site-association 文件如下所示,并在 Xcode 中添加了关联域:

{"applinks":{"apps":[],"details":[{"appID":"{my ID}","paths":["*"]}]},"webcredentials":{"apps":["{my ID}"]}}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 20

在 iOS 13 及更高版本中,使用场景委托,您的应用程序可以在启动时观察传入的通用链接事件,如下所示:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let url = connectionOptions.userActivities.first?.webpageURL {
       // ... or might have to cycle thru multiple activities
    }
}
Run Code Online (Sandbox Code Playgroud)

如果应用程序已经在运行,您可以使用以下命令:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let url = userActivity?.webpageURL {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

(我有一个非常简单的可下载演示应用程序,它证明这确实有效。我不明白它不起作用的说法;也许问题在于无法理解如何测试。)