iOS
Twitter login
8 min
add twitter login to your ios app using swift introduction this section explains how you can create an app with user registration using twitter 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 twitter set up to start using twitter functions, you need to go to twitter application management website , sign in with a twitter account and click on create new app create new app add your applicationās twitter consumer key on your parse applicationās settings page when asked to specify a ācallback urlā for your twitter app, please insert a valid url like http //twitter oauth callback http //twitter oauth callback this value will not be used by your ios or android application, but is necessary in order to enable authentication through twitter add the accounts framework and social framework social framework libraries to your xcode project add the following where you initialize the parse sdk, such as inapplication\ didfinishlaunchingwithoptions inapplication\ didfinishlaunchingwithoptions 1 pftwitterutils initializewithconsumerkey("your consumer key", consumersecret "your consumer secret") 2 login and signup pftwitterutils pftwitterutils provides a way to allow your pfusers pfusers to log in or sign up through twitter this is accomplished using the loginwithblock loginwithblock or loginwithtarget loginwithtarget messages 1 pftwitterutils loginwithblock { 2 (user pfuser?, error nserror?) > void in 3 if let user = user { 4 if user isnew { 5 print("user signed up and logged in with twitter!") 6 } else { 7 print("user logged in with twitter!") 8 } 9 } else { 10 print("uh oh the user cancelled the twitter login ") 11 } 12 } when this code is run, the following happens the user is shown the twitter login dialog the user authenticates via twitter, and your app receives a callback our sdk receives the twitter data and saves it to a pfuser pfuser if itās a new user based on the twitter handle, then that user is created your block block is called with the user 3 twitter linking if you want to associate an existing pfuser pfuser with a twitter account, you can link it like so 1 if !pftwitterutils islinkedwithuser(user) { 2 pftwitterutils linkuser(user, { 3 (succeeded bool?, error nserror?) > void in 4 if pftwitterutils islinkedwithuser(user) { 5 print("woohoo, user logged in with twitter!") 6 } 7 }) 8 } the steps that happen when linking are very similar to log in the difference is that on successful login, the existing pfuser is updated with the twitter information future logins via twitter will now log the user into their existing account if you want to unlink twitter from a user, simply do this 1 pftwitterutils unlinkuserinbackground(user, { 2 (succeeded bool?, error nserror?) > void in 3 if error == nil && succeeded { 4 print("the user is no longer associated with their twitter account ") 5 } 6 }) 6 twitter api calls our sdk provides a straightforward way to sign your api http requests to the twitter rest api https //dev twitter com/rest/public when your app has a twitter linked pfuser pfuser to make a request through our api, you can use the pf twitter pf twitter singleton provided by pftwitterutils pftwitterutils 1 let verify = nsurl(string "https //api twitter com/1 1/account/verify credentials json") 2 var request = nsmutableurlrequest(url verify!) 3 pftwitterutils twitter()! signrequest(request) 4 let task = nsurlsession sharedsession() datataskwithrequest(request) { data, response, error in 5 // check for error 6 // data will contain the response data 7 } 8 task resume()