iOS
Facebook login
11 min
add facebook login to your ios app using swift tutorial introduction this section explains how you can create an app with user registration using facebook login and parse server core features through back4app prerequisites to complete this quickstart, you need xcode an app created at back4app follow the new parse app tutorial to learn how to create a parse app at back4app an ios app connected to back4app note follow the install parse sdk (objc) tutorial to create an xcode project connected to back4app 1 facebook set up to start using facebook functions, you need to go to the facebook developer website and create an account and an app add your application’s facebook application id on your parse application’s settings page follow facebook’s instructions for getting started with the facebook sdk https //developers facebook com/docs/ios/getting started to create an app linked to the facebook sdk 2 link your facebook app with back4app go to your app dashboard at back4app website and click on server settings server settings find the “facebook login” block and click on settings settings the “facebook login” block looks like this 3\ go back to your xcode project, open your info plist info plist copy the code from facebook configuration , step 4a, item 2, and paste it in the \<dict> \</dict> \<dict> \</dict> part of your info plist 4\ in order to use a dialog box from facebook, also copy and paste the code from section 4a, item 3 into your info plist info plist file 5\ save 3 setting up your app add the following to your application\ didfinishlaunchingwithoptions method, after you’ve initialized the parse sdk 1 import fbsdkcorekit 2 import parse 3 4 // appdelegate swift 5 func application(application uiapplicatiofunc application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]?) > bool { 6 // initialize parse 7 let parseconfig = parseclientconfiguration { 8 $0 applicationid = "parseappid" 9 $0 clientkey = "parseclientkey" 10 $0 server = "parseserverurlstring" 11 } 12 parse initialize(with parseconfig) 13 pffacebookutils initializefacebook(applicationlaunchoptions launchoptions) 14 } 2\ add the following handlers in your app delegate 1 func application( application uiapplication, open url url, sourceapplication string?, annotation any) > bool { 2 3 return fbsdkapplicationdelegate sharedinstance() application( 4 application, 5 open url, 6 sourceapplication sourceapplication, 7 annotation annotation 8 ) 9 10 } 11 12 func application( app uiapplication, open url url, options \[uiapplication openurloptionskey any] = \[ ]) > bool { 13 14 return fbsdkapplicationdelegate sharedinstance() application( 15 app, 16 open url, 17 sourceapplication options\[ sourceapplication] as? string, 18 annotation options\[ annotation] 19 ) 20 21 } 22 23 //make sure it isn't already declared in the app delegate (possible redefinition of func error) 24 func applicationdidbecomeactive( application uiapplication) { 25 fbsdkappevents activateapp() 26 } 4 log in & sign up pfuser pfuser provides a way to allow your users to log in or sign up through facebook this is done by using the logininbackgroundwithreadpermissions logininbackgroundwithreadpermissions method like so 1 pffacebookutils logininbackground(withreadpermissions permissions) { 2 (user pfuser?, error error?) in 3 if let user = user { 4 if user isnew { 5 print("user signed up and logged in through facebook!") 6 } else { 7 print("user logged in through facebook!") 8 } 9 } else { 10 print("uh oh the user cancelled the facebook login ") 11 } 12 } when this code is run, the following happens the user is shown the facebook login dialog the user authenticates via facebook, and your app receives a callback using handleopenurl handleopenurl our sdk receives the user’s facebook access data and saves it to a pfuser pfuser if no pfuser pfuser exists with the same facebook id, then a new pfuser pfuser is created your code block is called with the user the current user reference will be updated to this user the permissions argument is an array of strings that specifies what permissions your app requires from the facebook user these permissions must only include read permissions to acquire publishing permissions for a user so that your app can, for example, post status updates on their behalf, you must call \[pffacebookutils logininbackgroundwithpublishpermissions ] logininbackgroundwithpublishpermissions ] 1 pffacebookutils logininbackgroundwithpublishpermissions(\["publish actions"], { 2 (user pfuser?, error nserror?) > void in 3 if user != nil { 4 // your app now has publishing permissions for the user 5 } 6 }) 5 linking if you want to associate an existing pfuser pfuser to a facebook account, you can link it like so 1 if !pffacebookutils islinkedwithuser(user) { 2 pffacebookutils linkuserinbackground(user, withreadpermissions nil, { 3 (succeeded bool?, error nserror?) > void in 4 if succeeded { 5 print("woohoo, the user is linked with facebook!") 6 } 7 }) 8 } 6 unlinking if you want to unlink facebook from a user, simply do this 1 pffacebookutils unlinkuserinbackground(user, { 2 (succeeded bool?, error nserror?) > void in 3 if succeeded { 4 print("the user is no longer associated with their facebook account ") 5 } 6 })