-->

How to open a file on iBooks

2019-02-20 04:49发布

问题:

How can I open a book on iBooks? I tried the following code, that works, but asks the user to choose an application to open the file:

self.docController = [UIDocumentInteractionController alloc];

//leaves only the file name
NSString *bookFile = @"my-book.pdf";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex: 0];

NSString* filePath = [documentsDirectory stringByAppendingPathComponent: bookFile];

// Using the Book path
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

// setting the delegate
_docController.delegate = self;

// open the menu only if we find a valid application that can 
// open the file
BOOL isValid = [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

I want it to open directly on iBooks, without asking the user for this.

Any tips?

回答1:

just use this

NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

Where stringUrl is your filePath, with itms-books: at the beginning

See this related question How do I launch iBooks e-reader programmatically on iPad?