在Swift3中从FBSDKGraphRequest返回数据时遇到一些问题

时间:2017-02-22 08:47:56

标签: swift3 fbsdk

我是Swift3的新手。现在,我尝试做一个.swift,它可以保留我需要的一些功能。

例如:我想得到如下的userName,所以我创建一个var来设置闭包中的值字符串。 一切都没关系,并且在关闭之前有值字符串,但是当它关​​闭时,值就会消失。 发生了什么 !!??有人能告诉我吗? Plz ..非常感谢你T_T

冷到这里:

import UIKit
import FBSDKLoginKit
import FBSDKCoreKit

class CheckStatus: FBSDKLoginButton {

    var userID: String = ""
    var userName: String = ""
    var userLink: String = ""
    var userEmail: String = ""
    var userPictureUrl: String = ""

    func getUserDefault() {
        guard FBSDKAccessToken.current() != nil else {
            return
        }

        let userInfo: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email, link, picture"])
        userInfo.start(completionHandler: { (_, result, error) -> Void in
            if error == nil {
                self.readPermissions = ["email", "public_profile"] // FBSDKLoginButton's permission
                guard let userDefault: [String:AnyObject] = result as? [String:AnyObject] else {
                    return
                }
                guard let userDefaultID: String = userDefault["id"] as? String else {
                    return
                }
                guard let userDefaultName: String = userDefault["name"] as? String else {
                    return
                }
                guard let userDefaultLink: String = userDefault["link"] as? String else {
                    return
                }
                guard let userDefaultEmail: String = userDefault["email"] as? String else {
                    return
                }
                guard let userDefaultPicture: [String:AnyObject] = userDefault["picture"] as? [String:AnyObject] else {
                    return
                }
                guard let userDefaultData: [String:AnyObject] = userDefaultPicture["data"] as? [String:AnyObject] else {
                    return
                }
                guard let userDefaultUrl: String = userDefaultData["url"] as? String else {
                    return
                }

                self.userID = userDefaultID
                self.userName = userDefaultName
                self.userLink = userDefaultLink
                self.userEmail = userDefaultEmail
                self.userPictureUrl = userDefaultUrl
                print("----- User data below -----\nID: \(userDefaultID)\nName: \(userDefaultName)\nLink: \(userDefaultLink)\nEmail: \(userDefaultEmail)\nPicture: \(userDefaultUrl)\n ----------------------")
                print("Have value : \(self.userName)") // This is work
            }
        })

        print("No value : \(self.userName)") // No value !!
    }
}

0 个答案:

没有答案