简体   繁体   English

Swift 5手柄通用链接

[英]Swift 5 handle Universal Links

I've implemented Universal Links in my app and it opens when specific links are clicked in emails, messages etc.我在我的应用程序中实现了通用链接,当在电子邮件、消息等中单击特定链接时它会打开。

Now I'm trying to handle this events but with no success at this point.现在我正在尝试处理这些事件,但目前没有成功。

Any ideas what am I doing wrong?任何想法我做错了什么?

Here's a part of what I've tried so far in my AppDelegate :这是我到目前为止在我的AppDelegate中尝试过的部分内容:

import Firebase
import FirebaseMessaging
import UserNotificationsUI
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, NSUserActivityDelegate {
    
    // ... register for notifications

   func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool 
   {
       print("Test 1") // Doesn't print anything
       return true
   }

   func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool
   {
       print("Test 2") // Doesn't print anything
       return true
   }
        
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    
    // ... handle notifications
    
}

PS I'm using Swift 5 on xCode 14 PS 我在 xCode 14 上使用 Swift 5

I had the same problem, the problem is that you have a SceneDelegate, because of this the AppDelegate methods are not called.我有同样的问题,问题是你有一个 SceneDelegate,因此没有调用 AppDelegate 方法。

Implement the following methods in your SceneDelegate to handle Universal Links when the app is already running and when it is not launched yet:在您的 SceneDelegate 中实现以下方法,以在应用程序已经运行和尚未启动时处理通用链接:

//This method is called when app is NOT running in the background.
//Easy to test with fatalError()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    
    if let userActivity = connectionOptions.userActivities.first {
        debugPrint("userActivity: \(userActivity.webpageURL)")
        fatalError()
    }
}

//This method is called when app is running in the background.
//Easy to test with debugPrint
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    
    debugPrint("userActivity: \(userActivity.webpageURL)")
}

From there, do whatever you need to handle the links.从那里,做任何你需要处理链接的事情。

Hope it helps:)希望能帮助到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM