2016-10-03 64 views
0

我正在使用Swift 3的iOS登录iOS SDK。graphRequest.start不返回任何数据

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

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 
} 

}

回答

0

在夫特3,THI s是你想要执行图形请求的方式。

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

所以,

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() 
+0

嗨! 这运行正常!但是,我不会得到数据(电子邮件,用户名等...)...我用“打印(结果)”测试,但没有返回数据 –

+0

在这种情况下,可能没有facebook用户在你调用图形请求的时候。你是在用户登录之前还是之后调用它? – DevKyle

+0

我编辑代码内容,看看并告诉我 –