I was on vacation this past week and during some of my down
time I started looking for a good networking library, like AFNetworking,
written in Swift but I was unable to find anything mature enough to work with. I know I could use any Objective-C library
with Mix and Match but I wanted something I could use natively with Swift apps. Therefore after all the searching I decided to write
my own, it is called RSNetworking. You can find the GitHub repository here: https://github.com/hoffmanjon/RSNetworking
RSNetworking is definitely not ready for a production app,
after all I just started writing it a couple of nights ago but all good
libraries have to start somewhere. Right
now the library consists on one simple file that contains the various
functions. As the library expands we
will be breaking the functions into separate files but not sure how the library
will expand yet. I would welcome anyone
that would like to contribute to this project or any suggestions that anyone
may have.
* func dataFromURL(url:
NSURL, completionHandler handler: RSNetworking.dataFromURLCompletionClosure): Retrieves an NSData object from the URL
passed in. This is the main function and
is used by the other three functions to retrieve an NSData object prior to
converting it to the required format
* func stringFromURL(url:
NSURL, completionHandler handler:
RSNetworking.stringFromURLCompletionClosure): Retrieves an NSString object from the URL
passed in. This function uses the dataFromURL
function to retrieve an NSData object and then converts it to an NSString
object.
* func dictionaryFromJsonURL(url:
NSURL, completionHandler handler:
RSNetworking.dictionaryFromURLCompletionClosure): Retrieves an NSDictionary object from the URL
passed in. This function uses the dataFromURL
function to retrieve an NSData object and then converts it to an NSDictionary
object. The data returned from the URL
should be in JSON format for this function to work properly.
* func imageFromURL(url:
NSURL, completionHandler handler:
RSNetworking.imageFromURLCompletionClosure): Retrieves an UIImage object from the
URL. This function uses the dataFromURL
function to retrieve an NSData object and then converts it to an UIImage
object.
Lets look at how we would use the RSNetworking library. Here is a sample of how we would call Apple’s
iTunes search API:
var client =
RSNetworking()
var testURL = NSURL.URLWithString("https://itunes.apple.com/search?term=jimmy+buffett&media=music")
client.dictionaryFromJsonURL(testURL,
completionHandler: {(response : NSURLResponse!, responseDictionary:
NSDictionary!, error: NSError!) -> Void in
if !error? {
println("Response Dictionary:
\(responseDictionary)")
} else {
println("Error : \(error)")
}
})
In this sample we begin by initializing RSNetworking and then
we create a NSURL object with the URL of the service we wish to access. We use the dictionaryFromJsonURL
function to make our request to the iTunes search API. When it returns the block is then
executed. Within the block we check for
errors. If there are no errors we log
the dictionary however if there is an error we log the error.
You can download and try RSNetworking here: https://github.com/hoffmanjon/RSNetworking. You can also find the latest information on
RSNetworking that thegithub URL. I have
included a sample project with the library that downloads and displays the data
returned from the iTunes search above in a UITableView. The sample also retrieves images and put them
in an in-memory cache before displaying them in the UITableView. Please try the library and let me know what
you would like to see added. Also, if
you can, please contribute to RSNetworking.
See update 1 to library here
No comments:
Post a Comment