【问题标题】:Can't get account info Parse & FB SDK无法获取账户信息 Parse & FB SDK
【发布时间】:2016-09-15 22:02:57
【问题描述】:

我正在使用 ParseFacebookUtilsV4,而 fb idk 似乎在检索用户信息时遇到问题,只是暂时打印到控制台中。问题是似乎启动请求的块没有被执行,因为每当我调试它似乎只是跳过启动完成处理程序。

// View controller code
        PFFacebookUtils.facebookLoginManager().loginBehavior = .web

        var loginTask = PFFacebookUtils.logInInBackground(withReadPermissions: [])

        loginTask.continue( { (bfTask) -> Any? in

            print("I'm here")

            let request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields":"id,email,name,first_name,last_name,picture"]  )
            print(request)
            request?.start {

                (connection, result, error) in

                print(result)

            }
            return ""
        })

// App delegate config

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let configuration = ParseClientConfiguration {
            $0.applicationId = "xxxx"
            $0.clientKey = "xxxxx"
            $0.server = "https://parseapi.back4app.com"
        }
        Parse.initialize(with: configuration)

        PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)

        PFTwitterUtils.initialize(withConsumerKey: "xxxxx", consumerSecret: "xxxxx")

        // Override point for customization after application launch.
        return true
    }

【问题讨论】:

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


    【解决方案1】:

    这是我用于 Facebook 请求的函数。我正在使用阻止请求:

    func loadFacebookUserDetails() {
    
        // Define fields we would like to read from Facebook User object
        let requestParameters = ["fields": "id, email, first_name, last_name, name"]
    
        // Send Facebook Graph API Request for /me
        let userDetails = FBSDKGraphRequest(graphPath: "me", parameters: requestParameters)
        userDetails.startWithCompletionHandler({
            (connection, result, error: NSError!) -> Void in
            if error != nil {
                let userMessage = error!.localizedDescription
                let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
    
                let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
                myAlert.addAction(okAction)
                self.presentViewController(myAlert, animated: true, completion: nil)
    
                PFUser.logOut()
                return
            }
    
            // Extract user fields
            let userId:String = result.objectForKey("id") as! String
            let userEmail:String? = result.objectForKey("email") as? String
            let userFirstName:String?  = result.objectForKey("first_name") as? String
            let userLastName:String? = result.objectForKey("last_name") as? String
    
    
    
            // Get Facebook profile picture
            let userProfile = "https://graph.facebook.com/" + userId + "/picture?type=large"
    
            let profilePictureUrl = NSURL(string: userProfile)
    
            let profilePictureData = NSData(contentsOfURL: profilePictureUrl!)
    
    
            // Prepare PFUser object
            if(profilePictureData != nil)
            {
                let profileFileObject = PFFile(name:"profilePic.jpeg",data:profilePictureData!)
                PFUser.currentUser()?.setObject(profileFileObject!, forKey: "ProfilePic")
            }
    
            PFUser.currentUser()?.setObject(userFirstName!, forKey: "Name")
            PFUser.currentUser()?.setObject(userLastName!, forKey: "last_name")
    
    
    
            if let userEmail = userEmail
            {
                PFUser.currentUser()?.email = userEmail
                PFUser.currentUser()?.username = userEmail
            }
    
    
    
            PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in
    
    
                if(error != nil)
                {
                    let userMessage = error!.localizedDescription
                    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
                    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
                    myAlert.addAction(okAction)
                    self.presentViewController(myAlert, animated: true, completion: nil)
                    PFUser.logOut()
                    return
    
    
                }
    
    
                if(success)
                {
                    if !userId.isEmpty
                    {
                        NSUserDefaults.standardUserDefaults().setObject(userId, forKey: "user_name")
                        NSUserDefaults.standardUserDefaults().synchronize()
    
    
                        dispatch_async(dispatch_get_main_queue()) {
                            self.dismissViewControllerAnimated(true, completion: nil)
                        }
    
                    }
    
                }
    
            })
    
    
        })
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2013-05-04
      相关资源
      最近更新 更多