【问题标题】:Saving PDF file to iBooks.将 PDF 文件保存到 iBooks。
【发布时间】:2014-03-10 03:39:55
【问题描述】:

我在 UIWebView 和 UIButton 中显示 PDF 文件列表。当我点击按钮时,我需要将我的 PDF 文件保存到 iBooks,以后用户可以从 iBooks 中阅读。

下面是我的代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@"mypdf" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];

供下载的UIButton:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(download:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[webView addSubview:button];

-(void)download:(id)sender{

}

【问题讨论】:

  • 为什么不使用 UIDocumentInteractionController 并让用户选择如何处理 PDF。
  • @rmaddy:我的要求是需要下载并保存到 iBooks。以后可以阅读。点击下载按钮时是否可以将 PDF 保存到 iBooks 中?
  • @rmaddy:谢谢。但是没有可用的 API 直接存储到 iBooks。因此,我们使用 UIDocumentInteractioncontroller 让用户在应用程序中打开 iBooks。我说的对吗?

标签: ios iphone pdf uiwebview ibooks


【解决方案1】:

您必须为此使用UIDocumentInteractionController

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath = [documentsDir stringByAppendingPathComponent:yourPdfFile.pdf];// your yourPdfFile file here
NSURL *url = [NSURL fileURLWithPath:pdfPath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];

docController.delegate = self;
[docController retain];
// docController is released when dismissed (autorelease in the delegate method)

BOOL isValid = [docController presentOpenInMenuFromRect:yourReadPdfButton.frame inView:self.view  animated:YES]; // Provide where u want to read pdf from yourReadPdfButton 

if (!isValid) {
NSString * messageString = [NSString stringWithFormat:@"No PDF reader was found on your device. In order to consult the %@, please download a PDF reader (eg. iBooks).", yourPDFFileTitle];

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:messageString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}

...

// UIDocumentInteractionController delegate method
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
 [controller autorelease];
}

希望这会对你有所帮助。

【讨论】:

  • 保存 pdf 后,您可以使用此代码或创建一个按钮来阅读和使用此代码。
  • 我已经知道了。单击下载按钮时,我需要将 PDF 文件直接保存到 iBooks。有可能吗?
  • 请查看本教程可能对您有所帮助andycodes.tumblr.com/post/738375724/…
  • 我已经参考了这段代码。但我的问题是我们可以直接保存到 iBooks 而不打开应用程序吗?
  • 不,我认为你不能这样做。你必须采用其他方式。
猜你喜欢
  • 1970-01-01
  • 2012-11-07
  • 2015-12-04
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多