iPhone	  Development:	         H"p	  Connec*on	               Jussi	  Pohjolainen	  Tampere	  University	  of	  Applied	  ...
H>p	  Connec@on	  •  NSUrlConnection   –  Easiest	  way	  to	  download	  the	  contents	  of	  an	  URL	  •  Crea@ng	  co...
Crea@ng	  a	  Connec@on	  // Create the requestNSURLRequest *theRequest=     [NSURLRequest requestWithURL: [NSURL URLWithS...
Delegate?	  // Create the connection with the request// and start loading the dataNSURLConnection *theConnection=     [[NS...
URLConnec@on	  class	  @interface URLConnection : NSObject{    // Contents of the URL    NSMutableData* receivedData;    N...
Connec@ng	  - (void) connect:(NSString*) url{    // create the request    NSURLRequest *theRequest=[NSURLRequest requestWi...
Receiving	  data	  - (void) connection:(NSURLConnection*) connection   didReceiveData:(NSData*) data{    [receivedData app...
When	  connec@on	  is	  finished	  - (void) connectionDidFinishLoading:(NSURLConnection *) connection{    // Create byte ar...
When	  failes..	  - (void) connection:(NSURLConnection *) connection   didFailWithError:(NSError *) error{   // Error hand...
Canceling	  - (void) cancel{    if ( self.theConnection )    {        [self.theConnection cancel];        [self.theConnect...
Usage	  [URLConnection *myConn = [[URLConnection alloc] init];[myConn connect:@"http://www.tamk.fi"];
CallBack	  Methods?	  @interface URLConnection : NSObject{    // Contents of the URL    NSMutableData* receivedData;    NS...
Usage	  // We are now in some ViewController class[URLConnection *myConn = [[URLConnection alloc] initWithHost: self];[myC...
When	  connec@on	  is	  finished	  - (void) connectionDidFinishLoading:(NSURLConnection *) connection{    unsigned char byt...
Upcoming SlideShare
Loading in...5
×

iPhone: Http Connection

10,578

Published on

Published in: Education, Technology, Design
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
10,578
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
140
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

iPhone: Http Connection

  1. 1. iPhone  Development:   H"p  Connec*on   Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  
  2. 2. 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  
  3. 3. Crea@ng  a  Connec@on  // Create the requestNSURLRequest *theRequest= [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.tamk.fi"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];// Create the connection with the request// and start loading the dataNSURLConnection *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}
  4. 4. Delegate?  // Create the connection with the request// and start loading the dataNSURLConnection *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!
  5. 5. 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
  6. 6. 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 }}
  7. 7. Receiving  data  - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data{ [receivedData appendData:data];}
  8. 8. 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;}
  9. 9. When  failes..  - (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error{ // Error handling}
  10. 10. Canceling  - (void) cancel{ if ( self.theConnection ) { [self.theConnection cancel]; [self.theConnection release]; }}
  11. 11. Usage  [URLConnection *myConn = [[URLConnection alloc] init];[myConn connect:@"http://www.tamk.fi"];
  12. 12. 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
  13. 13. Usage  // We are now in some ViewController class[URLConnection *myConn = [[URLConnection alloc] initWithHost: self];[myConn connect:@"http://www.tamk.fi"];
  14. 14. 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;}
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×