The network data exchange of most apps in iOS development is based on
HTTPof the agreement. This article will briefly describe several ways to use HTTP for network requests in Swift.
Note: When a network request is completed, a nsdata type of return data is obtained, and if the data format is JSON, you can use the system's own Nsjsonserialization class to parse the data , or use the Swiftyjson library to parse the data using the JSON parsing library mentioned in the article.
1. Use
NSURLConnection
The following example uses NSURLConnection the implementation of a simple asynchronous get operation:
Func Requesturl (urlstring:string) { var url:nsurl = Nsurl (string:urlstring) let request:nsurlrequest = NSURLRe Quest (Url:url) nsurlconnection.sendasynchronousrequest (Request, Queue:NSOperationQueue.mainQueue (), completionhandler:{ (response, data, error), Void in if error? { //handle Error here }else{ //handle data in NSData type }} }
2. Using the Yyhrequest-swift Library
This library encapsulates NSURLConnection and NSOperationQueue implements a simple HTTP Get/post/put/delete operation:
Let request = Yyhrequest (Url:nsurl (string: "http://www.google.com/")) request.loadwithcompletion {response, data, Error in //Request complete!}
3. Use the Swifthttp library.
This library is packaged NSURLSession to provide Get/post/put/delete, as well as upload and download support, which is more comprehensive:
var request = Httptask () request. GET ("Http://vluxe.io", Parameters:nil, Success: {(response:anyobject?), Void in },failure: {(error:nserror)- > Void in })
4. Using the Afnetworking network library in Objective-c
AFNetworkingLibrary is a powerful network request library in the field of iOS development.
- First introduce the Afnetworking library into the project, please refer to this tutorial
- And then in
<ProjectName>-Bridging-Header.hThe afnetworking is introduced in the header file:#import "Afnetworking/afnetworking.h"
The following is a brief demonstration of AFNetworking GET the usage in:
Func Requesturl (urlstring:string) {let manager = Afhttprequestoperationmanager () let params = ["A": 1, "B": 2]< C5/>manager. GET (urlstring, parameters:params, success: {operation:afhttprequestoperation!, responseobject: anyobject!) in Let responsedict = Responseobject as nsdictionary! }, failure: {(operation:afhttprequestoperation!, error:nserror!) In //handle Error }) }
InstanceFinally, I wrote a simple example to get the word interpretation by calling the PowerWord API swift . The project uses it SwiftHTTP as a network request to SwiftyJSON parse JSON data, and the code is hosted on GitHub with the following core code:
Request PowerWord API get word ' swift ' explanation var request = Httptask () var params:dictionary<string, anyobject> = ["W": " Swift "," Key ":" 30cba9ddd34b16db669a9b214c941f14 "," type ":" JSON "] request. GET ("http://dict-co.iciba.com/api/dictionary.php", Parameters:params, Success: {(response:anyobject?), Void in< C3/>let json = Jsonvalue (response!) println ("\ (JSON)") },failure: {(error:nserror), Void in println ("\ (Error)") })
from:http://swiftist.org/topics/135
HTTP requests in Swift