Download file in iPhone Device

74 Views Asked by At

I have a file URL and want to download it in my iPhone in document directory. So that I can use this downloaded file within other application like, share it on whatsApp, in email attachment etc.

I am using ASIHTTPRequest, what Destination path I should set?

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.ImageURL];
[request setDownloadDestinationPath:@"What Path I should Set"];
[request startSynchronous];

Here is a sample url of file which I want to download: http://www.fourspan.com/apps/agentreview/public/img/ads/1441791507_Muhammad.jpg

1

There are 1 best solutions below

0
Nishant On

Firstly, ASIHTTPRequest is obsolete, please avoid using it.

Here is the code using AFNetworking to download any file and save it to any location you want (savePath)

#define kDocumentsDirPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

- (void)downloadFile:(NSString *)filePath
{

AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:filePath]];

NSURLSessionDownloadTask *downloadTask = [sessionManager downloadTaskWithRequest:request
                                                                        progress:nil
                                                                     destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

                                                                         NSString *savePath = [kDocumentsDirPath stringByAppendingPathComponent:fileName];

                                                                         return [NSURL fileURLWithPath:savePath];

                                                                     }
                                                               completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

                                                                   if (error)
                                                                   {
                                                                       NSLog(@"Download failed for %@: %@", fileName, error.localizedDescription);

                                                                       [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Failed to download %@", fileName]
                                                                                                  message:error.localizedDescription
                                                                                                 delegate:nil
                                                                                        cancelButtonTitle:@"Uh Oh"
                                                                                         otherButtonTitles:nil] show];
                                                                   }
                                                                   else {

                                                                       NSLog(@"File downloaded: %@", fileName);
                                                                   }
                                                               }];
[downloadTask resume];
}