0

Download image or mp3 files from Web to iPhone document directory

Hi everyone

I am using this code to download files from my Web site

First, i check if file exist

- (void)checkFile
{

    image_path = @"background2.jpg";

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];

    NSString *myFilePath = [libraryDirectory stringByAppendingPathComponent:image_path];

    BOOL fileExists = [fileManager fileExistsAtPath:myFilePath];

    if (fileExists) {
        NSLog(@"file exist");
        self.myImage.image = [UIImage imageWithContentsOfFile:myFilePath];

    } else {
        NSLog(@"file not exist");

        [self downloadImageFile:@"http://www.alhikmeh.org/images/background2.jpg" newFileName:image_path];



}

This code for download:

- (void)downloadImageFile:(NSString *)path newFileName:(NSString *)newFileName
{
    NSURL *URL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    NSData *data = [NSData dataWithContentsOfURL:URL];

    if (data == nil) {
        NSLog(@"error");

    }
    NSString *appDocDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *storePath = [appDocDir stringByAppendingPathComponent:newFileName];
    BOOL success = [data writeToFile:storePath atomically:YES];

    if (success) {
        NSLog(@"success");
    } else {
        NSLog(@"not copied");
    }


}

The log print 'success' but not copy the file to document Directory!!

Almudhafar
  • 887
  • 1
  • 12
  • 26

2 Answers2

2

Let's try: (see my comment)

- (void)downloadImageFile:(NSString *)path newFileName:(NSString *)newFileName
{
NSURL *URL = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSData *data = [NSData dataWithContentsOfURL:URL];

if (data == nil) {
    NSLog(@"error");

}
NSString *appDocDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [appDocDir stringByAppendingPathComponent:newFileName];
// My comment : make sure that you check whether this file exists at above path???
// I run this code, and it can save data.
NSlog(@"%@",storePath);
BOOL success = [data writeToFile:storePath atomically:YES];

if (success) {
    NSLog(@"success");
} else {
    NSLog(@"not copied");
}
}
nmh
  • 2,497
  • 1
  • 15
  • 27
  • its my fault, load file from `NSLibraryDirectory` and save to `NSDocumentDirectory`, thanks – Almudhafar Apr 22 '14 at 05:42
  • Can i make a progress bar to this download? – Almudhafar Apr 22 '14 at 06:54
  • Of course, you should download in background thread. While downloading, you calculating the size to show in the progress bar. http://stackoverflow.com/questions/16453655/objective-c-downloading-file-with-progress-bar – nmh Apr 22 '14 at 07:11
1

I got sucessed by using your code,check your document directory.The path is

/Users/userName/Library/Application Support/iPhone Simulator/7.1/Applications/your project/Documents/background2.jpg
jack903652
  • 11
  • 2