我从Facebook加载用户照片时遇到问题。

我想获取Facebook用户照片以解析数据库。

我的代码:

let permissions:[String] = ["public_profile", "email"]
    PFFacebookUtils.logInInBackground(withReadPermissions: permissions) { (user, error) 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()
        } else {
            NSLog("User logged in through Facebook!")

        }
    }

func loadData(){
    let fbRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    fbRequest?.start(completionHandler: { (FBSDKGraphRequestConnection, result, error) 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"

                let URLRequest = NSURL(string: pictureURL)
                let URLRequestNeeded = NSURLRequest(url: URLRequest! as URL)



                NSURLConnection.sendAsynchronousRequest(URLRequestNeeded as URLRequest, queue: OperationQueue.main, completionHandler: { (response, data, error) in
                        if error == nil {
                            let picture = PFFile(data: data!)
                            PFUser.current()?.setObject(picture!, forKey: "profilePicture")
                            PFUser.current()?.saveInBackground()
                        }
                        else {
                            print("Error: \(String(describing: error?.localizedDescription))")
                        }
                    })


                PFUser.current()!.setValue(name, forKey: "username")
                PFUser.current()!.setValue(email, forKey: "email")
                PFUser.current()!.saveInBackground()
            }
        }
    })

}


但是我一直都有错误信息,并且在数据库中我有空行。

我该如何解决?

最佳答案

你可以试试

    @IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result!
                // if user cancel the login
                if (result?.isCancelled)!{
                    return
                }
                if(fbloginresult.grantedPermissions.contains("email"))
                {
                    self.getFBUserData()
                }
            }else {
                print(error!.localizedDescription)
//                self.view.showToast(withMessage: error!.localizedDescription)
            }
        }
    }

    func getFBUserData(){
        if((FBSDKAccessToken.current()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
                if (error == nil){
                    //everything works print the user data
                    print(result)
//                    if let data = result as? [String:Any],
//                        let user = Mapper<User>().map(
//                            JSONObject: data
//                        ){
//                        AppHelper.set(currentuser: user)
//                    }
                }else {
//                    self.view.showToast(withMessage: error!.localizedDescription)
                    print(error!.localizedDescription)
                }
            })
        }
    }

关于ios - iOS如何从Facebook获取用户照片使用parse swift 4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52867670/

10-12 06:13