Using NSURLSession and NSURLSessionDataTask in Swift to make a GET request

Make a simple GET request using NSURLSession and NSURLSessionDataTask. Here i’m creating a singleton that has access to ‘network’ struct that has a computed property called session.
//API.swift

public struct network {
    public var session:NSURLSession? {
    get {
        let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
        sessionConfig.HTTPAdditionalHeaders = ["API-KEY": "ABCDEF"];
        return NSURLSession(configuration: sessionConfig)
    }
    }
}

class API {
    class var sharedInstance : API {
    struct Static {
        static let instance : API = API()
        }
        return Static.instance
    }

//name argument isn't used in this method, 

    func apiRequest(name:String, completionblock:(error:NSError, wordInformation:NSDictionary)->()) {
        let session = network().session!;
        let task : NSURLSessionDataTask = session.dataTaskWithURL(NSURL(string:"https://url_here"),completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
            let JSON: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)
            NSLog(JSON!.description)
            return
            })
        task.resume()
    }
}

In your ViewController.swift file or another file that references the API class use this:


API.sharedInstance.apiRequest("name", completionblock: {(error: NSError, responseDictionary: NSDictionary) -> () in
//Set variables and do further processing here
 })

Make a simple GET request using NSURLSession and NSURLSessionDataTask. Here i’m creating a singleton that has access to ‘network’ struct that has a computed property called session.
//API.swift

public struct network {
    public var session:NSURLSession? {
    get {
        let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
        sessionConfig.HTTPAdditionalHeaders = ["API-KEY": "ABCDEF"];
        return NSURLSession(configuration: sessionConfig)
    }
    }
}

class API {
    class var sharedInstance : API {
    struct Static {
        static let instance : API = API()
        }
        return Static.instance
    }

//name argument isn't used in this method, 

    func apiRequest(name:String, completionblock:(error:NSError, wordInformation:NSDictionary)->()) {
        let session = network().session!;
        let task : NSURLSessionDataTask = session.dataTaskWithURL(NSURL(string:"https://url_here"),completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
            let JSON: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)
            NSLog(JSON!.description)
            return
            })
        task.resume()
    }
}

In your ViewController.swift file or another file that references the API class use this:


API.sharedInstance.apiRequest("name", completionblock: {(error: NSError, responseDictionary: NSDictionary) -> () in
//Set variables and do further processing here
 })

Leave a Reply

Your email address will not be published. Required fields are marked *