我正在制作一个应用程序,它从在线服务器下载文件并将其保存到应用程序的本地内存中。现在,我想要做的是,当您单击“查看 PDF”按钮时,它会将 PDF 文件直接打开到 iBooks。
这是我保存文件的代码:
currentURL = @"http://weblink.com/folder1/folder2/file.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
data = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
[data writeToFile:filePath atomically:YES];
});
这是我从互联网上找到的用于将文件打开到 iBooks 的内容(我将此代码放在按钮单击上):
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
该应用程序因该代码和此消息而崩溃:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'