我想将用户名字段中用户的用户名提取到解析中。
我正在使用loginWithFacebook(withcompletionHandler)
并fetchUserInforFromFacebook(withcompletionHandler)
获取用户信息,但我有一个问题:这些方法创建一个自己的对象并存储在解析用户名字段中,但我想获取用户用户名。
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 UP and logged in through Facebook!")
}
withcompletionHandler(success: true)
println("User logged in through Facebook!")
} else {
println("Uh oh. The user cancelled the 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 as NSDictionary
var currentUser : PFUser = PFUser.currentUser()
currentUser.setObject(userData.objectForKey("id") as String, forKey: "faceBookID")
currentUser.setObject( userData.objectForKey("name") as 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)
}
})
}
}