在這裏與夫特3.1沿工作的代碼的Xcode 8.3:
按照這些步驟獲得的Facebook數據: -
步驟1:
import FBSDKCoreKit
import FBSDKLoginKit
步驟2:符合委託。
FBSDKLoginButtonDelegate
步驟3:登錄按鈕IBAction方法。
// Facebook的登錄操作
@IBAction func facebookLoginAction(_ sender: Any) {
//Check Internet connection
guard InternetAvailable() else {return}
//Fb login process
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = .native
fbLoginManager.logOut()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.isCancelled {
}
else if (fbloginresult.declinedPermissions != nil){
if(fbloginresult.grantedPermissions.contains("email")) {
self.getFBUserData()
}
}
}
}
}
第4步:獲取Facebook的數據如下方式。
func getFBUserData(){
//Showing activity indicator
HUD.flash(.image(UIImage.gif(name: "App-Loader")), delay: 100.0)
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name, last_name,email, picture.type(large),gender"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
let newResult = result as AnyObject
let email = newResult["email"] as? String ?? ""
print("Your Email = \(email)")
let firstName = newResult["first_name"] as? String ?? ""
let lastName = newResult["last_name"] as? String ?? ""
print(FBSDKAccessToken.current())
let gender = newResult["gender"] as? String ?? ""
let sex = (gender == "male" ? HCConstants.Gender.MALE : HCConstants.Gender.FEMALE).description
let id = newResult["id"] as? String ?? ""
print("the access token is \(FBSDKAccessToken.current().tokenString)")
let accessToken = FBSDKAccessToken.current().tokenString
self.signInUser()
//Hid PKHUD
HUD.hide()
}
})
}
}
隨時對此發表評論,希望能解決您的問題。謝謝
您是否正確獲取社交數據? –
是的我正確地得到它,但我懷疑它是否會創建兩個會話? –
爲什麼你需要兩個不同的會話 –