Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let request = NSMutableURLRequest(URL: NSURL(string: address)!)
- request.HTTPMethod = "POST"
- let postString = "username=" + idStr! + "&password=" + pwStr!
- request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
- let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {data, response, error in
- if (error == nil) {
- do {
- let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
- if (result["fields"] != nil && result["fields"] as? NSNumber == 1) {
- if (result["exist"] != nil && result["exist"] as? NSNumber == 1) {
- if (result["success"] != nil && result["success"] as? NSNumber == 1) {
- dispatch_async(dispatch_get_main_queue(), { ()->Void in
- let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
- prefs.setBool(true, forKey: "isLoggedIn")
- prefs.setValue(self.idInput.text!, forKey: "username")
- prefs.setValue(result.valueForKey("name") as! String, forKey: "name")
- prefs.setValue(result.valueForKey("birth") as! String, forKey: "birth")
- prefs.setValue(result.valueForKey("phone") as! String, forKey: "phone")
- prefs.setValue(result.valueForKey("email") as! String, forKey: "email")
- prefs.setValue(result.valueForKey("church_name") as! String, forKey: "churchName")
- prefs.setValue(result.valueForKey("church_type") as! String, forKey: "churchType")
- prefs.setValue(result.valueForKey("church_duty") as! String, forKey: "churchDuty")
- self.performSegueWithIdentifier("showMain", sender: self)
- })
- } else {
- NSOperationQueue.mainQueue().addOperationWithBlock {
- let alert = UIAlertController(title: "Login Failed", message: "Wrong password", preferredStyle: UIAlertControllerStyle.Alert)
- alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)
- }
- }
- } else {
- NSOperationQueue.mainQueue().addOperationWithBlock {
- let alert = UIAlertController(title: "Login failed", message: "Username is not existed", preferredStyle: UIAlertControllerStyle.Alert)
- alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)
- }
- }
- } else {
- NSOperationQueue.mainQueue().addOperationWithBlock {
- let alert = UIAlertController(title: "Login Failed", message: "Connection Failed", preferredStyle: UIAlertControllerStyle.Alert)
- alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)
- }
- }
- } catch let error as NSError {
- print (error.localizedDescription)
- }
- } else {
- print (error)
- }
- })
- task.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement