gpt4 book ai didi

swiftui - Facebook 登录 + SwiftUI : loginButton & loginButtonDidLogOut are NOT called after login/logout

转载 作者:行者123 更新时间:2023-12-05 07:06:34 24 4
gpt4 key购买 nike

我正在尝试为我的 SwiftUI 应用程序实现 Facebook 登录,这是我的 AppDelegate.swift 文件:

import UIKit
import FirebaseCore
import FirebaseAuth
import FBSDKLoginKit
import FBSDKCoreKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, LoginButtonDelegate {

static var orientationLock = UIInterfaceOrientationMask.portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()

return ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any])
-> Bool {

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

return handled

}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

return ApplicationDelegate.shared.application(
application,
open: url
)
}

func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
print("FB Login Button function")
if let error = error {
print("ERROR1: \(error.localizedDescription)")
return
}

let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)

Auth.auth().signIn(with: credential) { (res, err) in
if err != nil {
print("ERROR2: \(String(describing: err?.localizedDescription))")
return
}

print("email: \(String(describing: res?.user.email))")
print("name: \(String(describing: res?.user.displayName))")
}

}

func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
print("Did logout")
}

好的,这是我的 SwiftUI 代码:

import SwiftUI
import FirebaseCore
import FBSDKLoginKit

struct LoginView: View {
@EnvironmentObject var thisSession: CurrentSession
@ObservedObject var mainData = MainViewModel()

var body: some View {
VStack {
facebook().frame(width: 240, height: 50)
}
}
}

struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView().environmentObject(CurrentSession())
}
}

struct facebook : UIViewRepresentable {

func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton {

let button = FBLoginButton()
//button.delegate = self
return button
}
func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<facebook>) {
print("FBButton updateUIView called")
}
}

我可以在我的 LoginView 上看到按钮,我什至可以登录/注销(按钮上的文本分别从“登录”更改为“注销”... 但是来自“loginButton”和“loginButtonDidLogOut”永远不会执行。我应该在我的调试控制台中看到一些消息,但没有调用这段代码。

我做错了什么吗?好像我忘了设置一些委托(delegate)或者我应该在其他地方调用 Auth.auth() 而不是 loginButton() 函数..

最佳答案

您的AppDelegate 符合LoginButtonDelegate,因此您只需要在makeUIView 中设置委托(delegate):

func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton {
let button = FBLoginButton()
button.delegate = UIApplication.shared.delegate as! AppDelegate
return button
}

关于swiftui - Facebook 登录 + SwiftUI : loginButton & loginButtonDidLogOut are NOT called after login/logout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62428911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com