Facebook SDK 4.7 Login error handling - ios

I added FBSDK 4.7 to my swift app and when it opens the safari view controller to login, and I try to tap "Done" without inputting anything into the email/password fields, the app breaks. It spits out "fatal error: unexpectedly found nil while unwrapping an Optional value"
How do I handle this error?
Current login code:
if let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager(){
fbLoginManager.logInWithReadPermissions(["email"],fromViewController:self,
handler:{(result:FBSDKLoginManagerLoginResult!,error:NSError!) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
print("Logged in successfully")
self.getFBUserData()
//fbLoginManager.logOut()
}
}
else{
print("HELPPPPPPPPPP")
}
})
}

No need to assign the types of variable (result, error) :
Here is working code with Facebook 4.7 SDK | Swift 2 | Xcode 7.
#IBAction func btnFBLoginPressed(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if fbloginresult.grantedPermissions != nil { //Here we have to check that the set contains value or not
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
print(result)
}
})
}
}
We have to check the grantedPermissions's Set is nil or not.
if fbloginresult.grantedPermissions != nil { //Here we have to check that the set contains value or not

Related

SIGABRT Because of the FBSDK

So I noticed that the FBSDK is affecting a lot the storyboard, so I'll present you what I want to do and when I open up the app why it presents the SIGABRT.
Here is want I want to perform:
func switchStoryboard() {
self.performSegue(withIdentifier: "LoginConnection", sender: self)
}
This is how I want it to be performed:
override func viewDidLoad() {
super.viewDidLoad()
if FBSDKAccessToken.current() != nil {
print("User is logged in")
}
else {
self.switchStoryboard()
}
Where the problem is supposed to be located at:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error != nil){
print(error)
return
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, err) in
if (error != nil){
print("Failed to get graph request", err)
return
}
print(result)
}
Well, I have no clue. I tried to debug, everything seems ok but it confuses me a lot, hopes someone here had the same mistake and could help me with this, thanks a lot.
fixed with:
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
print(result)
}
self.switchStoryboard()
print("user is not signed in")
})
}
problem is that the function of self.switchStoryboard() is not working as well as the both print's

Facebook login not dismissing the webpage.

I am using a custom button for my Facebook login. When I click the button a webpage inside the app opens the facebook page with login with app or email/phone. I click open in app, and go through the process. After that process, I get redirected to my app, but the webpage still appears, and is not dismiss.
#IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}

Fetching user's info from FB in swift

I have done FB login from my app in Swift 3. Now, I need to fetch profile picture from Facebook. Plzz any one who could give me the solution in Swift 3 ??
Please try this one.
let login = FBSDKLoginManager()
login.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
if (error != nil) {
self.view.makeToast("Error")
}
else if (result?.isCancelled)! {
self.view.makeToast("Cancel")
}
else {
if (FBSDKAccessToken.current() != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,interested_in,gender,birthday,email,age_range,name,picture.width(480).height(480)"]).start(completionHandler: { (connection, result, error) -> Void in
print(result)
})
}
}
}

Customized Facebook Login Button - After integration

