在注册 Parse 后端 (Swift) 获取 Facebook 个人资料图片



我正在创建一个将 Parse 作为后端服务的应用程序。我的用户应该能够通过Facebook注册和登录。

我在下面做了这个(工作绝对没问题)。

@IBAction func registerWithFacebook(sender: UIButton) {
    let permissions:[String] = ["user_about_me","user_relationships", "public_profile"]
    PFFacebookUtils.logInWithPermissions(permissions, {
        (user: PFUser!, error: NSError!) -> Void in
        if user == nil {
            NSLog("Uh oh. The user cancelled the Facebook login.")
        } else if user.isNew {
            NSLog("User signed up and logged in through Facebook!")
            self.loadData()
            self.performSegueWithIdentifier("initialToMain", sender: self)
        } else {
            NSLog("User logged in through Facebook!")
            self.performSegueWithIdentifier("initialToMain", sender: self)
        }
    })
}
func loadData(){
    let request:FBRequest = FBRequest.requestForMe()
    request.startWithCompletionHandler { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        if error == nil{
            if let dict = result as? Dictionary<String, AnyObject>{
                let name:String = dict["name"] as AnyObject? as String
                let email:String = dict["email"] as AnyObject? as String
                println(name)
                PFUser.currentUser().setValue(name, forKey: "username")
                PFUser.currentUser().setValue(email, forKey: "email")
                PFUser.currentUser().save()
            }
        }
    }
}

不幸的是,我无法从正在注册的用户那里获得个人资料图片。我该怎么做?

图片可通过以下 URL 中的用户 ID 公开获得:

https://graph.facebook.com/USER_ID/picture

您还可以请求各种尺寸:

https://graph.facebook.com/USER_ID/picture?width=300&height=300

这是有效的解决方案:

    func loadData(){
    let request:FBRequest = FBRequest.requestForMe()
    request.startWithCompletionHandler { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
        if error == nil{
            if let dict = result as? Dictionary<String, AnyObject>{
                let name:String = dict["name"] as AnyObject? as String
                let facebookID:String = dict["id"] as AnyObject? as String
                let email:String = dict["email"] as AnyObject? as String
                let pictureURL = "https://graph.facebook.com/(facebookID)/picture?type=large&return_ssl_resources=1"
                var URLRequest = NSURL(string: pictureURL)
                var URLRequestNeeded = NSURLRequest(URL: URLRequest!)

                NSURLConnection.sendAsynchronousRequest(URLRequestNeeded, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!, error: NSError!) -> Void in
                    if error == nil {
                        var picture = PFFile(data: data)
                        PFUser.currentUser().setObject(picture, forKey: "profilePicture")
                        PFUser.currentUser().saveInBackground()
                    }
                    else {
                        println("Error: (error.localizedDescription)")
                    }
                })
                PFUser.currentUser().setValue(name, forKey: "username")
                PFUser.currentUser().setValue(email, forKey: "email")
                PFUser.currentUser().saveInBackground()
            }
        }
    }
}

最新更新