Latest 0.2.7
Homepage https://github.com/prcela/NKNetworkKit
License MIT
Platforms ios 8.0, requires ARC
Dependencies Reachability
Authors

CI Status
Version
License
Platform

Swift library that hides some complexity of doing network requests.

Features

  • 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: "http://ip.jsontest.com")
    NKProcessor.process(request)

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

To handle the response, add these closures:

    NKProcessor.process(request,
        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"],
        options:NSJSONWritingOptions(0),
        error:nil)
    let postRequest = NKRequest(host: "http://httpbin.org", path: "post", postJsonData: data!)
    NSLog("Sending %@", postRequest.description)
    NKProcessor.process(postRequest,
        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: "http://www.virtualmechanics.com/support/tutorials-spinner/Simple.pdf")
    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)
            }
        }
    }

Requirements

Swift, minimim iOS 8.0

Installation

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

pod "NKNetworkKit"

Author

Krešimir Prcela, [email protected]

License

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": "https://github.com/prcela/NKNetworkKit",
    "license": "MIT",
    "authors": {
        "prcela": "[email protected]"
    },
    "source": {
        "git": "https://github.com/prcela/NKNetworkKit.git",
        "tag": "0.2.7"
    },
    "social_media_url": "https://twitter.com/kprcela",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "NKNetworkKit": [
            "Pod/Assets/*.png"
        ]
    },
    "dependencies": {
        "Reachability": []
    }
}

Pin It on Pinterest

Share This