Я пытаюсь реализовать вход в Facebook для своего приложения SwiftUI, и вот мой файл 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")
}
}
I могу видеть кнопку на моем LoginView, и я даже могу войти / выйти (текст на кнопке меняется соответственно с «Вход» на «Выйти» ... Но код из «loginButton» и «loginButtonDidLogOut» никогда не выполнено. Я должен был увидеть некоторые сообщения в моей консоли отладки, но этот код просто не вызывается.
Я что-то делаю не так? Похоже, я забыл установить какого-то делегата или мне нужно вызвать Auth. auth () в другом месте вместо функции loginButton () ..