NKNetworkKit 0.2.7

NKNetworkKit 0.2.7

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2015
SPMSupports SPM

Maintained by prcela.



NKNetworkKit

NetworkKit is the swift library that hides some complexity of doing network request.

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.