Optimus
Optimus

Reputation: 2210

How to save and retrieve files to apps temp folder in ios

I'm new to IOS development. I'm developing an app which involves downloading files and saving that to apps temp folder and I dont know how to do that my current code is given below

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(@"%@",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
    ASIHTTPRequest *saveUrl  = [ASIHTTPRequest requestWithURL:responseArray[i]];
    [saveUrl setDownloadDestinationPath:myString];
    [request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(@"%@",directoryContents);

The response array contain a list of URL for downloading files. I know something wrong with my code but I cant find out that error please help me to solve this problem

Upvotes: 1

Views: 1117

Answers (2)

Optimus
Optimus

Reputation: 2210

I found the solution'

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *htmlFilePath = [documentsDirectory  stringByAppendingPathComponent:fileName];
        [data writeToFile:htmlFilePath atomically:YES];

Upvotes: 2

Shams Ahmed
Shams Ahmed

Reputation: 4513

When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location. If downloadDestinationPath is not set, download data will be stored in memory

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(@"%@",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
    ASIHTTPRequest *saveUrl  = [ASIHTTPRequest requestWithURL:responseArray[i]];
    [saveUrl setDownloadDestinationPath:[myString stringByAppendingPathComponent:[NSString stringWithFormat:@"%i",i ]]; // for each file set a new location inside tmp
    [request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(@"%@",directoryContents);

Upvotes: 0

Related Questions