IOS network programming-synchronous and asynchronous connections of NSConnection

Source: Internet
Author: User

1 // 2 // ZFViewController. m 3 // 0628-Form Verification 4 // 5 // Created by zfan on 14-6-28. 6 // Copyright (c) 2014 zfan. all rights reserved. 7 // 8 9 # import "ZFViewController. h "10 # import" MBProgressHUD + MJ. h "11 12 @ interface ZFViewController () 13 14 @ property (weak, nonatomic) IBOutlet UITextField * userNameTextField; 15 @ property (weak, nonatomic) IBOutlet UITextField * passWordTextField; 16 17 @ end18 19 @ I Mplementation ZFViewController20 21-(void) viewDidLoad22 {23 [super viewDidLoad]; 24} 25 26-(IBAction) login :( id) sender27 {28 // verify that the account and password are valid 29 if (self. userNameTextField. text. length = 0) 30 {31 [MBProgressHUD showError: @ "enter an account"]; 32 return; 33} 34 35 if (self. passWordTextField. text. length = 0) 36 {37 [MBProgressHUD showError: @ "Enter Password"]; 38 return; 39} 40 41 // retrieve account and password 42 NSString * userName = self. userNam ETextField. text; 43 NSString * password = self. passWordTextField. text; 44 45 // generated URL46 NSString * urlPath = [NSString stringWithFormat: @ "http: // 192.168.1.101: 8080/ZFServer/login? Username = % @ & pwd = % @ ", userName, password]; 47 NSURL * url = [NSURL URLWithString: urlPath]; 48 49 // generate connection 50 NSURLRequest * urlRequest = [NSURLRequest requestWithURL: url]; 51 52 // establish a connection and receive the returned data (synchronously executed) 53 // NSData * loginData = [NSURLConnection sendSynchronousRequest: urlRequest returningResponse: nil error: nil]; 54 55 56 // establish a connection and receive returned data (asynchronously executed) 57 NSOperationQueue * queue = [[NSOperationQueue alloc] init]; 58 [NSURLConnection failed: urlRequest queue: queue completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {59 NSData * loginData = data; 60 NSLog (@ "% ld", (unsigned long) loginData. length); 61}]; 62 63 // print the returned Data length 64 NSLog (@ "% @", self. userNameTextField. text); 65 NSLog (@ "% @", self. passWordTextField. text); 66 // NSLog (@ "% ld", (unsigned long) loginData. length); 67} 68 69 @ end

Knowledge points:

1> concatenate a string using the NSString method to generate a complete URL path;

2> Create an NSURLRequest connection instance using the URL using the NSURLRequest class method;

3> establish a connection through the NSConnection class method and accept the returned data. It can be implemented through synchronous and asynchronous methods:

 

3.1> synchronization mode

[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];

This method does not create a new thread, and the returned value is NSData. Because no new thread is created, the thread will not continue to run down before the method is completed, which may cause freezing;

 

3.2> asynchronous mode

NSOperationQueue *queue = [[NSOperationQueue alloc] init];[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  NSData *loginData = data;  NSLog(@"%ld", (unsigned long)loginData.length);}];

This method creates a new thread to accept the data returned by the network. The returned value is void. After the data is accepted, the statement in the block is executed;

 

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.