简体   繁体   中英

Facebook login failed in iOS swift 4

I have implemented Facebook login into iOS app but I am getting this error:

在此输入图像描述

I have found a couple of solution here which are:

1) use the default facebook button.

2) provided a contact email address in the developer console.

3) made the app live in the developer console.

It is working fine in development mode but this issue starting to occur after I have published app into the app store.

I don't know what could be the reason for this. Can anyone help me?

EDIT

plist file Code:-

在此输入图像描述

CODE

For creating button:-

if FBSDKAccessToken.current() != nil
        {
            let manager = FBSDKLoginManager()
            manager.logOut()
        }

let loginButton = FBSDKLoginButton()
    // Optional: Place the button in the center of your view.
    loginButton.delegate = self
    loginButton.frame = CGRect(x: (self.view.frame.size.width / 2) - (loginButton.frame.size.width / 2), y: self.view_facebook.frame.origin.y, width: loginButton.frame.size.width, height: loginButton.frame.size.height + 12)
    loginButton.readPermissions = ["public_profile", "email"]
    self.view.addSubview(loginButton)

facebook Login Code:-

extension LogInAndSignUpViewController : FBSDKLoginButtonDelegate
{
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {

        if error != nil {

        }else if result.isCancelled {
            print("cancel")
        }else {
            //  self.fbRsponseLable.text = "User  login Successfully"

            print(result!)
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if fbloginresult.grantedPermissions != nil {
                if(fbloginresult.grantedPermissions.contains("email"))
                {
                    self.serviceCalledForFetchUserInfoFromFacebook()

                }
                else
                {
                    self.serviceCalledForFetchUserInfoFromFacebook()
                }
            }
        }

    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Did logout via LoginButton")
    }
}

    func serviceCalledForFetchUserInfoFromFacebook()
        {
            if((FBSDKAccessToken.current()) != nil)
            {
                FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email,name"]).start(completionHandler: { (connection, result, error) -> Void in
                    if (error == nil)
                    {
                        print(result!)
                        let dictName : NSDictionary = (result as? NSDictionary)!
                        if let name = dictName.value(forKey: "name")
                        {
                            print(name)
                            self.F_name = name as! String
                        }

                        if let id = dictName.value(forKey: "id")
                        {
                            print(id)
                            self.F_id = id as! String
                        }

                        if let email = dictName.value(forKey: "email")
                        {
                            print(email)
                            self.F_email = email as! String
                        }
                    }
                    else
                    {
                        print(error!)
                    }
                })
            }
        }

APP DELEGATE CODE

func applicationWillResignActive(_ application: UIApplication) {
        FBSDKAppEvents.activateApp()
    }

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

    }

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


    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication:  options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation:  options[UIApplication.OpenURLOptionsKey.annotation] as? String)

    }

Have you implemented the method in AppDelegate regarding the URL Scheme?

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

        if let appId = FBSDKSettings.appID() { 
            if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host ==  "authorize" {
                //return FBSDKApplicationDelegate.shared.application(app, open: url, options: options)
                return FBSDKApplicationDelegate.sharedInstance()?.application(app, open: url, options: options) ?? false
            }
        }
        return false
    }
func makeLoginUsingFb() {
        let loginManager = LoginManager()
        loginManager.logIn(readPermissions: [.publicProfile,.email], viewController: self) { (response) in
            switch response {
            case .failed(let err):
                print(err.localizedDescription)
            case .cancelled:
                print("User Cancelled Login")
            case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                self.getDataFromFB(accessToken: accessToken)
                print("Logged in!")
                print(grantedPermissions)
                print(declinedPermissions)
                print(accessToken)
            }
        }
    }

    func getDataFromFB(accessToken: AccessToken?) {
        let connection = GraphRequestConnection()
        connection.add(GraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email, first_name, last_name"], accessToken: accessToken, httpMethod: .GET, apiVersion: .defaultVersion)) { httpResponse, result in
            switch result {
            case .success(let response):


                let response = response.dictionaryValue
                let jResponse = JSON(response!).dictionaryValue
                let fName = jResponse["first_name"]?.stringValue
                let lName = jResponse["last_name"]?.stringValue
                let id = jResponse["id"]?.stringValue
                let email = jResponse["email"]?.stringValue

                //==========Save Data To Server And then goto your next HOME ViewController==========

            case .failed(let error):
                print("Graph Request Failed: \(error)")
            }
        }
        connection.start()
    }

App Delegate:

import FacebookCore

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

        SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

        return true
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM