在iBooks中打开本地PDF文件 [英] Open local PDF file in iBooks

查看:314
本文介绍了在iBooks中打开本地PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开我在iBooks中的应用程序中本地存储的PDF文件。该应用程序当前在表格视图中有一个文献列表,当每个单元格被点击时,该应用程序将转换为显示PDF文件的webView(效果很好)。我在navBar的右上方创建了一个BarButton,允许用户在iBooks中打开PDF(这样他们就可以将它存储在他/她的设备上)。

I am trying to open a PDF file that I have stored locally within my app in iBooks. The app currently has a list of "literature" in a table view and when each cell is tapped the app segues to a webView that displays the PDF file (works great). I have made a BarButton at the top right in navBar to allow the user to open the PDF in iBooks (so they can store it on his/her device).

所以该按钮将打开一个 UIDocumentInteractionController ,显示设备上可以打开文件的所有应用程序(检查iBooks是否已安装)。然后当我点击iBooks图标时,应用程序崩溃了。我能够修改代码以便iBooks打开而不会崩溃,但是PDF文件没有被执行所以它有点毫无意义(下面的代码会在它崩溃时恢复原状)。

So far the button will open a UIDocumentInteractionController that displays all apps on the device that can open the file (after checking if iBooks is installed). Then when I click the iBooks icon the app crashes. I was able to revise the code so that iBooks opens without crashing, but the PDF file is not carried through so it's kind of pointless (code below is reverted back to when it crashes).

以下代码位于barButton的IBAction内...

Code below is inside the IBAction of the barButton...

NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];

UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-bookss:"]])
{
    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    NSLog(@"ibooks is installed");
}
else
{
    NSLog(@"no ibooks installed");
}


推荐答案

修正了它!花了一些时间远离这个项目,两周后回来后我在<15分钟内想出来了。

Fixed it! Took some time away from the project and after coming back two weeks later I figured it out in <15 minutes.

所以这是docController和内存问题在.h文件中声明并使用(保留)它完美地工作。我也能够使用[NSBundle mainBundle]方法。

So it was a memory issue for the docController and by declaring in the .h file and using (retain) it works perfectly. I was also able to use the [NSBundle mainBundle] method as well.

.h
@property (retain)UIDocumentInteractionController *docController;

.m
@synthesize docController;

//in bar button IBAction
NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];

docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-books:"]]) {

    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    NSLog(@"iBooks installed");

} else {

    NSLog(@"iBooks not installed");

}

这篇关于在iBooks中打开本地PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