I used http://www.raywenderlich.com/85080/beginning-alamofire-tutorial to add the Alamofire framework.
For SwiftyJSON I just dropped the .swift file in my project.
| import Alamofire | |
| class Something { | |
| var postCancelled: Bool = false | |
| func postToIp(ip: String) { | |
| let parameters = [ | |
| "a": "value 1", | |
| "b": "value 2", | |
| ] | |
| Alamofire.request(.POST, "http://\(ip)/api", parameters: parameters, encoding: .JSON) | |
| .responseJSON({ (_, _, jsondata, _) in | |
| let jsondata = JSON(jsondata!) // this is SwiftyJSON | |
| if let success = jsondata["success"].dictionary { | |
| println("test succeeded") | |
| } else { | |
| if (!self.postCancelled) { | |
| println("retry after 1 second") | |
| let delayInSeconds = 1.0 | |
| let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC))) | |
| dispatch_after(delay, dispatch_get_main_queue()) { | |
| println("retrying...") | |
| self.postToIp(ip) | |
| } | |
| } | |
| } | |
| }) | |
| } | |
| } |
I used http://www.raywenderlich.com/85080/beginning-alamofire-tutorial to add the Alamofire framework.
For SwiftyJSON I just dropped the .swift file in my project.