swiftoption-typeparse-serverforced-unwrapping

Swift: Parse server login facebook impossible to retrieve the mail


I do not understand why I can not retrieve the email of the user.

I receive the error

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I managed to retrieve all data except the mail ... see my code:

if let dict = result as? Dictionary<String, AnyObject>{
    let userName: String = dict["name"] as AnyObject? as! String
    let facebookID: String = dict["id"] as AnyObject? as! String             
    let email: String = dict["email"] as AnyObject? as! String

    let user = PFUser.current()!
    let pictureURL = "https://graph.facebook.com/\(facebookID)/picture?type=large&return_ssl_resources=1"

    let group = DispatchGroup()
    group.enter()
    DispatchQueue.main.async {
        let imageData = NSData.init(contentsOf: NSURL(string: pictureURL)! as URL)
        if let picture = PFFile(name:"pic.png", data: imageData! as Data) {
            user["profilePicture"] = picture
            user.saveInBackground()
        }
        group.leave()
    }

    user["username"] = userName
    //user["email"] = email

    user.saveInBackground()

    self.performSegue(withIdentifier: "segueToFeedControllerF", sender: nil)
}

Solution

  • btn_Facebook.addTarget(self, action: #selector(handleCustomFBLogin), for: .touchUpInside)

    @objc func handleCustomFBLogin(sender:UIButton!){
        FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, err) in
            if(err != nil){
                print("Custom FB Login Failed")
                return
            }
            //print(result?.token.tokenString)
            self.showEmailAddress()
        }
    }
    
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!){
        if(error != nil){
            print(error)
            return
        }
        print("Successfully Logged in using facebook")
        showEmailAddress()
    }
    
    func showEmailAddress(){
        let accesstoken = FBSDKAccessToken.current();
        guard let accessTokenString = accesstoken?.tokenString else {return}
    
        FBSDKGraphRequest(graphPath: "/me", parameters: ["fields" : "id, name, first_name, last_name, email, birthday, picture"]).start { (connection, result, err) in
            if(err != nil){
                print("Failed to start GraphRequest", err ?? "")
                return
            }
            print(result ?? "")
            if(result != nil){
                self.sendDetailsForFacebookLogin(result: result as! NSDictionary)
            }else{
                MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: StringClass.sharedInstance.lcStr_loginfailedcaps, myMessage: StringClass.sharedInstance.lcStr_plsTryAgain)
            }
    
        }
    
    }
    
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!){
        print("Logged out of Facebook")
    }
    
    func sendDetailsForFacebookLogin(result:NSDictionary){
        print(result)
    }