【问题标题】:Get Facebook Profile picture at Sign-up for Parse backend (Swift)在 Sign-up for Parse 后端获取 Facebook 个人资料图片 (Swift)
【发布时间】:2015-03-16 20:59:32
【问题描述】:

我正在创建一个使用 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()
            }
        }
    }
}

很遗憾,我无法从注册用户那里获得个人资料图片。我该怎么做?

【问题讨论】:

    标签: ios facebook swift facebook-graph-api parse-platform


    【解决方案1】:

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

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

    您还可以要求各种尺寸:

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

    【讨论】:

    • 感谢您的回答,但实际上我已经知道了。现在我自己解决了。
    【解决方案2】:

    这是有效的解决方案:

        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()
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2016-01-24
      • 2017-09-30
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多