简体   繁体   English

无法下载/读取我的ios应用程序文档路径中的文件

[英]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. 我正在使用以下代码从url下载电子书并将其存储到我的设备中。 While this works in emulator, in my ipad I am getting error saying file is not available. 虽然这在模拟器中有效,但是在我的ipad中,我收到错误消息,指出文件不可用。 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];

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. 如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM