TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Mar 2017 |
Maintained by Andy Sherwood.
This extension to NSURLSession gives you typed responses from HTTP requests, both asynchronous and synchronous. Exceptions are thrown if an error occurs.
The following typed responses are supported:
String
[String:AnyObject]
NSData
UIImage
or NSImage
NSXMLParser
Get an NSURLSession
let session = NSURLSession.sharedSession()
Create an NSURLRequest instance for the data you want load
let request = NSURLRequest(URL: NSURL(string: "http://loripsum.net/api/plaintext")!)
Call NSURLSession for the type you are interested in, e.g. "text"
let task = session.textTaskWithRequest(request) { (text:String?, response:NSHTTPURLResponse?, error:NSError?) in
// Do something interesting with text
NSLog("\(text)")
}
task.resume()
let text = try session.awaitTextWithRequest(textRequest)
do {
let request = NSURLRequest(URL: NSURL(string: "http://date.jsontest.com/")!)
let json = try session.awaitJsonWithRequest(request)
let date = json["date"]
NSLog("\(date)")
}
catch let error as NSError {
NSLog("\(error.localizedDescription)")
}
do {
var request = NSMutableURLRequest(URL: NSURL(string: "https://mega.lotto/api")!)
request.HTTPMethod = "POST"
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject([4,8,15,16,23,42], options: [])
try session.awaitVoidWithRequest(request)
print("OK")
}
catch let error as NSError {
NSLog("\(error.localizedDescription)")
}
@objc
class WxParserDelegate : NSObject, NSXMLParserDelegate {
private var _elements:[String] = []
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
_elements.append(elementName)
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
_elements.removeLast()
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
let path = _elements.joinWithSeparator("/")
if (path == "dwml/data/moreWeatherInformation") {
NSLog("\(string)")
}
}
}
do {
var request = NSMutableURLRequest(URL: NSURL(string: "http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?whichClient=NDFDgen&lat=38.99&lon=-77.01")!)
var parser = try session.awaitXmlWithRequest(request)
let delegate = WxParserDelegate()
xmlParser.delegate = delegate
xmlParser.parse()
}
catch let error as NSError {
NSLog("\(error.localizedDescription)")
}
Shoutout to Basem Emara whose blog post got me started: Creating Cross-Platform Swift Frameworks for iOS, watchOS, and tvOS via Carthage and CocoaPods