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

标签 ios objective-c xcode ipad

我正在使用下面的代码从 url 下载电子书并将其存储到我的设备中。虽然这在模拟器中有效,但在我的 iPad 中,我收到错误消息,指出文件不可用。我无法读取设备相应路径中的文件。请帮助..

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];

最佳答案

您获取应用程序文档目录路径的方式不正确。试试这个:

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];

如果您有疑问,请告诉我。

关于ios - 无法下载/读取我的 ios 应用程序文档路径中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27691702/

相关文章:

ios - 范围不正确

ios - 从本地联系人获取详细信息后如何区分电话号码

ios - 核心数据故障对象

ios - Flutter [firebase_messaging] IOS 构建错误

ios - 关闭应用程序并在 appstart 上启动 webview

iphone - 限制 UITableView 的滚动

ios - 为什么时间不自动更改为最新的分钟/小时?

swift - 迁移到 swift 3

iphone - 我可以制作具有这些规范的 iPhone 应用程序吗?

ios - Appium,仪器在启动时崩溃