用户使用Facebook登录后应用程序崩溃

时间:2016-02-04 19:56:35

标签: ios swift facebook facebook-ios-sdk nsurl

我把我的应用程序带到了App Store。一切都在我的工作正常,显然是在审稿人的最后。

应用程序上线后,一些用户报告在用Facebook登录后立即崩溃。以下是我认为崩溃的代码(在用户使用Facebook登录后立即运行):

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit

protocol getUserDataDelegate {
    func gotData()
}

public var userEmailForMixpanel = ""
public var userNameForInvites = ""

class GetUserData: NSObject {

var facebookid:String = ""
var userEmail:String = ""
var userFirstName:String = ""
var userLastName: String = ""
var userGender:String = ""
var userBirthday:String = ""
var delegate = getUserDataDelegate?()

func returnUserData() {
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath:  "me", parameters: ["fields": "id, email, first_name, last_name, gender, birthday"])
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            print("Error: \(error)")
        }
        else
        {
            self.facebookid = (result.valueForKey("id") as? String)!
            self.userEmail = (result.valueForKey("email") as? String)!
            self.userFirstName = (result.valueForKey("first_name") as? String)!
            self.userLastName = (result.valueForKey("last_name") as? String)!
            self.userGender = (result.valueForKey("gender") as? String)!
            //self.userBirthday = (result.valueForKey("birthday") as? String)!

            userEmailForMixpanel = self.userEmail
            userNameForInvites = self.userFirstName

            NSUserDefaults.standardUserDefaults().setValue(userEmailForMixpanel, forKey: "userEmail")
            NSUserDefaults.standardUserDefaults().setValue(userNameForInvites, forKey: "userName")
            NSUserDefaults.standardUserDefaults().synchronize()

            Mixpanel.sharedInstanceWithToken("abdc")
            let mixpanel = Mixpanel.sharedInstance()
            mixpanel.registerSuperProperties(["Gender":self.userGender])

            print(self.facebookid)
            print(self.userEmail)
            print(self.userFirstName)
            print(self.userLastName)
            print(self.userGender)
            //print(self.userBirthday)

            self.checkIfUserExists()
        }
    })
}

func checkIfUserExists() {

    showTutorial = true

    let url:NSURL = NSURL(string: "url")!

    let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        let userTokenDataDictionary:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary

        if userTokenDataDictionary ["token"] != nil {
            userAccessToken = (userTokenDataDictionary["token"] as? String)!
            NSUserDefaults.standardUserDefaults().setValue(userAccessToken, forKey: "userAccessToken")
            NSUserDefaults.standardUserDefaults().synchronize()
            print("Token for Existing User:\(userAccessToken)")
            self.finishedGettingData()
        }

        if userTokenDataDictionary ["error"] != nil {
            userAccessToken = (userTokenDataDictionary["error"] as? String)!
            print(userAccessToken)
            print("User needs to be created")
            self.createNewUserFromFacebook()
        }
    }
    task.resume()
}

func createNewUserFromFacebook() {

    let url:NSURL = NSURL(string: "url")!

    print(url)

    let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        let userTokenDataDictionary:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary

        if userTokenDataDictionary ["token"] != nil {
            userAccessToken = (userTokenDataDictionary["token"] as? String)!
            NSUserDefaults.standardUserDefaults().setValue(userAccessToken, forKey: "userAccessToken")
            NSUserDefaults.standardUserDefaults().synchronize()
        }

        if userTokenDataDictionary ["error"] != nil {
            userAccessToken = (userTokenDataDictionary["error"] as? String)!
            print(userAccessToken)
        }

        print("Token for New User:\(userAccessToken)")
        self.finishedGettingData()
    }
    task.resume()
}

func checkIfUserHasUsedListenerApp() {

    let accessToken = NSUserDefaults.standardUserDefaults().stringForKey("userAccessToken")!

    let url:NSURL = NSURL(string: "url")!

    print(url)

    let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        let adDataDict:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary

        if adDataDict ["used_ListenerApp"] != nil {
            let responseCode = adDataDict.valueForKey("used_ListenerApp") as! Bool
            print(responseCode)
            if responseCode == false {
                Mixpanel.sharedInstanceWithToken("abc")
                let mixpanel = Mixpanel.sharedInstance()
                mixpanel.track("New User Signed Up", properties: ["distinct_id":userEmailForMixpanel])
            }
            if responseCode == true {
                return
            }
        }
    }
    task.resume()
}

func finishedGettingData() {
    self.checkIfUserHasUsedListenerApp()

    Mixpanel.sharedInstanceWithToken("abc")
    let mixpanel = Mixpanel.sharedInstance()
    mixpanel.track("User Logged In", properties: ["distinct_id":userEmailForMixpanel])

    if let actualdelegate = self.delegate {
        actualdelegate.gotData()
    }
  }
}

它只会让一些用户崩溃,而不是全部。我甚至尝试创建一个循环,生成一堆用户数据以通过此代码运行它,我无法复制崩溃。

任何建议都会受到很多赞赏。

更新

看起来它与Facebook没有返回电子邮件地址有关。我会继续看。

1 个答案:

答案 0 :(得分:2)

我明白了。部分问题是Facebook没有为某些用户返回电子邮件地址,所以我检查了一下,如果它没有返回电子邮件,我创建了一个带有他们的Facebook ID来让他们完成登录过程(fbid @ facebook.mywebsite.com)。

问题的另一部分是一些用户使用也分配给他们的Facebook帐户的电子邮件地址登录我的网站,并尝试使用Facebook在应用程序上登录。我通过在授权过程中将其Facebook信息与现有帐户合并来修复此问题,如果找到现有帐户的话。