0

I'm displaying list of PDF files in UIWebView along with UIButton. When I click the button I need to save my PDF file to iBooks and later user can read from iBooks.

Here is my code below:

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 for download:

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{

}
Irfan
  • 4,301
  • 6
  • 29
  • 46
user3351727
  • 67
  • 4
  • 14
  • Why not use a `UIDocumentInteractionController` and let the user choose what to do with the PDF. – rmaddy Mar 10 '14 at 03:47
  • @rmaddy: My requirement is need to download and save to iBooks. Later can read. Is it possible to save PDF into iBooks while click download button? – user3351727 Mar 10 '14 at 03:50
  • Check this search: http://stackoverflow.com/search?q=%5Bios%5D+open+pdf+ibooks – rmaddy Mar 10 '14 at 03:52
  • @rmaddy: thanks. But no API available directly store to iBooks. So, we have use UIDocumentInteractioncontroller let user to open iBooks inside app. Am I correct? – user3351727 Mar 10 '14 at 04:02

1 Answers1

1

You have to use UIDocumentInteractionController for this:

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

Hope this will help you.

TheEye
  • 9,280
  • 2
  • 42
  • 58
Irfan
  • 4,301
  • 6
  • 29
  • 46