ユーザー名フィールドのユーザーのユーザー名を解析にフェッチしたい。
loginWithFacebook(withcompletionHandler)
ユーザー情報を取得するためにandを使用し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)
}
})
}
}