我正在使用下面的代码从 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/