graphRequest.start不返回任何数据

时间:2016-10-03 21:24:45

标签: ios swift facebook-graph-api

我使用Facebook SDK使用Swift 3登录iOS。

这是我的代码。问题是没有返回任何值。

import UIKit

类ViewController:UIViewController {

@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var uLoginButton: UIButton!

let loginButton: FBSDKLoginButton = {

    let button = FBSDKLoginButton()

    button.readPermissions = ["email"]

    return button;

}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(loginButton);
    loginButton.center = view.center
    loginButton.delegate = self

    if (FBSDKAccessToken.current() != nil) {
        fetchProfile();
    }

    self.style()
}

func fetchProfile(){

    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "email, name, first_name, last_name, picture.type(large)"])

    let connection = FBSDKGraphRequestConnection()

    connection.add(graphRequest, completionHandler: { (connection, result, error) in

        if error != nil {

            //do something with error
            print("No Data returned")
            print(result)
        } else {

            //do something with result
            print("Data returned")
            print(result)

        }

    })

    connection.start()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func style(){
    self.email.layer.cornerRadius = 15;
    self.password.layer.cornerRadius = 15;
    self.uLoginButton.layer.cornerRadius = 15;

}

}

扩展名ViewController:FBSDKLoginButtonDelegate {

internal func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {

    // when the login is correct!
    print("Success login!");
    fetchProfile()
}

internal func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    FBSDKAppEvents.activateApp()
}

internal func loginButtonWillLogin(_ loginButton: FBSDKLoginButton!) -> Bool {
    return true
}

}

1 个答案:

答案 0 :(得分:0)

在Swift 3中,这就是你想要执行图形请求的方式。

  1. 初始化FBSDKGraphRequest
  2. 初始化FBSDKGraphRequestConnection
  3. 添加请求连接请求
  4. 开始连接。
  5. 所以,

    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "email"])
        let connection = FBSDKGraphRequestConnection()
        connection.add(graphRequest, completionHandler: { (connection, result, error) in
    
            if error != nil {
    
                //do something with error
    
            } else {
    
                //do something with result
    
            }
    
        })
    
        connection.start()