Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
- let apiCall = webApi()
- dispatch_async(dispatch_get_main_queue()) {
- apiCall.callCheckIsUserLogged(nil, password : self.passwordField.text, email: self.mailField.text){ (ok) in
- }
- }
- return userIsLogged
- }
- typealias successClosure = (success : Bool) -> Void
- //Make a call to check if the user exist, server returns json with success or failure
- func callCheckIsUserLogged(username: String?, password : String?, email: String?,completed : successClosure){
- userApiCallUrl = "http://apiurl.com/users/login"
- let call = asyncCallClass()
- call.doAsyncCallWithParams(userApiCallUrl, calltype: "POST", username: username, pass: password, mail: email){ (success) in
- completed(success: true)
- }
- }
- internal typealias completion = (success : Bool) -> Void
- private var flagCompletion : Bool = false
- //Handle async class with this method
- //var callType is aditioned everytime an arg is not passed nil.
- //callType == 3 it is a call to check if user is logged
- //callType == 2 is a call to register a new user
- func doAsyncCallWithParams(url : String, calltype : String, username : String?, pass : String?, mail : String?, completed : completion){
- var callType : Int = 0
- //Set Async Url
- setUrl(url)
- //Set Post Params
- if let user : String = username{
- self.username = "username=(user)"
- callType += 1
- }
- if let password : String = pass{
- self.password = "password=(password)"
- callType += 1
- }
- if let mail : String = mail{
- self.email = "email=(mail)"
- callType += 1
- }
- //register a new user
- if(callType == 3){
- paramString = "(self.username)&(self.password)&(self.email)"
- }
- //check if user is logged, send email and password
- if(callType == 2){
- paramString = "(self.email)&(self.password)"
- }
- //Do call
- callWithCompletionHandler { (success) in
- self.flagCompletion = true
- completed(success: self.flagCompletion)
- }
- }
- private typealias completionAsyncCall = (success : Bool) -> Void
- private func callWithCompletionHandler(completed : completionAsyncCall){
- asyncJson.removeAllObjects()
- //Set async call params
- let request = NSMutableURLRequest(URL: NSURL(string: self.url!)!)
- request.HTTPMethod = "POST"
- let trimmedPostParam : String = self.paramString!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
- request.HTTPBody = trimmedPostParam.dataUsingEncoding(NSUTF8StringEncoding)
- let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
- guard error == nil && data != nil else {
- // check for fundamental networking error
- print("error=(error)")
- return
- }
- if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
- // check for http errors
- print("statusCode should be 200, but is (httpStatus.statusCode)")
- print("response = (response)")
- }
- let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
- let result : AnyObject = responseString!.parseJSONString!
- if let nsMutableResult = result as? NSMutableArray{
- print("NSMutableArray")
- }
- if let nsDictResult = result as? NSMutableDictionary{
- self.parseMutableDictionary(nsDictResult)
- }
- self.flag = true // true if download succeed,false otherwise
- completed(success: flagAsyncCall!)
- }
- task.resume()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement