Your SlideShare is downloading. ×
iPhone: Http Connection
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

iPhone: Http Connection

10,500
views

Published on

Published in: Education, Technology, Design

1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
10,500
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
138
Comments
1
Likes
8
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. iPhone  Development:   H"p  Connec*on   Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  
  • 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. 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. 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. 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. 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. Receiving  data  - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data{ [receivedData appendData:data];}
  • 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. When  failes..  - (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error{ // Error handling}
  • 10. Canceling  - (void) cancel{ if ( self.theConnection ) { [self.theConnection cancel]; [self.theConnection release]; }}
  • 11. Usage  [URLConnection *myConn = [[URLConnection alloc] init];[myConn connect:@"http://www.tamk.fi"];
  • 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. Usage  // We are now in some ViewController class[URLConnection *myConn = [[URLConnection alloc] initWithHost: self];[myConn connect:@"http://www.tamk.fi"];
  • 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;}