Build an Apache server and write a script back to the client requesting data in PHP
1<?PHP2 //header ("Content-type:text/html;charset=utf-8");3 Header("Content-type:text/json;charset=utf-8");4 if(isset($_request[' Name '])) {5 $result=$_request[' Name '];6 $arr=Array("Result" =$result);7 $json=json_encode ($arr);8 Echo $json;9 }Ten?>
The PHP implementation receives a remote post or GET request from a client and returns it to a simple JSON string.
Open Xcode to create a Singleview project, add a button and a label to IB and wire it to the outlet in the code, the address of my test php file is:
Let URL = "http://192.168.1.106/apprequesttest/index.php"
The following describes two ways to implement a network request, adding a network request to the button's action event
The IOS SDK comes with a network request method:
Get Request Method:
1var msg ="Jimmy"2Nsurlconnection.sendasynchronousrequest (Nsurlrequest (Url:nsurl (string:"\ (URL)? name=\ (msg)"), Queue:nsoperationqueue ()) {(res:nsurlresponse!, data:nsdata!, error:nserror!)-Voidinch3 ifLet d =data{4var getmsg = NSString (data:d, encoding:nsutf8stringencoding)! asString5Dispatch_async (Dispatch_get_main_queue (), {(), Voidinch6Self.label.text =getmsg7 })8 }9}
Post Request Method:
1var req = nsmutableurlrequest (Url:nsurl (string: URL)!)2Req. HttpMethod ="POST"3Req. Httpbody = NSString (string:"Name=jimmy"). datausingencoding (nsutf8stringencoding)4Nsurlconnection.sendasynchronousrequest (req, Queue:nsoperationqueue ()) {(_, data, _)-Voidinch5 ifLet d =data{6var postmsg =NSString (data:d, encoding:nsutf8stringencoding)7Dispatch_async (Dispatch_get_main_queue (), {(), Voidinch8Self.label.text = postmsg! asString9 })Ten } One}
Third-party Open Source Library Alamofire:
Alamofire is a third-party open source Library in Swift network programming that replaces Afnetworking, and the author is the same person who uses Alamofire to download the library on GitHub and then introduce it into the project, as shown in:
Drag the alamofire.xcodeproj from the unpacking package to the project name
Add the framework of the tail of the Red arrows above in the embedded binaries, and be careful not to add the following framework, although the two names are the same, but the above is the iOS library, the following is the OSX library, click the Add button to run the program, to ensure that the compilation is successful.
We can use it in our engineering files by drinking it into the alamofire.
1Alamofire.request (. POST, URL, parameters: ["name":"Jimmy"]). Responsejson (options:NSJSONReadingOptions.AllowFragments) {(req, _, JSON, _) Voidinch2 //println (req. urlstring)3 //println (JSON?. Valueforkey ("result"))4Self.label.text = json?. Valueforkey ("result") as?String5 6}
Through the alamofire can only need a few lines of code to complete the network data request, the above parameters is to send a POST request to PHP, the request name is named, the value is "Jimmy", so that the label will quickly display the returned JSON data, Since Alamofire is an asynchronous request, you do not have to jump to the main thread when updating the UI, as in the first method, and in Alamofire, the request can do whatever the UI does in the required function if it is returned correctly.
Swift Network data request method