我正在尝试为我的 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")
}
}
我可以看到我的登录视图上的按钮,我甚至可以登录/注销(按钮上的文本分别从"登录"更改为"注销"......但是来自"loginButton"和"loginButtonDidLogOut"的代码永远不会被执行。我应该在我的调试控制台中看到一些消息,但只是没有调用此代码。
我做错了什么吗?似乎我忘了设置一些委托,或者我应该在其他地方调用 Auth.auth(( 而不是 loginButton(( 函数。
你的AppDelegate
符合LoginButtonDelegate
,所以你只需要在makeUIView
内设置委托:
func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton {
let button = FBLoginButton()
button.delegate = UIApplication.shared.delegate as! AppDelegate
return button
}