Demo
#import "SDViewController.h" @interface Sdviewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield *username;@ Property (weak, nonatomic) Iboutlet Uitextfield *password; @end @implementation sdviewcontroller/** * Network Request Step * 1, URL OK resource * NSString *urlstrl = @ ""; * Nsurl *url = [Nsurl Urlwithstring:urlstrl]; * * (1) The Get URL contains parameters * (2) The POST URL contains parameters * * 2, build request URLRequest * (1) Get does not need to handle request parameters * URLRequest * (2) Post needs to wrap parameters in the request, specify HTTP method and HTTP data body * nsmutableurlrequest * HttpMethod = @ "POST" * htppbody = include login Binary data for information * 3, send request *!!! In real-world development, all network requests are asynchronous * nsurlconnection Sendasynchronousrequest * In the asynchronous method of request, docking the received data for processing! *///Login button Click Event-(ibaction) Loginbtnclick: (ID) sender{//[self getLoginWithUserName:self.userName.text andpassword: Self.password.text]; [Self PostLoninWithUserName:self.userName.text andPassword:self.password.text];} /** GET request Login */-(void) Getloginwithusername: (NSString *) userName Andpassword: (NSString *) password{ 1, URL--Prepare resources nsstring *URLSTR = [NSString stringwithformat:@ "Http://localhost/login.php?username=%@&password =%@ ", Username,password]; Nsurl *url = [Nsurl urlwithstring:urlstr]; 2, create network request//urlrequest request default is get nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; 3. Send network request//All network requests are using async method [Nsurlconnection sendasynchronousrequest:request Queue:[[nsoperationqueue alloc] init] completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {//1> will return binary data, converted to string (1) The conversion of binary data to a string does not have a direct class method, need Alloc initwithdata//(2) Hint: in the development of the network, if you need to track the specific content returned by the network, the data will often be converted to string output! Convert to string in a way that is often used in debugging! NSString *result = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; NSLog (@ "Result-%@", result); NSString *respond = [NSString stringwithformat:@ "respond-%@", response]; NSLog (@ "Respond-%@", respond); JSON conversion, format is and nsdictionary quick Wrapping methodVery much like//convert JSON to dictionary nsdictionary *dict = [Nsjsonserialization jsonobjectwithdata:data Options:nsjsonreadingmuta Blecontainers Error:null]; NSLog (@ "dict-%@", dict); }]; }/** POST request login */-(void) Postloninwithusername: (NSString *) userName Andpassword: (NSString *) password{//1, URL prep resource NS String *urlstr = @ "http://localhost/login.php"; Nsurl *url = [Nsurl urlwithstring:urlstr]; 2, Request,post method, need to establish a variable request nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod = @ "POST"; Set request mode//2.1 data body nsstring *bodystr = [NSString stringwithformat:@ "username=%@&password=%@", Username,passwor D]; Converts a string into a binary number request. Httpbody = [Bodystr datausingencoding:nsutf8stringencoding]; 3. Send Request [nsurlconnection sendasynchronousrequest:request Queue:[[nsoperationqueue alloc] init] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {//1> will return the binary data, goConverting binary data to strings without a direct class method requires Alloc initwithdata//(2) Hint: When developing a network, if you need to keep track of what the network returns, you will often convert data to string output first! Convert to string in a way that is often used in debugging! NSLog (@ "Response-%@", response); NSString *result = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; NSLog (@ "Result-%@", result); Nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:NULL]; NSLog (@ "dict-%@", dict); Nserror *erro = Connectionerror; NSLog (@ "Erro-%@", Erro); }]; } @end
iOS Network Basics