Swift 5手柄通用链接

wh6knrhe  于 2023-01-29  发布在  Swift
关注(0)|答案(1)|浏览(138)

我已经在我的应用程序中实现了Universal Links,当点击电子邮件、消息等中的特定链接时,它会打开。
现在我正试图处理这些事件,但在这一点上没有成功。
你知道我做错了什么吗?
以下是我目前在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
    
}

另外,我在xCode 14上使用Swift 5

gudnpqoy

gudnpqoy1#

我遇到了同样的问题,问题是你有一个SceneDelegate,因此没有调用AppDelegate方法。
在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)")
}

从那里,做任何你需要处理的链接。
希望有帮助:)

相关问题