I am trying to customize the Facebook login button after following this guide. Everything is integrated correctly, I can login to Facebook, and logout without a hitch.
Now my next goal is to make the screen look like my mock up, this includes placing the login button correctly on the storyboard, but the only issue is that it doesn't show up on the story board, it just appears when I run the simulation. I looked at other overflow answers, but they weren't really helpful as they were geared towards earlier versions of swift / Xcode, and didn't work from what the comments said. My code is exactly the same as the guide, as this is the first screen that I am trying to implement.
Help would be much appreciated.
If you want to use the custom facebook SDK button you can create any custom button in the storyboard and provide action in the viewcontroller like this
Import at top
import FBSDKCoreKit
import FBSDKLoginKit
Swift 3:
#IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
// if user cancel the login
if (result?.isCancelled)!{
return
}
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}
Older Swift version:
#IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}
If you want default facebook login button you can take UIView and set Class* field of the Custom Class** section in the Identity Inspector, we must set the FBLoginValue
Ref: Default SDK login button tutorial:http://www.appcoda.com/ios-programming-facebook-login-sdk/
Ref: Custom SDK login button for parameters and more info:https://developers.facebook.com/docs/facebook-login/ios
POD INSTALL
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target “ProjectName” do
pod 'FacebookCore'
pod 'FacebookLogin'
pod 'FacebookShare'
end
AppDelegate.swift
import FacebookLogin
import FBSDKLoginKit
import FacebookCore
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
ViewController.swift
import UIKit
import FacebookLogin
import FBSDKLoginKit
import FacebookCore
class ViewController: UIViewController {
override func viewDidLoad() {
if(FBSDKAccessToken.current() == nil){
print("Not logged in ")
}else{
print("Logged in already")
getFacebookUserInfo()
}
}
#IBAction func CustomButton_Click(_ sender: Any) {
getFacebookUserInfo()
}
func getFacebookUserInfo(){
let loginManager = LoginManager()
loginManager.logIn([.publicProfile, .email ], viewController: self) { (result) in
switch result{
case .cancelled:
print("Cancel button click")
case .success:
let params = ["fields" : "id, name, first_name, last_name, picture.type(large), email "]
let graphRequest = FBSDKGraphRequest.init(graphPath: "/me", parameters: params)
let Connection = FBSDKGraphRequestConnection()
Connection.add(graphRequest) { (Connection, result, error) in
let info = result as! [String : AnyObject]
print(info["name"] as! String)
}
Connection.start()
default:
print("??")
}
}
}
}
Info.plist
Info.plist --> right click ->OpenAs -->Source Code --> add < dict > .. < /dict >
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>YOUR APP ID</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>YOUR APP ID</string>
<key>FacebookDisplayName</key>
<string>YOUR APP NAME</string>
#IBAction func buttonLogin(_ sender: Any) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["public_profile","email"], from: self) { (result, error) in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil {
if(fbloginresult.grantedPermissions.contains("email")) {
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
let dict: NSDictionary = result as! NSDictionary
if let token = FBSDKAccessToken.current().tokenString {
print("tocken: \(token)")
let userDefult = UserDefaults.standard
userDefult.setValue(token, forKey: "access_tocken")
userDefult.synchronize()
}
if let user : NSString = dict.object(forKey:"name") as! NSString? {
print("user: \(user)")
}
if let id : NSString = dict.object(forKey:"id") as? NSString {
print("id: \(id)")
}
if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
print("email: \(email)")
}
}
})
}
}
}
}
}
}
Welcome to StackOverflow, Shika! That tutorial shows you how to add the button that Facebook provides as part of their SDK. The tutorial tells you centers the button in the center of the device's screen. Facebook allows you to create your own button, if you'd like to. In my opinion, I like it better, because you can play with it in the storyboard, unlike the button created programmatically in Swift. You can use the Facebook docs here and scroll down to "Custom Login Button". Your code will change, but not by much! If you choose to go this route, make sure to delete the code you copied and pasted to add the button located in viewDidLoad. Also, you can delete the FBSDKLoginButtonDelegate you added at the top of the Swift file.

Facebook Login Swift

I wrote a basic Swift Class for Facebook login handling.
I want to check if the user already authorised the App, because in my case, the user gets asked everytime if he authorises the app - it switches to safari everytime instead of simply logging in. Sometimes the login completely fails - no error message, but also no success.
Here is my code:
class FacebookLogin{
private var data : NSMutableData? = nil
init(){
}
let facebookReadPermissions = ["public_profile", "email", "user_friends"]
func loginToFacebookWithSuccess(successBlock: () -> (), andFailure failureBlock: (NSError?) -> ()) {
if FBSDKAccessToken.currentAccessToken() != nil {
return
}
FBSDKLoginManager().logInWithReadPermissions(self.facebookReadPermissions, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
if error != nil {
// Process error
FBSDKLoginManager().logOut()
failureBlock(error)
} else if result.isCancelled {
// Handle cancellations
FBSDKLoginManager().logOut()
failureBlock(nil)
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
var allPermsGranted = true
let grantedPermissions = Array(result.grantedPermissions).map( {"\($0)"} )
for permission in self.facebookReadPermissions {
if !contains(grantedPermissions, permission) {
allPermsGranted = false
break
}
}
if allPermsGranted {
// Do work
let fbToken = result.token.tokenString
let fbUserID = result.token.userID
println(fbUserID)
successBlock()
} else {
//The user did not grant all permissions requested
failureBlock(nil)
}
}
})
}
func login() -> Void{
if(self.alreadyLoggedIn() == false){
self.performLogin()
}else{
self.getFBUserData()
}
}
private func performLogin() -> Void {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager();
fbLoginManager.loginBehavior = FBSDKLoginBehavior.SystemAccount
println(FBSDKAccessToken.currentAccessToken())
fbLoginManager.logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
})
}
private func alreadyLoggedIn() -> Bool {
if (FBSDKAccessToken.currentAccessToken() != nil)
{
return true
}else{
return false
}
}
private func getFBUserData() -> Void{
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
}
})
}
}
}
Thanks
In your view controller viewDidLoad() you can add this code :
if (FBSDKAccessToken.currentAccessToken() == nil)
{
// User is not already logged
println("No Logged")
}
else
{
// User is already logged
println("Already Logged")
}

Resources