为什么UIAlertController永远不会消失?

时间:2015-10-04 05:07:50

标签: ios iphone facebook swift login

我在我的swift应用程序和facebook登录按钮方法中集成了facebook登录,我有这个

let LOG_IN_URL = NSURL(string: "http://127.0.0.1/pitic_web_service/log_in.php")

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    if let e = error {
        DialogUtil.showSimpleDialog(self, title: "Error iniciar sessión con facebook", message: e.localizedDescription)
    } else if !result.isCancelled {
        let facebookDataRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "first_name, last_name, email"])
        facebookDataRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
            if error != nil {
                DialogUtil.showSimpleDialog(self, title: "Error al obtener información de usuario Facebook", message: error.localizedDescription)
            } else {
                //Log in with facebook
                //let userData = result as! NSDictionary
                let postParams: Dictionary<String, AnyObject> = [
                    JSONKeys.KEY_USER:[
                        JSONKeys.KEY_FACEBOOK_ID: result.valueForKey(JSONKeys.KEY_USER_ID) as! String,
                        JSONKeys.KEY_EMAIL: result.valueForKey(JSONKeys.KEY_EMAIL) as! String,
                        JSONKeys.KEY_FIRST_NAME: result.valueForKey(JSONKeys.KEY_FIRST_NAME) as! String,
                        JSONKeys.KEY_LAST_NAME: result.valueForKey(JSONKeys.KEY_LAST_NAME) as! String
                    ]
                ]
                self.startLogInRequest(postParams)
            }
        })
    }
}   

startLogInRequest方法:

func startLogInRequest(postParams: Dictionary<String, AnyObject>) {
    self.progressDialog = DialogUtil.showProgressDialog(self, title: "Iniciando sesión", message: "Por favor espere...")
    let request = NSMutableURLRequest(URL: LOG_IN_URL!)
    request.HTTPMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    do {
        request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(postParams, options: NSJSONWritingOptions.PrettyPrinted)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
            //Return to main thread
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                if let pd = self.progressDialog {
                    pd.dismissViewControllerAnimated(true, completion: { () -> Void in
                        if let e = error {
                            DialogUtil.showSimpleDialog(self, title: "Error al iniciar sesión", message: e.localizedDescription)
                        } else if let JSONData = data {
                            do {
                                let dictionary: NSDictionary = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                                let error = dictionary.valueForKey(JSONKeys.KEY_ERROR) as! Bool
                                if error {
                                    let errorMessage = dictionary.valueForKey(JSONKeys.KEY_MESSAGE) as! String
                                    DialogUtil.showSimpleDialog(self, title: "Error al iniciar sesión", message: errorMessage)
                                } else {
                                    let defaults = NSUserDefaults.standardUserDefaults()
                                    defaults.setObject(JSONData, forKey: JSONKeys.KEY_USER)
                                    self.performSegueWithIdentifier(self.logInSegueIdentifier, sender: self)
                                }
                            } catch let error as NSError {
                                DialogUtil.showSimpleDialog(self, title: "Error parsing JSON", message: error.localizedDescription)
                            }
                        }
                    })
                }
            })
        })
        task.resume()
    } catch let error as NSError {
        if let pd = self.progressDialog {
            pd.dismissViewControllerAnimated(true, completion: { () -> Void in
                DialogUtil.showSimpleDialog(self, title: "Wrong login params", message: error.localizedDescription)
            })
        }
    }
}
//The method startLogInRequest works fine here
@IBAction func logInButtonPressed() {

    if let emailQuery = self.emailTextField.text {

        if let passwordQuery = self.passwordTextField.text {

            let email = emailQuery.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

            let password = passwordQuery.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

            if !email.isEmpty && !password.isEmpty {
                let postParams: Dictionary<String, AnyObject> = [
                    JSONKeys.KEY_USER:[
                        JSONKeys.KEY_EMAIL:email,
                        JSONKeys.KEY_PASSWORD:password
                    ]
                ]
                self.startLogInRequest(postParams)
            }
        }
    }
}

问题是startLogInRequest方法中显示的progressdialog永远不会消失!并且completionHandler中的方法从未调用过。

0 个答案:

没有答案