iphone: http connection

14

Click here to load reader

Upload: jussi-pohjolainen

Post on 06-May-2015

10.514 views

Category:

Education


1 download

TRANSCRIPT

Page 1: iPhone: Http Connection

iPhone  Development:  H"p  Connec*on  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: iPhone: Http Connection

H>p  Connec@on  

•  NSUrlConnection – Easiest  way  to  download  the  contents  of  an  URL  

•  Crea@ng  connec@on  •  Canceling  connec@on  •  Collec@on  of  delegate  methods  for  controlling  the  connec@on  

Page 3: iPhone: Http Connection

Crea@ng  a  Connec@on  // Create the request NSURLRequest *theRequest= [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.tamk.fi"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0]; // Create the connection with the request // and start loading the data NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if ( theConnection ) { // Create the NSMutableData that will hold // the received data (A wrapper for byte buffer) // receivedData is declared as a method instance elsewhere . // Creates empty data wrapper. receivedData = [ [NSMutableData data] retain ]; } else { // inform the user that the download could not be made }

Page 4: iPhone: Http Connection

Delegate?  // Create the connection with the request

// and start loading the data

NSURLConnection *theConnection=

[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

// Now "self" must conform to the delegate methods that are:

// - (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse *)response

// - (void) connection:(NSURLConnection*) connection didFailWithError:(NSError *) error

// - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data

// - (void) connectionDidFinishLoading:(NSURLConnection *) connection

 // These methods are are now called automatically!

Page 5: iPhone: Http Connection

URLConnec@on  class  @interface URLConnection : NSObject

{

// Contents of the URL

NSMutableData* receivedData;

NSURLConnection* theConnection;

}

// Make the connection

- (void) connect: (NSString* url);

// Cancel the connection

- (void) cancel;

// Delegate methods

- (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse*)response

- (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error

- (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data

- (void) connectionDidFinishLoading:(NSURLConnection*) connection

Page 6: iPhone: Http Connection

Connec@ng  - (void) connect:(NSString*) url

{

// create the request

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]

cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:60.0];

// create the connection with the request

// and start loading the data

self.theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )

{

receivedData=[[NSMutableData data] retain];

}

else

{

// Connection failed

// Give error message

}

}

Page 7: iPhone: Http Connection

Receiving  data  - (void) connection:(NSURLConnection*) connection

didReceiveData:(NSData*) data

{

[receivedData appendData:data];

}

Page 8: iPhone: Http Connection

When  connec@on  is  finished  - (void) connectionDidFinishLoading:(NSURLConnection *) connection { // Create byte array from the received data unsigned char byteBuffer[[receivedData length]]; [receivedData getBytes:byteBuffer]; NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:[receivedData length] encoding:4]; // Do something with the data.. For example // make a call back method for the viewController [temp release]; [connection release]; [receivedData release]; self.theConnection = nil; }

Page 9: iPhone: Http Connection

When  failes..  - (void) connection:(NSURLConnection *) connection

didFailWithError:(NSError *) error

{

// Error handling

}

Page 10: iPhone: Http Connection

Canceling  - (void) cancel

{

if ( self.theConnection )

{

[self.theConnection cancel];

[self.theConnection release];

}

}

Page 11: iPhone: Http Connection

Usage  [URLConnection *myConn = [[URLConnection alloc] init];

[myConn connect:@"http://www.tamk.fi"];

Page 12: iPhone: Http Connection

CallBack  Methods?  @interface URLConnection : NSObject { // Contents of the URL NSMutableData* receivedData; NSURLConnection* theConnection; MyViewController* myViewController; } - (id) initWithHost:(MyViewController*) host { self = [super init]; self.myViewController = host; self.theConnection = nil; return self; } // Make the connection - (void) connect: (NSString* url); // Cancel the connection - (void) cancel; // Delegate methods - (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse*)response - (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data - (void) connectionDidFinishLoading:(NSURLConnection*) connection

Page 13: iPhone: Http Connection

Usage  // We are now in some ViewController class

[URLConnection *myConn = [[URLConnection alloc] initWithHost: self];

[myConn connect:@"http://www.tamk.fi"];

Page 14: iPhone: Http Connection

When  connec@on  is  finished  - (void) connectionDidFinishLoading:(NSURLConnection *) connection { unsigned char byteBuffer[[receivedData length]]; [receivedData getBytes:byteBuffer]; NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:[receivedData length] encoding:4]; // Do something with the data.. For example // make a call back method for the viewController [myViewController setThisTextToTextView: temp]; [temp release]; [connection release]; [receivedData release]; self.theConnection = nil; }