iOS4 SDK - Opening PDF’s in iBooks
For as simple as opening PDF’s in iBooks for iPhone and iPad I had some issues. Here is a short guide to do just that, open PDF’s from your application into iBooks. This demo will be using the iPhone, although I don’t think there will be much difference on iPhone or iPad.
First of all we will be using the UIKit Framework, this is generally already included into all headers you create. We will be using the UIDocumentInteractionController Class
UIDocumentInteractionController Documentation
First of all you will need to create a NSURL object like so:
NSURL *url = [NSURL fileURLWithPath:@”Some File URL”];
Next is what took me a while to figure out, the fact that the UIDocumentInteractionController object is autoreleased object. Thus once you leave the code block you create it in, it will be released and crash your application.
So to get around this I use the object as a property, and release it from memory at a later point.
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController retain];
BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
Using the presentOpenInMenuFromRect will return whether there is a valid application that can open the file. If there isn’t it will return False. In this case a PDF can be opened by iBooks, I add some extra validation to display an error message to the user if the file cannot be opened as expected.
In short this code listed will open an UIActionSheet with the iBooks selection, but if the file is not valid nothing will happen.
The following delegate methods will need to be implemented:
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller


Any questions, comments, or anything I missed please post a comment and let me know.
-
localbusinessapps reblogged this from andycodes
-
kandroidsi reblogged this from andycodes
-
aceisbored reblogged this from andycodes
-
jacksonfdam reblogged this from andycodes
-
walpage reblogged this from andycodes
-
sitdh reblogged this from andycodes
-
roothybrid7 reblogged this from andycodes
-
jdryden likes this
-
mindamwhardnett likes this
-
shanesuohatch likes this
-
harrietterejmaj likes this
-
francoisezakrap likes this
-
kennethswcgreen likes this
-
almawsanabria likes this
-
yokokgjelwell likes this
-
giannajoenorvel likes this
-
elysecqhterwill likes this
-
juliantapwilds likes this
-
franciscorhuwie likes this
-
clarindajkirwan likes this
-
irinaqecrouch likes this
-
katheyfsuaguirr likes this
-
elisainfbianchi likes this
-
ionemstadler likes this
-
januaryublehman likes this
-
ivaemcfishman likes this
-
lonnieudballest likes this
-
joeyzgtuttle likes this
-
shantaexhikeda likes this
-
rachelleelcrock likes this
-
simonegscates likes this
-
philliskmfabriz likes this
-
neldaddbernstei likes this
-
kimiarood likes this
-
divinajsbrandon likes this
-
staceetufwilber likes this
-
kiraoxlantz likes this
-
abbeyglcaudle likes this
-
ttgacdlda likes this
-
gtiblzsbe likes this
-
hkacyqjrae likes this
-
hfekybwsij likes this
-
aigbzwiqpv likes this
-
qqvfnfmkoo likes this
-
ysifbapxny likes this
-
heojnhfsn likes this
-
drugsrdrugs likes this
-
sadhasivam likes this
-
kimada likes this
-
hector99 reblogged this from andycodes
- Show more notes