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