backendless.userService.loginWithFacebookSDK with swift

Hello and thanks in advance for your time.

I’ve been looking around for an example in swift of the Login with Facebook SDK but without success.

Is there any official example?

Best Regards

Hi Hermes,

You can investigate the objective-c FacebookLogin sample: https://github.com/Backendless/iOS-Samples/tree/master/samples/objective-c/backendlessDemos - and implement swift version.

We plan to implement the swift sample asap, and we let you know about.

Regards,
Slava

Thanks Vyacheslav I already did that and try with the Login with SDK and Easy Login method too, using all the different signatures available, without success.
I’m getting an error with the FAULT parameter when trying to call the method (please see the picture attached)

197:99: Cannot convert value of type ‘Fault?.Type’ (aka ‘Optional<Fault>.Type’) to expected argument type ‘AutoreleasingUnsafeMutablePointer<Fault?>’ (aka ‘AutoreleasingUnsafeMutablePointer<Optional<Fault>>’)

Best Regards

In Swift you should use the following method signatures:

  • for sync - methods without “error” parameter, for example:
try backendless.userService.easyLoginWithFacebookFieldsMapping(["111":"222"], permissions: ["email"])
  • for async - methods with blocks, for example:
        backendless.userService.easyLoginWithFacebookFieldsMapping(["111":"222"], permissions: ["email"],
            response: {(result : AnyObject!) -> () in
                print ("RESULT: \(result)")
            },
            error: { (fault : Fault!) -> () in
                print("Server reported an error: \(fault)")
        })

Many thanks Slava, I’ll try that and return to post the result.

Regards

You can also use the sync methods with error parameter:

 var error: Fault?
backendless.userService.login("fdsa2@fdsa.com", password: "asdf1", error: &error)
 if error == nil {
 print("LOGIN: \(backendless.userService.currentUser)")
 }
 else {
 print("Server reported an error: \(error)")
 }

The sync methods without error parameter must use “objective-c try/catch wrapper” (because they throw the objective-c style exception):

        Types.tryblock({ () -> Void in
            
            let user = self.backendless.userService.login("james.bond@mi6.co.uk", password: "iAmWatchingU")
            print("User has been logged in (SYNC): \(user)")
            },
            catchblock: { (exception) -> Void in
                print("Server reported an error: \(exception as! Fault)")
        })

Hi again Slava, I’m trying now using the login with Facebook SDK but now I’m getting an error. I have the Enable Multiple Logins turned on

Server reported an error: FAULT = ‘3033’ [Unable to register user. User already exists.] <Unable to register user. User already exists.>









 func application(application: UIApplication, openURL url: NSURL,

        sourceApplication: String?,

        annotation: AnyObject?) -> Bool {

            

    

             let result =  FBSDKApplicationDelegate.sharedInstance().application(

                application,

                openURL: url,

                sourceApplication: sourceApplication,

                annotation: annotation)




            if result {

                

                let token = FBSDKAccessToken.currentAccessToken()

                let fieldsMapping = [

                    "id" : "facebookId",

                    "name" : "name",

                    "birthday": "birthday",

                    "first_name": "fb_first_name",

                    "last_name" : "fb_last_name",

                    "gender": "gender",

                    "email": "email"

                ]

                

                backendless.userService.loginWithFacebookSDK(

                    token,

                    fieldsMapping: fieldsMapping,

                    response: { (var registeredUser : AnyObject! ) -> () in

                        

                        print("User has been logged in (ASYNC): \(registeredUser)")

                        

                        // Login is successfull

                        

                        

                    },

                    error: { (var fault : Fault!) -> () in

                        print("Server reported an error: \(fault)")

                      

                    }

                )

            }

            

            return result

        

        

            

    }

“Multiple logins” are not related to user registrations. When you register a user with a value for the property marked as “identity” and another user already exists with that identity, the server returns the error you’re getting.

Hi Mark.

What method should I use then in the following scenario?

1.- User Login with Facebook credentials in one device. (I understand that if this is the first time to use the app this is when the registration process takes place)

2.- Same User wants to Login from another device using the same Facebook credentials. (this is when I’m getting this error)

Many thanks in advance

Hi Hermes,

I will have to defer to our server-side devs who should be able to answer. I will open an internal ticket to get the answer for the question.

Regards,
Mark

By the way, the Android version of the app has no problem. The user uses Facebook credentials to login and logout several times. No error is thrown.

Thanks so much Mark, very appreciated.

Hi Hermes,

I cannot reproduce an issue of this thread. I just logged in from iPhone 5, iPad-mini and simulator at the same time. I’ve checked the both methods - easyFacebookLogin and loginFacebookSDK, and they have gone successfully.

Can you give us your sample project which demonstrates this problem - here in attachment or by my email: slavav@themidnightcoders.com

Regards,
Slava

Thank you Slava, I just sent you an email.

I am experiencing the same issue. I have logged into the account using loginWithFacebook from iOS then Android. When logging in using Facebook Android, I now get the following error even though the user is setup as a Facebook user in the database:

Server reported an error: FAULT = ‘3033’ [Unable to register user. User already exists.] <Unable to register user. User already exists.>

Hi Austin,

We have an internal ticket to investigate this and correct the problem.

Regards,
Mark

Thanks, Mark.

In response to Slava, the issue for me seems to be more about cross platform instead of cross device i.e. if I Facebook login from a device using an iOS key and then a Windows Phone or Android device. When I cross platforms for the same user I am experiencing the problem which is what I believe the original poster describes as well.

In your example, all of your devices were iOS devices.

Hi, Austin, Hermes!

We’ve tried different combinations of cross platform Facebook logins (different devices, login order, permissions and fieldsmapping) but could not reproduce the error you are reporting.
Can you please show your code for invoking login methods (both for iOS and Android) so we can can track and fix this error? Your application IDs would be also helpful.
Thanks!

Could you also please send screenshots of your Facebook app Settings (Basic and Advanced tabs). Either here or anatoly.stepanyuk@themidnightcoders.com

Thanks