Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. let request = NSMutableURLRequest(URL: NSURL(string: address)!)
  2. request.HTTPMethod = "POST"
  3. let postString = "username=" + idStr! + "&password=" + pwStr!
  4. request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
  5.  
  6. let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {data, response, error in
  7. if (error == nil) {
  8. do {
  9. let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
  10.  
  11. if (result["fields"] != nil && result["fields"] as? NSNumber == 1) {
  12. if (result["exist"] != nil && result["exist"] as? NSNumber == 1) {
  13. if (result["success"] != nil && result["success"] as? NSNumber == 1) {
  14. dispatch_async(dispatch_get_main_queue(), { ()->Void in
  15. let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
  16. prefs.setBool(true, forKey: "isLoggedIn")
  17. prefs.setValue(self.idInput.text!, forKey: "username")
  18. prefs.setValue(result.valueForKey("name") as! String, forKey: "name")
  19. prefs.setValue(result.valueForKey("birth") as! String, forKey: "birth")
  20. prefs.setValue(result.valueForKey("phone") as! String, forKey: "phone")
  21. prefs.setValue(result.valueForKey("email") as! String, forKey: "email")
  22. prefs.setValue(result.valueForKey("church_name") as! String, forKey: "churchName")
  23. prefs.setValue(result.valueForKey("church_type") as! String, forKey: "churchType")
  24. prefs.setValue(result.valueForKey("church_duty") as! String, forKey: "churchDuty")
  25. self.performSegueWithIdentifier("showMain", sender: self)
  26. })
  27.  
  28. } else {
  29. NSOperationQueue.mainQueue().addOperationWithBlock {
  30. let alert = UIAlertController(title: "Login Failed", message: "Wrong password", preferredStyle: UIAlertControllerStyle.Alert)
  31. alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: nil))
  32. self.presentViewController(alert, animated: true, completion: nil)
  33. }
  34. }
  35. } else {
  36. NSOperationQueue.mainQueue().addOperationWithBlock {
  37. let alert = UIAlertController(title: "Login failed", message: "Username is not existed", preferredStyle: UIAlertControllerStyle.Alert)
  38. alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: nil))
  39. self.presentViewController(alert, animated: true, completion: nil)
  40. }
  41. }
  42. } else {
  43. NSOperationQueue.mainQueue().addOperationWithBlock {
  44. let alert = UIAlertController(title: "Login Failed", message: "Connection Failed", preferredStyle: UIAlertControllerStyle.Alert)
  45. alert.addAction(UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: nil))
  46. self.presentViewController(alert, animated: true, completion: nil)
  47. }
  48. }
  49. } catch let error as NSError {
  50. print (error.localizedDescription)
  51. }
  52. } else {
  53. print (error)
  54. }
  55. })
  56. task.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement