Saravanan
Saravanan

Reputation: 57

Unable to download/read a file in documents path of my ios app

I am using the below code to download a ebook from url and store into my device. While this works in emulator, in my ipad I am getting error saying file is not available. I am not able to read the file in the respective path of the device. please help..

NSString *urls =  [NSString stringWithFormat: @"http://hungerin.com/?download_file=%@&order=%@&email=%@&key=%@", BookID, orderKey, email, downloadId];

                    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: urls]];

                    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@""]];

                    NSString *filePathAppend = [NSString stringWithFormat: @"/testPubb.app/Documents/%@.epub",BookID];

                    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:filePathAppend];

                    [pdfData writeToFile:filePath atomically:YES];

Upvotes: 0

Views: 445

Answers (1)

Adeel Ur Rehman
Adeel Ur Rehman

Reputation: 566

The way you are getting the app document directory path is not correct. Try this:

NSString *urls =  [NSString stringWithFormat: @"http://hungerin.com/?download_file=%@&order=%@&email=%@&key=%@", BookID, orderKey, email, downloadId];
NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urls]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.epub",BookID];
[pdfData writeToFile:filePath atomically:YES];

Let me know if you have questions.

Upvotes: 1

Related Questions