Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
  2.  
  3. let apiCall = webApi()
  4.  
  5. dispatch_async(dispatch_get_main_queue()) {
  6.  
  7. apiCall.callCheckIsUserLogged(nil, password : self.passwordField.text, email: self.mailField.text){ (ok) in
  8. }
  9. }
  10. return userIsLogged
  11. }
  12.  
  13. typealias successClosure = (success : Bool) -> Void
  14. //Make a call to check if the user exist, server returns json with success or failure
  15. func callCheckIsUserLogged(username: String?, password : String?, email: String?,completed : successClosure){
  16.  
  17. userApiCallUrl = "http://apiurl.com/users/login"
  18.  
  19. let call = asyncCallClass()
  20. call.doAsyncCallWithParams(userApiCallUrl, calltype: "POST", username: username, pass: password, mail: email){ (success) in
  21. completed(success: true)
  22. }
  23.  
  24. }
  25.  
  26. internal typealias completion = (success : Bool) -> Void
  27. private var flagCompletion : Bool = false
  28.  
  29. //Handle async class with this method
  30. //var callType is aditioned everytime an arg is not passed nil.
  31. //callType == 3 it is a call to check if user is logged
  32. //callType == 2 is a call to register a new user
  33. func doAsyncCallWithParams(url : String, calltype : String, username : String?, pass : String?, mail : String?, completed : completion){
  34.  
  35. var callType : Int = 0
  36.  
  37.  
  38. //Set Async Url
  39. setUrl(url)
  40.  
  41. //Set Post Params
  42. if let user : String = username{
  43. self.username = "username=(user)"
  44. callType += 1
  45. }
  46. if let password : String = pass{
  47. self.password = "password=(password)"
  48. callType += 1
  49. }
  50. if let mail : String = mail{
  51. self.email = "email=(mail)"
  52. callType += 1
  53. }
  54.  
  55. //register a new user
  56. if(callType == 3){
  57. paramString = "(self.username)&(self.password)&(self.email)"
  58.  
  59. }
  60. //check if user is logged, send email and password
  61. if(callType == 2){
  62. paramString = "(self.email)&(self.password)"
  63. }
  64.  
  65. //Do call
  66. callWithCompletionHandler { (success) in
  67. self.flagCompletion = true
  68. completed(success: self.flagCompletion)
  69. }
  70. }
  71.  
  72. private typealias completionAsyncCall = (success : Bool) -> Void
  73. private func callWithCompletionHandler(completed : completionAsyncCall){
  74. asyncJson.removeAllObjects()
  75.  
  76. //Set async call params
  77. let request = NSMutableURLRequest(URL: NSURL(string: self.url!)!)
  78. request.HTTPMethod = "POST"
  79. let trimmedPostParam : String = self.paramString!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
  80.  
  81. request.HTTPBody = trimmedPostParam.dataUsingEncoding(NSUTF8StringEncoding)
  82.  
  83. let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
  84. guard error == nil && data != nil else {
  85. // check for fundamental networking error
  86. print("error=(error)")
  87. return
  88. }
  89. if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
  90. // check for http errors
  91. print("statusCode should be 200, but is (httpStatus.statusCode)")
  92. print("response = (response)")
  93. }
  94.  
  95. let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
  96.  
  97.  
  98. let result : AnyObject = responseString!.parseJSONString!
  99.  
  100. if let nsMutableResult = result as? NSMutableArray{
  101. print("NSMutableArray")
  102. }
  103. if let nsDictResult = result as? NSMutableDictionary{
  104. self.parseMutableDictionary(nsDictResult)
  105. }
  106.  
  107.  
  108. self.flag = true // true if download succeed,false otherwise
  109. completed(success: flagAsyncCall!)
  110. }
  111. task.resume()
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement