4.18.24 Movement Legacies, Reflection, and Review.pptx
iPhone: Http Connection
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 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
}
4. 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!
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;
}
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;
}