Latest 0.2.7
License MIT
Platforms ios 8.0, requires ARC
Dependencies Reachability

CI Status

Swift library that hides some complexity of doing network requests.


  • Easy syntax for sending request
  • Handling web response in place with closures
  • Posting json data
  • Mechanism for starting and automatic resuming of file download
  • Observe downloading progress ratio

Basic usage

Import this kit into your swift file:

import NKNetworkKit

Simple request:

    let request = NKRequest(host: "")

NKProcessor is adding request to operation queue and processing it asynchronously.

To handle the response, add these closures:

        success: {response in
            let result = response.parsedJsonObject() as! NSDictionary
            NSLog("response: (result)")
        failure: nil,
        finish: nil)

Post some json data:

    let data = NSJSONSerialization.dataWithJSONObject(["text":"example_text"],
    let postRequest = NKRequest(host: "", path: "post", postJsonData: data!)
    NSLog("Sending %@", postRequest.description)
        success: {response in
            let result = response.parsedJsonObject() as! NSDictionary
            NSLog("Result of simple JSON post as dictionary: (result)")
        failure: nil, finish: nil)

Or, download a file:

    let url = NSURL(string: "")
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
    let downloadPath = documentsPath.stringByAppendingPathComponent("simple.pdf")
    NKProcessor.startOrResumeDownloadTaskWithURL(url!, downloadPath: downloadPath, delegateQueue: nil)

Observe the event of completed download:

    let nc = NSNotificationCenter.defaultCenter()
    nc.addObserver(self, selector: "downloadTaskDidFinish:", name: NKNotificationDownloadTaskDidFinish, object: nil)

    func downloadTaskDidFinish(notification: NSNotification)
        let fdi = notification.object as! NKFileDownloadInfo

Inside the info object there is the url of the completed download.

Behind the scene, NKProcessor is keeping session, data and current state of all currently downloading tasks (files).
If network connection becomes offline and online again, the processor will automatically continue with file downloading.

At any moment you can check the progress ratio and task status of downloading file:

    let fdi = NKProcessorInfo.shared.infoForUrl(url)

Note that you can use your own NSOperationQueue when you are willing to download too many files. Your queue is defining rules of max concurent downloads.

One more thing, connect the download progress ratio with your custom class (i.e. derive MyProgressView from UIProgressView):

    if let dfi = NKProcessorInfo.shared.infoForUrl(url!)
        dfi.addObserver(myProgressView, forKeyPath: "downloadRatio", options: NSKeyValueObservingOptions.allZeros, context: nil)

    class MyProgressView: UIProgressView
        override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>)
            let fdi = object as! NKFileDownloadInfo
            dispatch_async(dispatch_get_main_queue()) {
                self.setProgress(fdi.downloadRatio, animated: false)


Swift, minimim iOS 8.0


NKNetworkKit is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "NKNetworkKit"


Krešimir Prcela, [email protected]


NKNetworkKit is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "NKNetworkKit",
    "version": "0.2.7",
    "summary": "Leave the coplexity of network operations behind the scene. In Swift.",
    "description": "The network requests are added to NSOperationQueue so they are evaluated asynchronously.nSimple closures for handling success, failure and finish events.nSimple file download and observing mechanism for downloading progress.",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "prcela": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.2.7"
    "social_media_url": "",
    "platforms": {
        "ios": "8.0"
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "NKNetworkKit": [
    "dependencies": {
        "Reachability": []

Pin It on Pinterest

Share This