Use one of the following methods to create a Net instance
12345
// without baseURLletnet=Net()// with baseURLletnet=Net(baseUrlString:"http://www.puqiz.com/")
HttpRequest
GET Request
123456789101112131415161718
leturl="get_path"letparams=["integerNumber":1,"doubleNumber":2.0,"string":"hello"]net.GET(url,params:params,successHandler:{responseDatainletresult=responseData.json(error:nil)NSLog("result \(result)")},failureHandler:{errorinNSLog("Error")})// you can also make a request with absolute urlleturl="http://www.puqiz.com/get_path"net.GET(absoluteUrl:url,params:params,successHandler:{responseDatainletresult=responseData.json(error:nil)NSLog("result \(result)")},failureHandler:{errorinNSLog("Error")})
By using responseData in sucessHandler closure you can quickly
get json dictionary
get image
parse xml
for GET, POST, PUT, DELETE request.
12345678
// get json dictionary from response dataletjsonDic=responseData.json(error:error)// get image from response dataletimage=responseData.image()// parse xml with delegateletresult=responseData.parseXml(delegate:self)
POST Request
Net will automatically check your params to send request as a URL-Encoded request or a Multi-Part request. So you can easily post with number, string, image or binary data.
Before using download/upload function you have to call setupSession method to setup the session.
12
// setup session without backgroundIdentifiernet.setupSession()
To perform background downloads or uploads, you have to call setupSession method with a background identifier string. Then your download/upload tasks can be run even when the app is suspended, exits or crashes.
123456789101112
// setup session with backgroundIdentifiernet.setupSession(backgroundIdentifier:"com.nghialv.download")// you can set eventsForBackgroundHandler closure// this closure will be invoked when a task is completed in the backgroundnet.eventsForBackgroundHandler={urlSessioninurlSession.getDownloadingTasksCount{downloadingTaskCountinifdownloadingTaskCount==0{NSLog("All files have been downloaded!")}}}
Download
123456789101112131415
letdownloadTask=net.download(absoluteUrl:url,progress:{progressinNSLog("progress \(progress)")},completionHandler:{fileUrl,erroriniferror!=nil{NSLog("Download failed")}else{NSLog("Downloaded to : \(fileUrl)")}})// you can control your taskdownloadTask.resume()downloadTask.suspend()downloadTask.cancel()