Ios 将PDF文件保存到iBooks。

Ios 将PDF文件保存到iBooks。,ios,iphone,pdf,uiwebview,ibooks,Ios,Iphone,Pdf,Uiwebview,Ibooks,我正在UIWebView中显示PDF文件列表以及UIWebView按钮。当我点击该按钮时,我需要将我的PDF文件保存到iBooks,以后用户可以从iBooks中读取 下面是我的代码: NSString *path = [[NSBundle mainBundle] pathForResource:@"mypdf" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSU

我正在UIWebView中显示PDF文件列表以及UIWebView按钮。当我点击该按钮时,我需要将我的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];
ui下载按钮:

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{

}
为此,您必须使用:

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];
}

希望这能对您有所帮助。

为什么不使用
UIDocumentInteractionController
,让用户选择如何处理PDF。@rmaddy:我的要求是需要下载并保存到iBooks。以后可以阅读。单击下载按钮时是否可以将PDF保存到iBooks中?请检查此搜索:@rmaddy:谢谢。但没有直接存储到iBooks的API。所以,我们使用UIDocumentInteractioncontroller让用户在应用程序内打开iBooks。我说的对吗?保存pdf后,您可以使用此代码或创建一个按钮来阅读和使用此代码。我已经知道这一点。单击下载按钮时,我需要将PDF文件直接保存到iBooks。有可能吗?请检查本教程,它可能会帮助您我已经参考了此代码。但我的问题是,我们可以不打开弹出式应用程序直接保存到iBooks吗?不,我认为你不能这样做。你必须采用其他方法。