我正在创建一个从在线服务器下载文件并将其保存到App的本地内存的应用程序。 现在,我想要做的是当您单击“查看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'
答案 0 :(得分:0)
在您阅读时使用路径写入时比较filePath。它们看起来与我不同,所以你可能在按钮点击代码中有一个无效/零参考。
答案 1 :(得分:0)
尝试将pdf保存到文档文件夹
- (NSString *)applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
//在你的街区内
filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF.pdf"];
NSLog(@"PDF path: %@",filePath);
[data writeToFile:filePath atomically:YES];
要在iBooks用户UIDocumentInteractionController中打开pdf,请参阅this获取教程。
答案 2 :(得分:0)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-bookss://%@",self.pathToFile]]];
这确实打开了本地pdf到Ibooks。 ' ITMS-bookss'不是'itms-books'如果它已经位于您的Ibooks应用程序中。
另外,看起来将文件添加到iBooks就是使用不太可自定义的UIDocumentInteractionController。