Add “Open in iBooks” to UIWebView

坚强是说给别人听的谎言 提交于 2019-12-03 05:20:18

问题


I am developing a wrapper application for a site. Basically, it opens mobile version of a site in UIWebView. Some links on the site point to PDFs.

When the same site is opened in Safari and a link to PDF is tapped a nice black stripe with "Open in iBooks" is shown over the PDF. Like on the picture below:

How could I implement the same looking stripe in my app?

EDIT:

I am not asking about how to create a black button on translucent background.

I am interested in reproducing the whole workflow:

  • User navigates to a PDF
  • A stripe (view) popups if and only if there is iBooks app (or any other PDF viewer) installed.
  • Tapping a button in popup transfers document to that app and the app opens.

回答1:


To check if iBooks is installed you can call:

BOOL iBooksInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"ibooks://"]];

You can present a list of of applications (why limit to iBooks only? ;) ) using:

//use the UIDocInteractionController API to get list of devices that support the file type
NSURL *pdfURL = // your pdf link.
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:pdfURL];

//present a drop down list of the apps that support the file type, click an item in the list will open that app while passing in the file.
 [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

Note that this doesn't work on the iOS Simulator unless you made an app that reads PDFs!

If you really want to only give the option to let the PDF to be opened in iBooks, you might want to try appending the file's URL to the @"ibooks://" scheme or the one of the other two schemes that iBooks provide (which work for books in the iBook Store but I'm not sure if it also works for other URLs) which are @"itms-books://" and @"itms-bookss://". You can then do something like:

NSURL *iBooksURLScheme = [NSURL URLWithString:@"ibooks://"];
NSString *fileURLString = // your file URL as *string*
NSURL *finalURL = [iBooksURLScheme URLByAppendingPathComponent:fileURLString];

[[UIApplication sharedApplication] openURL:finalURL];



回答2:


(Answered again as my previous answer did not include code. Apologies)

For a solution that fixed my issue I found a great example here.

I have cut and pasted it here incase it helps someone. Full credit to absoluteripple.com

Assuming your class is called ViewController, then in the ViewController.h file:        

    
@interface ViewController : UIViewController 
            {
                UIDocumentInteractionController *docController;
            }

Add the following methods in ViewController.m:         //- set up the UIDocumentInteraction controller and set its delegate to self so we can handle the callback events        


- (UIDocumentInteractionController *) setupControllerWithURL:(NSURL *)fileURL
                                               usingDelegate:(id <UIDocumentInteractionControllerDelegate>)         interactionDelegate {
    
            UIDocumentInteractionController *interactionController =
            [UIDocumentInteractionController interactionControllerWithURL:fileURL];
            interactionController.delegate = interactionDelegate;
    
            return interactionController;
            }

               //- the key instance method here is the presentOptionsMenuFromBarBUttonItem         //- it is assumed here that there is a BarButtonItem called _btnActions        
    

    - (void)showOptionsMenu
            {
                NSURL *fileURL = [NSURL fileURLWithPath:@"THE_FILE_URL_PATH"];
                docController = [self setupControllerWithURL:fileURL
                                       usingDelegate:self];
                bool didShow = [docController presentOptionsMenuFromBarButtonItem:_btnActions
                                                                 animated:YES];
                if (!didShow) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"Sorry. The appropriate apps are not found on this device." 
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles: nil];
                        [alert show];
                }
            }
  1. Add a method to invoke the above when you want to show the apps that you could send the file to In this example, a UIBarButton is wired up to the following IBActions:    
    - (IBAction)ActionButtonClicked:(id)sender {
            [self showOptionsMenu];}

That is it. When the button is clicked an action sheet will appear (all powered by the Apple's UIDocumentInteractionController class) that shows the apps (if any) that you could send the file to.

You can optionally implement the following delegate methods:

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application

- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller


来源:https://stackoverflow.com/questions/13375472/add-open-in-ibooks-to-uiwebview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!