saving email address Facebook + Parse (Swift) -
so i'm trying use parse + facebook login people in app, experiencing difficulty saving actual name/email address of user. i'm using facebook authentication now, , right user able login/reigster stores username encoded string (ie 6zahveaklzykna5weq29) instead of actual name. furthermore, no email address store (i using parse default "user" table).
here code
main view controller:
class homeviewcontroller: uiviewcontroller, pfloginviewcontrollerdelegate, pfsignupviewcontrollerdelegate { @iboutlet weak var namelabel: uilabel! var noteobjects: nsmutablearray! = nsmutablearray() let permissions = ["public_profile", "email"] override func viewdidload() { super.viewdidload() utils.loginwithfacebook() } override func viewdidappear(animated: bool) { super.viewdidappear(animated) if (pfuser.currentuser() == nil) { // utils.loginwithfacebook() let loginviewcontroller = myloginviewcontroller() loginviewcontroller.signupcontroller = pfsignupviewcontroller() //self.presentviewcontroller(loginviewcontroller, animated: true, completion: nil) loginviewcontroller.fields = pfloginfields.facebook loginviewcontroller.delegate = self var signupviewcontroller = pfsignupviewcontroller() signupviewcontroller.delegate = self loginviewcontroller.signupcontroller = signupviewcontroller self.presentviewcontroller(loginviewcontroller, animated: true, completion: nil) }else { // utils.loginwithfacebook() var username = pfuser.currentuser().username namelabel.text = username self.fetchallobjectsfromlocaldatastore() self.fetchallobjects() } } func fetchallobjectsfromlocaldatastore() { var query: pfquery = pfquery(classname: "note") query.fromlocaldatastore() query.wherekey("username", equalto: pfuser.currentuser().username) query.findobjectsinbackgroundwithblock { (objects, error) -> void in if (error == nil) { var temp: nsarray = objects nsarray self.noteobjects = temp.mutablecopy() as! nsmutablearray // self.tableview.reloaddata() }else { println(error.userinfo) } } } func fetchallobjects() { pfobject.unpinallobjectsinbackgroundwithblock(nil) var query: pfquery = pfquery(classname: "note") query.wherekey("username", equalto: pfuser.currentuser().username) query.findobjectsinbackgroundwithblock { (objects, error) -> void in if (error == nil) { pfobject.pinallinbackground(objects, block: nil) self.fetchallobjectsfromlocaldatastore() }else { println(error.userinfo) } } } func loginviewcontroller(logincontroller: pfloginviewcontroller!, shouldbeginloginwithusername username: string!, password: string!) -> bool { if (!username.isempty || !password.isempty) { return true }else { return false } } func loginviewcontroller(logincontroller: pfloginviewcontroller!, didloginuser user: pfuser!) { self.dismissviewcontrolleranimated(true, completion: nil) } func loginviewcontroller(logincontroller: pfloginviewcontroller!, didfailtologinwitherror error: nserror!) { println("failed log in...") } func signupviewcontroller(signupcontroller: pfsignupviewcontroller!, shouldbeginsignup info: [nsobject : anyobject]!) -> bool { if let password = info?["password"] as? string { // return password.utf16count >= 8 return count(password.utf16) >= 8 } else { return false } } func signupviewcontroller(signupcontroller: pfsignupviewcontroller!, didsignupuser user: pfuser!) { self.dismissviewcontrolleranimated(true, completion: nil) } func signupviewcontroller(signupcontroller: pfsignupviewcontroller!, didfailtosignupwitherror error: nserror!) { println("failed sign up...") } func signupviewcontrollerdidcancelsignup(signupcontroller: pfsignupviewcontroller!) { println("user dismissed sign up.") } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } // mark: - table view data source override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { //var upcoming: addnotetableviewcontroller = segue.destinationviewcontroller as! addnotetableviewcontroller if (segue.identifier == "editnote") { } } }
utils class have facebook login code
import foundation class utils { class func notloggedin() -> bool { let user = pfuser.currentuser() // here assume user must linked facebook return user == nil || !pffacebookutils.islinkedwithuser(user) } class func loggedin() -> bool { return !notloggedin() } class func loginwithfacebook() { pffacebookutils.loginwithpermissions(["public_profile", "user_friends", "email"]) { (user: pfuser!, error: nserror!) -> void in if user == nil { nslog("the user cancelled facebook login (user nil)") } else { nslog("the user logged in facebook (user not nil)") // here set acl installation if let installation = pfinstallation.currentinstallation() { let acl = pfacl(user: pfuser.currentuser()) // user can write acl.setpublicreadaccess(true) // can read acl.setwriteaccess(true, forrolewithname: "admin") // admins can write installation.acl = acl //installation.saveeventually() } // username , fbid utils.obtainusernameandfbid() } } } class func obtainusernameandfbid() { if notloggedin() { return } let user = pfuser.currentuser() // won't nil because logged in // return if have username , fbid (note check fbid because parse automatically fills in username random numbers) if let fbid = user["fbid"] as? string { if !fbid.isempty { println("we have username , fbid -> return") return } } // request facebook println("performing request fb username , idf...") if let session = pffacebookutils.session() { if session.isopen { println("session open") fbrequestconnection.startformewithcompletionhandler({ (connection: fbrequestconnection!, result: anyobject!, error: nserror!) -> void in println("done me request") if error != nil { println("facebook me request - error not nil :(") } else { println("facebook me request - error nil :)") println(result) // have 2 ways access result: // 1) println(result["name"]) println(result["id"]) println(result["email"]) // 2) println(result.name) println(result.objectid) println(result.email) // save parse: pfuser.currentuser().username = result.email pfuser.currentuser().email = result.email pfuser.currentuser().setvalue(result.objectid, forkey: "fbid") pfuser.currentuser().setvalue(result.email, forkey: "email") //pfuser.currentuser().saveeventually() // use saveeventually if want sure save succeed } }) } } } }
the login screen user logs utilise facebook api won't give user's email. instead give access token 6zahveaklzykna5weq29
seeing. unless create custom web view not able store user's email. can store access token in nsuserdefaults or in case parse (i recommend using nsuserdefaults it's easier) , use make calls facebook api.
Comments
Post a Comment