SwiftUI新应用生命周期如何连接Facebook SDK

9rnv2umw  于 2022-12-17  发布在  Swift
关注(0)|答案(2)|浏览(154)

我正在创建带有Firebase和Facebook身份验证的SwiftUI应用程序,我在基本设置方面遇到了问题。在Facebook上为开发人员提供了如何设置应用程序和替换整个AppDelegate类的教程:

import UIKit
import FBSDKCoreKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
          
        ApplicationDelegate.shared.application(
            application,
            didFinishLaunchingWithOptions: launchOptions
        )

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

        ApplicationDelegate.shared.application(
            app,
            open: url,
            sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
            annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        )

    }  

}

我的问题是,当我使用新的生命周期和App struct时,如何做同样的事情:

import SwiftUI
import Firebase
import FBSDKCoreKit

@main
struct SplitApp: App {
    
    init() {
        FirebaseApp.configure()
      }
    
    var body: some Scene {
        WindowGroup {
            WelcomeView()
        }
    }
}

我必须使用旧的生命周期吗?谢谢!

pgvzfuti

pgvzfuti1#

将UIApplicationDelegateAdaptor添加到SwiftUI应用程序

import SwiftUI
import Firebase

@main
struct SplitApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            WelcomeView()
        }
    }
    
    
    class AppDelegate: NSObject, UIApplicationDelegate {
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
            
            ApplicationDelegate.shared.application(
                application,
                didFinishLaunchingWithOptions: launchOptions
            )
                
            FirebaseApp.configure()
            
            
            return true
        }
        
        
        func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            
            ApplicationDelegate.shared.application(
                app,
                open: url,
                sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                annotation: options[UIApplication.OpenURLOptionsKey.annotation]
            )
        }
    }
}
stszievb

stszievb2#

你可以在你的内容视图中使用onappearn。

var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear(){
                    ApplicationDelegate.shared.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)
                }
        }

相关问题