samedi 9 mai 2015

Xcode 3.6.1 Login Tutorial : JSON no response

I learning IOS and i try to build login app with Xcode 6.3.1, Swift, JSON & PHP. my code :

            var reponseError: NSError?
        var response: NSURLResponse?

        var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

        if ( urlData != nil ) {
            let res = response as! NSHTTPURLResponse;

            NSLog("Response code: %ld", res.statusCode);

            if (res.statusCode >= 200 && res.statusCode < 300)
            {
                var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

                NSLog("Response ==> %@", responseData);

                var error: NSError?

                var jsonData: NSDictionary?
                if let jsonData = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary {

                    let success:NSInteger = jsonData.valueForKey("success") as! NSInteger

                    //[jsonData[@"success"] integerValue];

                    NSLog("Success: %ld", success);

                    if(success == 1)
                    {
                        NSLog("Login SUCCESS");

                        var prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
                        prefs.setObject(username, forKey: "USERNAME")
                        prefs.setInteger(1, forKey: "ISLOGGEDIN")
                        prefs.synchronize()

                        self.dismissViewControllerAnimated(true, completion: nil)
                    } else {
                        var error_msg:NSString

                        if jsonData["error_message"] as? NSString != nil {
                            error_msg = jsonData["error_message"] as! NSString
                        } else {
                            error_msg = "Unknown Error"
                        }
                        var alertView:UIAlertView = UIAlertView()
                        alertView.title = "Sign in Failed!"
                        alertView.message = error_msg as String
                        alertView.delegate = self
                        alertView.addButtonWithTitle("OK")
                        alertView.show()

                    }

                }

            } else {
                var alertView:UIAlertView = UIAlertView()
                alertView.title = "Sign in Failed!"
                alertView.message = "Connection Failed"
                alertView.delegate = self
                alertView.addButtonWithTitle("OK")
                alertView.show()
            }
        } else {
            var alertView:UIAlertView = UIAlertView()
            alertView.title = "Sign in Failed!"
            alertView.message = "Connection Failure"
            if let error = reponseError {
                alertView.message = (error.localizedDescription)
            }
            alertView.delegate = self
            alertView.addButtonWithTitle("OK")
            alertView.show()
        }
    }

But when i run the app with correct Username & password. This block always skipped(false)

var jsonData: NSDictionary?
                if let jsonData = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary {

and this is my PHP code response if username & password match

error_log("User $email: password match.");
        echo '{"success":1}';

please help

Aucun commentaire:

Enregistrer un commentaire