你好,我需要一些帮助我一直在搜索关于这个主题的几个小时我尝试了很多东西但我做不到。
这是我的代码
let facebookReadPermissions = ["public_profile", "email"]
func getFBUserData() {
if((FBSDKAccessToken.current()) != nil){
self.getFBUserDataSecondStep()
} else {
FBSDKLoginManager().logIn(withReadPermissions: self.facebookReadPermissions, from: self) { (result, error) in
if error != nil {
FBSDKLoginManager().logOut()
} else if (result?.isCancelled)! {
FBSDKLoginManager().logOut()
} else {
self.showLoadingAlert()
let grantedPermissions = result?.grantedPermissions.map( {"\($0)"} )
for permission in self.facebookReadPermissions {
if (grantedPermissions?.contains(permission))! {
print("permission: \(permission)")
// CONSOLE WRITING HERE LIKE THIS
// permission: public_profile
// permission: email
}
}
if (result?.grantedPermissions.contains("email"))! {
print("result is: \(result?.description ?? "nil")")
// result is: <FBSDKLoginManagerLoginResult: 0x1c0441920> ( console )
self.getFBUserDataSecondStep()
} else {
FBSDKLoginManager().logOut()
}
}
}
}
}
func getFBUserDataSecondStep(){
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
if let dict = result as? Dictionary<String, Any> {
print(result)
// COMING WITH LIKE THIS
//"first_name" = Esn;
//id = 10156020817443470;
//"last_name" = "\U00c7ak\U0131ralar";
//name = "Esn Banu\U015f \U00c7ak\U0131ralar";
//--// Where is email ?
var email = ""
var firstName = ""
var lastName = ""
var facebookId = ""
var facebookToken = ""
if let controlEmail = dict["email"] {
email = controlEmail as! String
}
if let controlFirstName = dict["first_name"] {
firstName = controlFirstName as! String
}
if let controlLastName = dict["last_name"] {
lastName = controlLastName as! String
}
if let controlFacebookId = FBSDKAccessToken.current().userID {
facebookId = controlFacebookId
}
if let controlFacebookToken = FBSDKAccessToken.current().tokenString {
facebookToken = controlFacebookToken
}
loginWithFacebook(email: email, firstName: firstName, lastName: lastName, facebookId: facebookId, accessToken: facebookToken, finishedClosured: { (state) in
if state {
let mainPage = self.mainStoryBoard.instantiateViewController(withIdentifier: "SWRevealViewController")
self.removeLoadingAlert()
self.passPage(page: mainPage)
return
}
self.removeLoadingAlert()
self.alert(message: "Giris Basarisiz")
return
})
}
} else {
print(error?.localizedDescription)
}
})
}
我需要获取人员的电子邮件才能将用户注册到我的应用程序。但是无论我尝试什么,电子邮件都没有。我该如何解决这个问题,如果我尝试使用我的个人帐户没问题,但其他用户可能会出现问题。
Best Answer-推荐答案 strong>
Note : This is in swift 4.0
//giving permission
func getpermission(){
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
// fbLoginManager.loginBehavior = FBSDKLoginBehavior.web
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil {
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
}
}
}
// Get all details from here
func getFBUserData(){
var fbId : String = ""
var fbEmail : String = ""
var fbName : String = ""
var fbPickUrl : String = ""
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print("Result111:\(String(describing: result)) "as Any)
}
let dict = result as! NSDictionary
print("FB Email1st:\(dict)")
fbId = dict["id"] as! String
fbName = dict["name"] as! String
fbEmail = dict["email"] as! String
//get user picture url from dictionary
fbPickUrl = (((dict["picture"] as? [String: Any])?["data"] as? [String:Any])?["url"] as? String)!
print("FB ID: \(fbId)\n FB Email:\(fbEmail) \n FbName:\(fbName) \n FBProfileUrl:\(fbPickUrl)\n")
})
}
}
关于ios - facebook登录电子邮件不会很快(没有任何解决方案吗?),我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/47147596/
|