2016-10-03 51 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

我編輯代碼內容,看看並告訴我 –