A Quick & Simple Method
Get file
//Get file on a background thread to stop GUI locking up
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://mydomain.com/comefile.php"]];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
File Received
//***********************************
//***********************************
//********** FILE RECEIVED **********
//***********************************
//***********************************
- (void)fetchedData:(NSData *)responseData
{
}
A Fuller Featured Method
In your .h header file
@interface ###
{
NSMutableData *fileData;
NSURLConnection *connectionInProgress;
}
- (void)DownloadFileFromUrl:(NSString *)fileURL;
In Your .m file
//********************************************
//********************************************
//********** DOWNLOAD FILE FROM URL **********
//********************************************
//********************************************
- (void)DownloadFileFromUrl:(NSString *)fileURL
{
NSLog(@"Get file from URL starting");
NSURL *url = [NSURL URLWithString:fileURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
//Clear any existing connection if there is one
if (connectionInProgress)
{
[connectionInProgress cancel];
[connectionInProgress release];
}
[fileData release];
fileData = [[NSMutableData alloc] init];
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];
}
//*************************************************
//*************************************************
//********** NEXT BLOCK OF DATA RECEIVED **********
//*************************************************
//*************************************************
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[fileData appendData:data];
}
//******************************************
//******************************************
//********** ALL OF DATA RECEIVED **********
//******************************************
//******************************************
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Got file from URL");
//CONVERT TEXT FILE TO STRING
//NSString *SourceString = [[[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding] autorelease];
//NSLog(@"Text file received. Contents = %@", s);
//DISPLAY AN IMAGE FILE
//[MyImageView setImage:[UIImage imageWithData:fileData]];
//RELEASE CONECTION
if (connectionInProgress)
{
[connectionInProgress release];
connectionInProgress = nil;
}
}
//***************************************
//***************************************
//********** CONNECTION FAILED **********
//***************************************
//***************************************
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connectionInProgress release];
connectionInProgress = nil;
[fileData release];
fileData = nil;
NSLog(@"Get file from URL failed");
}
//*****************************
//*****************************
//********** DEALLOC **********
//*****************************
//*****************************
- (void)dealloc
{
[fileData release];
[connectionInProgress release];
[super dealloc];
}