iOS network programming--nsconnection synchronous and asynchronous connections

Source: Internet
Author: User

1 //2 //ZFVIEWCONTROLLER.M3 //0628-Forms validation4 //5 //Created by Zfan on 14-6-28.6 //Copyright (c) 2014 Zfan. All rights reserved.7 //8 9 #import "ZFViewController.h"Ten #import "mbprogresshud+mj.h" One  A @interfaceZfviewcontroller () -  -@property (Weak, nonatomic) Iboutlet Uitextfield *Usernametextfield; the@property (Weak, nonatomic) Iboutlet Uitextfield *Passwordtextfield; -  - @end -  + @implementationZfviewcontroller -  +- (void) Viewdidload A { at [Super Viewdidload]; - } -  --(ibaction) Login: (ID) Sender - { -     //Verify that the account and password are legal in     if(Self.userNameTextField.text.length = =0) -     { to[Mbprogresshud ShowError:@"Please enter your account number"]; +         return; -     } the      *     if(Self.passWordTextField.text.length = =0) $     {Panax Notoginseng[Mbprogresshud ShowError:@"Please enter your password"]; -         return; the     } +      A     //Remove account and password theNSString *username =Self.userNameTextField.text; +NSString *password =Self.passWordTextField.text; -      $     //Generate URL $NSString *urlpath = [NSString stringWithFormat:@"http://192.168.1.101:8080/zfserver/login?username=%@&pwd=%@", userName, password]; -Nsurl *url =[Nsurl Urlwithstring:urlpath]; -      the     //Build Connection -Nsurlrequest *urlrequest =[Nsurlrequest Requestwithurl:url];Wuyi      the     //establish a connection and receive return data (synchronous execution) - //NSData *logindata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:nil Error:nil]; Wu      -      About     //establish a connection and receive return data (asynchronous execution) $Nsoperationqueue *queue =[[Nsoperationqueue alloc] init]; -[Nsurlconnection sendasynchronousrequest:urlrequest queue:queue completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) { -NSData *logindata =data; -NSLog (@"%ld", (unsignedLong) logindata.length); A     }]; +      the     //print the returned data length -NSLog (@"%@", self.userNameTextField.text); $NSLog (@"%@", self.passWordTextField.text); the //NSLog (@ "%ld", (unsigned long) logindata.length); the } the  the @end

Key points of knowledge:

1> the full URL path by stitching the string through the NSString class method;

2> through the Nsurlrequest class method, using the URL to establish a nsurlrequest connection instance;

3> establishes the connection and accepts the return data through the Nsconnection class method, which can be implemented synchronously and asynchronously:

3.1> Sync mode

[Nsurlconnection sendsynchronousrequest:urlrequest Returningresponse:nil Error:nil];

This method does not create a new thread, the return value is NSData, and because the thread is not new, the thread will not continue to run down until the method is completed, causing the card to die easily;

3.2> Async Mode

Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];[ Nsurlconnection sendasynchronousrequest:urlrequest queue:queue Completionhandler:^ (NSURLResponse *response,  NSData *data, Nserror **logindata = data; NSLog (@ "%ld"long) logindata.length);}];

This method creates a new thread to accept the return data from the network side, the return value is void, and the statement in block is executed when the data is accepted;

 

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.