Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift & Facebook login : my UIViewController does not conform to FBSDKLoginButtonDelegate

Even if Facebook tutorial says that there is no need of an header, I had problems while adding FBSDK functionalities via simple importing framework in the .swift files.

So, I followed this tutorial: http://www.brianjcoleman.com/tutorial-how-to-use-login-in-facebook-sdk-4-0-for-swift/ using swift 6.3 SDK Facebook 4.1 SDK

but I got two issues

FBLoginViewViewController does not conform to FBSDKLoginButtonDelegate

Cannot assign a value of type 'FBLoginViewViewController' to a value
of type 'FBSDKLoginButtonDelegate!'

so I can't login, I tried removing the import statements, no change, I also implemented every statement in the appDelegate, everything should be ok, but still...

thanks in advance

 //
//  FBLoginViewViewController.swift
//  SocialFBLogin
//
//  Created by XXXX XXXX on 18/05/15.
//  Copyright (c) 2015 XXXX XXXX. All rights reserved.
//

import UIKit

import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit



//@objc(FBLoginViewViewController)
class FBLoginViewViewController: UIViewController, FBSDKLoginButtonDelegate     {



@IBOutlet weak var FBLoginPicture: UIImageView!
@IBOutlet weak var FBLoginNameLabel: UILabel!

override func viewDidLoad() {


    super.viewDidLoad()
    // Do any additional setup after loading the view.

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        // User is already logged in, do work such as go to next view controller.
    }
    else
    {
        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email", "user_friends"]
        loginView.delegate = self
    }

    // Facebook Delegate Methods

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        println("User Logged In")

        if ((error) != nil)
        {
            // Process error
        }
        else if result.isCancelled {
            // Handle cancellations
        }
        else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if result.grantedPermissions.contains("email")
            {
                // Do work
            }
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        println("User Logged Out")
    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
like image 935
biggreentree Avatar asked May 18 '15 22:05

biggreentree


3 Answers

Sir, I just solved this issue! You gotta conform your viewController to FBSDKLoginButtonDelegate. first, you add it to your protocols like this:

class MainView: UIViewController, UITableViewDataSource, UITableViewDelegate, FBSDKLoginButtonDelegate 
{
    //Your Code
}

To conform to this protocol you have to implement 2 following methods:

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    println("User Logged In")
    if ((error) != nil)
    {
        // Process error
    }
    else if result.isCancelled {
        // Handle cancellations
    }
    else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.grantedPermissions.contains("email")
        {
            // Do work
        }
    }
}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    println("User Logged Out")
}

You just copy this code and you'll be fine!

Updated to Swift 3

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    print("User Logged In")
    if ((error) != nil)
    {
        // Process error
    }
    else if result.isCancelled {
        // Handle cancellations
    }
    else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.grantedPermissions.contains("public_profile")
        {
            // Do work
        }
    }
}

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("User Logged Out")
}
like image 196
Raphael Souza Avatar answered Nov 20 '22 15:11

Raphael Souza


By just implementing these methods it worked

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if (error == nil) {

        } else {
            println(error.localizedDescription)
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        println("User logged out...")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if (FBSDKAccessToken.currentAccessToken() != nil) {
            println("User logged in...")
        } else {
            println("User not logged in...")
        }
        let fbLoginButton: FBSDKLoginButton! = FBSDKLoginButton()
        self.view.addSubview(fbLoginButton)
        fbLoginButton.delegate = self
        fbLoginButton.center = self.view.center
        fbLoginButton.readPermissions = ["public_profile", "email", "user_friends"]
    }
like image 3
adubey Avatar answered Nov 20 '22 17:11

adubey


Move "func loginButton(..." and "func loginButtonDidLogOut(..." out of "override func viewDidLoad() {...}".

like image 1
Hanage Avatar answered Nov 20 '22 16:11

Hanage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!