None of SceneDelegate methods (options connectionOptions & continue userActivity) are triggered when opening the app via Firebase Dynamic Link

huangapple go评论109阅读模式
英文:

None of SceneDelegate methods (options connectionOptions & continue userActivity) are triggered when opening the app via Firebase Dynamic Link

问题

我在我的iOS应用程序中遇到了Firebase Dynamic Links的问题。每当我使用动态链接打开应用程序时,我的AppDelegate或SceneDelegate中的函数似乎都没有被调用,除了指示场景转到后台的提示。

这是我的AppDelegate(还包括SceneDelegate):

import Foundation
import KochavaTracker
import UIKit
import AppTrackingTransparency
import RevenueCat
import Firebase
import FirebaseCore
import FirebaseAuth
import FirebaseDynamicLinks
import UserNotifications

class AppDelegate: NSObject, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
    let gcmMessageIDKey = "gcm.message_id"
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        // 在这里配置和初始化您的应用程序
        
        return true
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
    }
    
    func incrementAppLaunches() {
        // 增加应用程序启动计数的逻辑在这里
    }
    
    func checkAndAskForNotifications() -> Bool {
        // 检查并请求通知权限的逻辑在这里
    }
}

class SceneDelegate: NSObject, UISceneDelegate {

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // 当场景连接到应用程序时的逻辑在这里
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // 当场景进入后台时的逻辑在这里
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // 当场景即将进入前台时的逻辑在这里
    }

    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        // 处理用户活动的逻辑在这里
    }
}

当我点击Firebase Dynamic Link时,应用程序确实会打开,所以我知道方案已正确设置。然而,除了一个指示场景进入后台的消息之外,调试控制台中什么都没有打印出来。

是否有人知道为什么在使用Firebase Dynamic Links时,我的SceneDelegate函数没有被调用(除了sceneWillEnterForeground)?任何帮助将不胜感激。

英文:

I'm facing an issue with Firebase Dynamic Links in my iOS application. Whenever I open the app using a dynamic link, none of the functions in my AppDelegate or SceneDelegate seem to be called, except for the indication that the scene went to the background.

Here's my AppDelegate (containing also SceneDelegate)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import Foundation
import KochavaTracker
import UIKit
import AppTrackingTransparency
import RevenueCat
import Firebase
import FirebaseCore
import FirebaseAuth
import FirebaseDynamicLinks
import UserNotifications

class AppDelegate: NSObject, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
    let gcmMessageIDKey = &quot;gcm.message_id&quot;
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -&gt; Bool {
        
        Purchases.logLevel = .error
        Purchases.configure(
            with: Configuration.Builder(withAPIKey: &quot;@@@@@&quot;)
                .with(appUserID: UIDevice.current.identifierForVendor?.uuidString ?? &quot;none&quot;)
                .build()
        )
        
        Purchases.shared.delegate = self // set up the delegate
        
        let apparence = UITabBarAppearance() // toolbar fixed
        apparence.configureWithOpaqueBackground()
        if #available(iOS 15.0, *) {UITabBar.appearance().scrollEdgeAppearance = apparence}
        
        incrementAppLaunches()
        
        FirebaseApp.configure()
        
        if checkAndAskForNotifications() {
            Messaging.messaging().delegate = self
            
            if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self
                
                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
            } else {
                let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
            
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
        }

        return true
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
    }
    
    func incrementAppLaunches() {
        let defaults = UserDefaults()
        let key = &quot;numberOfAppLaunches&quot;
        var count = defaults.integer(forKey: key)
        count += 1
        defaults.set(count, forKey: key)
    }
    
    func checkAndAskForNotifications() -&gt; Bool {
        let defaults = UserDefaults()
        let key = &quot;numberOfAppLaunches&quot;
        let count = defaults.integer(forKey: key)
        
        if count &gt; 2 {
            // Ask for notification permissions
            //print(&quot;[checkAndAskForNotifications] count = \(count)&quot;)
            return true
        } else {
            //print(&quot;[checkAndAskForNotifications] count = \(count)&quot;)
            return false
        }
    }
}

class SceneDelegate: NSObject, UISceneDelegate {

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        print(&quot;0000&quot;)
        // A new scene was added to the app.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIViewController()
            window.makeKeyAndVisible()
        }
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        print(&quot;5555&quot;)

        // A scene did enter the background.
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        print(&quot;1111&quot;)

        // A scene is about to enter the foreground.
    }


    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        print(&quot;3333&quot;)

        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
            let incomingURL = userActivity.webpageURL,
            let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true),
            let path = components.path else {
                return
        }
        let params = components.queryItems ?? [URLQueryItem]()

        print(&quot;path = \(path)&quot;)
        print(&quot;params = \(params)&quot;)
    }
    
}

<!-- end snippet -->

When I tap on the Firebase Dynamic Link, the app does open, so I know that the scheme is set up correctly. However, nothing is printed in the debug console, except for a message indicating the scene went to the background.

Does anyone know why my SceneDelegate functions aren't being called (except for sceneWillEnterForeground) when using Firebase Dynamic Links? Any help would be greatly appreciated.

答案1

得分: 0

我设法以一种意想不到的方式解决了它。尽管Xcode没有标记任何错误,但我发现简单地将FirebaseDynamicLinks添加到链接的库中可以解决问题。一旦我这样做了,应用程序就开始正常接收来自Firebase动态链接的参数。有点反直觉,但它奏效了!

英文:

I managed to resolve it in an unexpected way. Even though Xcode didn't flag any errors, I found that simply adding FirebaseDynamicLinks to the linked libraries rectified the issue. Once I did that, the app began receiving parameters from Firebase Dynamic Links as expected. It's a bit counterintuitive, but it worked!

huangapple
  • 本文由 发表于 2023年8月11日 00:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定