我一直在努力在swift中創建一個Facebook登錄/註冊函數。我一直在尋找一個教程,但一直沒能找到任何東西。所以我一直在努力自己做。它似乎工作,但爲什麼它不保存我的facebookName,性別和圖像在數據庫中?它將它保存在模擬器中,但不是當我使用我的ios 8設備時?facebook解析註冊swift
我收到此日誌消息「用戶註冊並通過Facebook登錄!」,一個新的用戶添加到解析類,而不是姓名,形象和性別......
@IBAction func login(sender: UIButton!) {
var permissionArray = ["user_about_me", "user_relationships", "user_birthday", "user_location"]
PFFacebookUtils.initializeFacebook()
PFFacebookUtils.logInWithPermissions(permissionArray, block: { (user: PFUser!, error: NSError!) in
println(user)
if user == nil {
println(error.localizedDescription)
} else {
if user.isNew {
var userQuery = PFUser.query()
userQuery.getObjectInBackgroundWithId(PFUser.currentUser().objectId) {
(userObject: PFObject!, error: NSError!) -> Void in
var fbRequest = FBRequest.requestForMe()
fbRequest.startWithCompletionHandler { (connection: FBRequestConnection!, result:AnyObject!, error: NSError!) in
if error == nil {
//FACEBOOK DATA IN DICTIONARY
var userData = result as NSDictionary
var faceBookId = userData.objectForKey("id") as NSString
var faceBookName = userData.objectForKey("first_name") as NSString
var faceBookMiddle = userData.objectForKey("middle_name") as NSString
var faceBookGender = userData.objectForKey("gender") as NSString
var url:NSURL = NSURL.URLWithString(NSString(format:"https://graph.facebook.com/%@/picture?width=320", faceBookId))
var err: NSError?
var imageData :NSData = NSData.dataWithContentsOfURL(url, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)
var imageFile = PFFile(name: "image.jpg", data: imageData) as PFFile
println(userData)
userObject.setObject(faceBookName, forKey: "name")
userObject.setObject(imageFile, forKey: "file")
userObject.setObject(faceBookGender, forKey: "gender")
userObject.saveInBackground()
var sharedInstance:userSingleton = userSingleton.sharedInstance
sharedInstance.userName = (userObject.objectForKey("name") as NSString)
sharedInstance.userGender = (userObject.objectForKey("gender") as NSString)
(userObject.objectForKey("file") as PFFile).getDataInBackgroundWithBlock {
(theImageData: NSData!, error: NSError!) -> Void in
println(error)
if error == nil {
sharedInstance.userImage = UIImage(data:theImageData)
}
self.performSegueWithIdentifier("LoginSegue", sender: self)
}
}
}
}
println("User signed up and logged in through Facebook!")
} else {
println("User logged in through Facebook!")
}
}
})
}
}