所以我试图用NSURLConnection下载一本ePub书。到目前为止,下载工作正常。当我尝试保存并打开它时会出现问题。当我尝试使用UIDocumentInteractionController打开它时,应用程序崩溃说该目录为空。
这是我的代码:
//Download ePub file
NSURL *url = [NSURL URLWithString:@"http://www.terceiroanjo.com/materiais/missaopiloto.epub"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
然后
// save it ("data" = NSMutableData that was appended from the received data of the download)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *filePath = [NSString stringWithFormat:@"%u/%@", NSDocumentDirectory,@"missaopiloto.epub"];
[data writeToFile:filePath atomically:YES];
}
并打开ePub:
NSString *path = [[NSBundle mainBundle] pathForResource:@"missaopiloto" ofType:@"epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
当我尝试打开它时,应用程序崩溃了:
*由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * - [NSURL initFileURLWithPath:]:nil字符串参数'
我明白了,目录是nill,但为什么呢?我究竟做错了什么。有什么想法吗?
感谢。
答案 0 :(得分:0)
我认为这里的问题是,一旦下载并保存文件,它就不会成为捆绑包的一部分。
如果要访问该文件,则必须提供本地路径并创建本地URL
NSString *path = [NSString stringWithFormat:@"%u/%@", NSDocumentDirectory,@"missaopiloto.epub"];
NSURL *urls = [NSURL fileURLWithPath:path];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:urls];
documentController.delegate = self;
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];