Get&post Login

Source: Internet
Author: User

1 #import "MJViewController.h"2 3 @interfaceMjviewcontroller ()4@property (Weak, nonatomic) Iboutlet Uitextfield *UserName;5@property (Weak, nonatomic) Iboutlet Uitextfield *userpwd;6 7@property (Weak, nonatomic) Iboutlet UILabel *Logonresult;8 9 @endTen  One @implementationMjviewcontroller A /** - all network requests, unified use asynchronous Request! -   the in future development, if you use a simple get/head request, you can use the Nsurlconnction async method - get check/post increase/put change/delete delete/head -  GET 1> url 2> nsurlrequest 3> nsurlconnction async POST 1> URL 2> nsmuta Bleurlrequest 26. HttpMethod = @ "POST"; Str from Firebug paste directly, or write 28 variable name 1 = value 1& variable Name 2 = value 2 29 30. Httpbody = [Str datausingencoding:nsutf8stringencoding]; 3> nsurlconnction Async +   -  */ the-(ibaction) Userlogon * { $ [self postlogon];Panax Notoginseng } -  the #pragmaMark-post Login +- (void) Postlogon A { the     //1. URL +Nsurl *url = [Nsurl urlwithstring:@"http://localhost/login.php"]; -      $     //2. Request (can change the request) $Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; -     // ? POST -     //The default is a GET request theRequest. HttpMethod =@"POST"; -     //? Data BodyWuyiNSString *str = [NSString stringWithFormat:@"username=%@&password=%@", Self.userName.text, Self.userPwd.text]; the     //converting a string into data -Request. Httpbody =[str datausingencoding:nsutf8stringencoding]; Wu      -     //3. Connect, asynchronous About[Nsurlconnection sendasynchronousrequest:request Queue:[[nsoperationqueue alloc] init] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) { $          -         if(Connectionerror = =Nil) { -             //execute after the network request is over! -             //convert data into a string ANSString *str =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; +              the             //num = 2 -NSLog (@"%@ %@", str, [Nsthread CurrentThread]); $              the             //Update Interface the[[Nsoperationqueue Mainqueue] addoperationwithblock:^{ theSelf.logonResult.text =str; the             }]; -         } in     }]; the      the     //num = 1 AboutNSLog (@"come here%@", [Nsthread CurrentThread]); the } the  the #pragmaMark-get Login +- (void) Getlogon - { the     //1. URLBayiNSString *urlstr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", Self.userName.text, Self.userPwd.text]; the      theNsurl *url =[Nsurl Urlwithstring:urlstr]; -      -     //2. Request theNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; the      the     //3. Connection the     //1> cannot do follow-up work until login is completed! -     //2> Login In progress, you can allow users to do something else will be better! the     //3> allows the login operation to be performed in other threads without blocking the main thread's work the     //4> Conclusion: Landing is also an asynchronous access, the middle need to block live the[Nsurlconnection sendasynchronousrequest:request Queue:[[nsoperationqueue alloc] init] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {94          the         if(Connectionerror = =Nil) { the             //execute after the network request is over! the             //convert data into a string98NSString *str =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; About              -             //num = 2101NSLog (@"%@ %@", str, [Nsthread CurrentThread]);102             103             //Update Interface104[[Nsoperationqueue Mainqueue] addoperationwithblock:^{ theSelf.logonResult.text =@"Login Complete";106             }];107         }108     }];109      the     //num = 1111NSLog (@"come here%@", [Nsthread CurrentThread]); the     113Nsurlresponse *response =Nil; the     //1. Does the &response really understand? the     //2. Error: Why is null, not nil the     //NULL is the c language = 0117     //in C, there is no danger if you point the address of the pointer to 0118     119     //Nil is OC and is an empty object sending messages without problems - //[Response MIMEType];121[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:null];122 }123 124 @end

Get&post Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.