ios - How to get the username from Facebook in Swift -
i want fetch username of user in username field parse.
i using loginwithfacebook(withcompletionhandler)
, fetchuserinforfromfacebook(withcompletionhandler)
fetch userinfo have problem: these methods create own object , store in parse username field, want user username.
func loginwithfacebook(withcompletionhandler: (success:bool) ->()){ var permissions : array = [ "user_location","public_profile"] pffacebookutils.logininbackgroundwithreadpermissions(permissions, { (user: pfuser?, error: nserror?) -> void in if let user = user { if user.isnew { self.fetchuserinforfromfacebook(withcompletionhandler) println("user signed , logged in through facebook!") } withcompletionhandler(success: true) println("user logged in through facebook!") } else { println("uh oh. user cancelled facebook login.") self.showerrormessage(error!) withcompletionhandler(success: false) } }) } func fetchuserinforfromfacebook(withcompletionhandler: (success:bool) ->()){ if ((fbsdkaccesstoken.currentaccesstoken()) != nil){ var request = fbsdkgraphrequest(graphpath:"me", parameters:nil) request.startwithcompletionhandler({connection, result, error in if error == nil { //facebook data in dictionary var userdata = result nsdictionary var currentuser : pfuser = pfuser.currentuser() currentuser.setobject(userdata.objectforkey("id") string, forkey: "facebookid") currentuser.setobject( userdata.objectforkey("name") string, forkey: "fullname") currentuser.email = userdata.objectforkey("email") as? string currentuser.username = userdata.objectforkey("email") as? string var location = userdata.objectforkey("location") as? nsdictionary var locname = location?.objectforkey("name") as? string if let loc = locname { var fullnamearr = split(loc) {$0 == ","} var city: string? = fullnamearr.count > 0 ? fullnamearr[0] : nil var contry: string? = fullnamearr.count > 1 ? fullnamearr[1]: nil currentuser.setobject(userdata.objectforkey("country"), forkey: "countryname") currentuser.setobject(userdata.objectforkey("city"), forkey: "cityname") } currentuser.saveinbackground() withcompletionhandler(success: true) }else{ println("error") self.showerrormessage(error) withcompletionhandler(success: false) } }) } }
much easier approach username:
firstly you need call fbsession method basic profile info , id of user.
secondly once user fb id, can directly pass fb id in graph api: https://graph.facebook.com/?id=1008223822
aand username in following json format:-
{ "id": "1008223822", "first_name": "dj\u00e9", "gender": "male", "last_name": "destolicci", "link": "https://www.facebook.com/dje.destolicci", "locale": "fr_fr", "name": "dj\u00e9 destolicci", "username": "dje.destolicci" }
here code:-
//first simple login , authenticate user basic profile info , fb id fbsession.openactivesessionwithreadpermissions(["public_profile", "email", "user_friends"] , allowloginui: true, completionhandler: { (session:fbsession!, state:fbsessionstate, error:nserror!) -> void in if (error==nil) { fbrequest.requestforme().startwithcompletionhandler({ (connection:fbrequestconnection!, result:anyobject!, error:nserror!) -> void in if (error==nil) { var buserfacebookdict = result as! nsdictionary let fb_user_id = buserfacebookdict["id"]! as! string // call fb graph api to username of user. var graphprofileurl = nsurl(string: "https://graph.facebook.com/?id=\(fb_user_id)") var request1: nsurlrequest = nsurlrequest(url: graphprofileurl!) var response: autoreleasingunsafemutablepointer<nsurlresponse? >=nil var error: nserrorpointer = nil var dataval: nsdata = nsurlconnection.sendsynchronousrequest(request1, returningresponse: response, error:nil)! var err: nserror println(response) var jsonresult: nsdictionary = nsjsonserialization.jsonobjectwithdata(dataval, options: nsjsonreadingoptions.mutablecontainers, error: nil) as! nsdictionary println("synchronous\(jsonresult)") let fb_user_name = jsonresult["username"]! as! string //finally username } else { //utility.showerroralert(error.description) } }) } })
Comments
Post a Comment