我花了很多年时间试图弄清楚为什么5%的用户下载我的应用无法成功登录Facebook。
95%的时间表现如预期所有这一切都适合全世界。
但是对于其他5%的应用程序在第一个ViewController页面上崩溃了。然后,当他们重新启动应用程序时,会成功发送到第二页以检查它们是否已存在于数据库中,但Facebook ID为零并显示错误对话框。
我无法调试它,因为我的设备和绝大多数设备似乎没有此错误。
如果有人能够提出问题所在,我将永远感激不尽!
第一页面视图控制器:
import UIKit
class ViewController: UIViewController {
@IBAction func btnFBLoginPressed(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager .logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if result.isCancelled {
return
}
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
//fbLoginManager.logOut()
}
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email, gender, age_range, locale"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
let userName : NSString = result.valueForKey("name") as! NSString
let facebookID : NSString = result.valueForKey("id") as! NSString
let firstName : NSString = result.valueForKey("first_name") as! NSString
let lastName : NSString = result.valueForKey("last_name") as! NSString
let email : NSString = result.valueForKey("email") as! NSString
let gender : NSString = result.valueForKey("gender") as! NSString
let locale : NSString = result.valueForKey("locale") as! NSString
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("\(userName)", forKey: "userName")
defaults.setObject("\(facebookID)", forKey: "facebookID")
defaults.setObject("\(firstName)", forKey: "firstName")
defaults.setObject("\(lastName)", forKey: "lastName")
defaults.setObject("\(email)", forKey: "email")
defaults.setObject("\(gender)", forKey: "gender")
defaults.setObject("\(locale)", forKey: "locale")
}
})
}
}
override func viewDidAppear(animated: Bool) {
if((FBSDKAccessToken.currentAccessToken()) != nil){
print("logged in")
self.performSegueWithIdentifier("login",sender:self)
}else{
print("Not logged in")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
// fbLoginManager.logOut()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
登录后的第二页:
import Foundation
import UIKit
import Parse
import Bolts
class loginViewController: UIViewController {
override func viewDidAppear(animated: Bool)
{
let defaults = NSUserDefaults.standardUserDefaults()
if let facebookID = defaults.stringForKey("facebookID"){
let firstName = defaults.stringForKey("firstName")
let lastName = defaults.stringForKey("lastName")
let email = defaults.stringForKey("email")
let gender = defaults.stringForKey("gender")
// Lots of code that queries database
} else{
let alert = UIAlertView()
alert.title = "Error"
alert.message = "Error Code: 01"
alert.addButtonWithTitle("OK")
alert.show()
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logOut()
self.performSegueWithIdentifier("backToLoginScreen", sender: nil)
}
}
}
答案 0 :(得分:0)
我认为问题是
fbLoginManager .logInWithReadPermissions(["email"], fromViewController: self, handler: { (result, error) -> Void in
因为您只是要求收到电子邮件,但是您将获得身份证,姓名以及许多与电子邮件无关的内容。因此,我会更好地更改此类权限。
fbLoginManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self) { (result, error) -> Void in
希望这会帮助你!