Hi,
Here are part of the codes that I have implemented to search for document directory and URL testing to download and save images into the iphone.
thankx
- (NSString*) documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex: 0];
if(!documentsDirectory)
{
[self logError: @"Documents directory not found!"];
}
else
{
[self info: documentsDirectory];
}
return documentsDirectory;
}
- (BOOL) writeApplicationData: (NSData *)data toDirectory: (NSString *)directory toFile: (NSString *)fileName
{
NSString *appFile = [directory stringByAppendingPathComponent: fileName];
[self showAlertWithTitle: @"File Path" message: appFile];
//return ([data writeToFile: appFile atomically: YES]);
return ([data writeToFile: appFile atomically: NO]);
}
- (void) URLTest
{
NSString *url = @"http://earthquake.usgs.gov/images/globes/-5_130.jpg";
NSURLRequest* theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: url] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];
NSURLConnection* theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self];
if(theConnection)
{
// Create the NSMutableData that will hold the received data
// receivedData is declared as a method instance elsewhere
receivedData = [[NSMutableData data] retain];
[self showAlertWithTitle: @"URL Test" message: @"Download could be made"];
}
else
{
// inform the user that the download could not be made
[self showAlertWithTitle: @"URL Test" message: @"Download could not be made"];
}
}
- (void) connectionDidFinishLoading: (NSURLConnection*)connection
{
// do something with the data
NSString *string = [[NSString alloc] initWithData: receivedData encoding: NSASCIIStringEncoding];
NSLog(@"\n\nData: \n\n%@", string);
if(!receivedData)
{
[self showAlertWithTitle: @"receiveData is nil" message: nil];
}
else
{
BOOL result = NO;
[self info: @"Saving..."];
NSString *appFile = [[self documentsDirectory] stringByAppendingPathComponent: @"savedImageData"];
result = [receivedData writeToFile: appFile atomically: YES]; // Error here
if(result)
{
[self showAlertWithTitle: @"Saving" message: @"Successful"];
}
else
{
[self showAlertWithTitle: @"Saving" message: @"Failed"];
}
}
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);
// release the connection, and the data object
[connection release];
[receivedData release];
[self showAlertWithTitle: @"URL Test" message: @"Did finish loading"];
}