NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.thaicreate.com/url/string.php"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
// เมื่อเริ่มทำงาน - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { receivedData = [[NSMutableData alloc] init]; } // เมื่อเริ่มรับค่าที่ Server ทำการ Response กลับมา - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; } // เมื่อการทำงานผิดพลาด - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Connection failed! Error - %@", [error localizedDescription]); } // เมื่อการทำงานเสร็จสิ้น - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if(receivedData) { // } }
<?php echo "This string from server!"; ?>
// // ViewController.h // NSURLConnectionApp // // Created by Weerachai on 12/8/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UILabel *lblString; } @property(nonatomic,assign) NSMutableData *receivedData; @end
// // ViewController.m // NSURLConnectionApp // // Created by Weerachai on 12/8/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize receivedData; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.thaicreate.com/url/string.php"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { self.receivedData = nil; } else { UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [connectFailMessage show]; [connectFailMessage release]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { receivedData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connection release]; [receivedData release]; // inform the user UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil]; [didFailWithErrorMessage show]; [didFailWithErrorMessage release]; //inform the user NSLog(@"Connection failed! Error - %@", [error localizedDescription]); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if(receivedData) { NSLog(@"%@",receivedData); NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]; [lblString setText:dataString]; NSLog(@"%@",dataString); } // release the connection, and the data object [connection release]; [receivedData release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [lblString release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท