Situation
Hello, I'm using FBSDK for my app login. When Facebook app is uninstalled, I will be redirected to the browser once I click the "continue with Facebook button". All works well, it will call the func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?)
Problem
My problem is that when the Facebook app installed and I continue with the application, it's not calling the func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?)
so I can't continue with the authentication
Code
struct login : UIViewRepresentable {
@EnvironmentObject var manager : HttpAuth
func makeCoordinator() -> login.Coordinator {
return login.Coordinator(manager)
}
func makeUIView(context: UIViewRepresentableContext<login>) -> FBLoginButton {
let button = FBLoginButton()
button.permissions = ["email"]
button.delegate = context.coordinator
return button
}
func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<login>) {
}
class Coordinator : NSObject, LoginButtonDelegate {
var manager: HttpAuth
init(_ HttpAuth: HttpAuth) {
self.manager = HttpAuth
}
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
print("////////////////////////////////////////////////////////////////////////loginButton")
if error != nil{
print("////////////////////////////////////////////////////////////////////////Failed Login")
} else if result!.isCancelled{
print("////////////////////////////////////////////////////////////////////////User cancelled")
} else{
print("////////////////////////////////////////////////////////////////////////Successful login")
let r = GraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: AccessToken.current?.tokenString, version: nil, httpMethod: HTTPMethod(rawValue: "GET"))
r.start(completionHandler: { (data, response, error) in
if error == nil {
guard let response = response else { return }
let messageDictionary : Any = response
let jsonData = try! JSONSerialization.data(withJSONObject: messageDictionary, options: [])
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)!
let hi = Data(jsonString.utf8)
let user = try! JSONDecoder().decode(FBgraph.self, from: hi)
print("FacebookLoginRep: updateUIView \(user.email)")
self.manager.checkDetails(username: user.email, password: nil, facebook_key: user.id)
}
else{
print("FacebookLoginRep: updateUIView error")
}
})
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
}
}
}
Wow, wasted 6 hours. I finally found the solution. I was missing this in the SceneDelegate.
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
let _ = ApplicationDelegate.shared.application(
UIApplication.shared,
open: url,
sourceApplication: nil,
annotation: [UIApplication.OpenURLOptionsKey.annotation])
}