【问题标题】:Add "Open in iBooks" to UIWebView将“在 iBooks 中打开”添加到 UIWebView
【发布时间】:2012-11-14 08:41:09
【问题描述】:

我正在为网站开发一个包装应用程序。基本上,它会在 UIWebView 中打开网站的移动版本。网站上的一些链接指向 PDF。

当在 Safari 中打开同一个站点并点击指向 PDF 的链接时,PDF 上会显示一条带有“在 iBooks 中打开”的漂亮黑色条纹。如下图所示:

如何在我的应用中实现相同的条纹?

编辑:

不是询问如何在半透明背景上创建黑色按钮。

我有兴趣重现整个工作流程:

  • 用户导航到 PDF
  • 条纹(视图)弹出窗口当且仅当安装了 iBooks 应用(或任何其他 PDF 查看器)。
  • 点击弹出窗口中的按钮将文档传输到该应用程序并打开应用程序。

【问题讨论】:

  • 按钮的视图(条纹?)或功能?
  • @Daij-Djan 请查看问题更新
  • 我担心的是:D jk 你可以使用 ibooks url 方案
  • 您是如何处理 UIDocumentInteractionController 需要物理文件并且无法与 UIWebview 的 url 交互的问题的?

标签: ios uiwebview


【解决方案1】:

要检查是否安装了 iBooks,您可以调用:

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

您可以使用以下方式显示应用程序列表(为什么仅限于 iBooks?;)):

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

请注意,这在 iOS 模拟器上不起作用,除非您制作了一个可以读取 PDF 的应用!

如果您真的只想提供让 PDF 在 iBooks 中打开的选项,您可能想尝试将文件的 URL 附加到 @"ibooks://" 方案或其他两个方案之一iBooks 提供(适用于 iBook Store 中的书籍,但我不确定它是否也适用于其他 URL)是 @"itms-books://" 和 @"itms-bookss://"。然后,您可以执行以下操作:

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

[[UIApplication sharedApplication] openURL:finalURL];

【讨论】:

  • 感谢您的详细解答。
  • 我知道这是一个老问题,但如果你在 ARC 下使用这种方式,你会得到一个错误。 self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
  • 附注:UIDocumentInteractionController 的 url 必须是本地的,文件 url(不是用于将 pdf 下载到 UIWebView 的那个)。因此,您需要先保存下载的带有 pdf 扩展名的 pdf 数据(最好保存到您的文档目录),然后使用 [NSURL fileURLWithPath:pdfPath] 创建 url。
【解决方案2】:

(再次回答,因为我之前的答案没有包含代码。道歉)

对于解决我的问题的解决方案,我找到了一个很好的示例 here

我已将其剪切并粘贴在这里,以防它对某人有所帮助。完全归功于absoluteripple.com

假设你的类叫做 ViewController,那么在 ViewController.h 文件中:

@interface 视图控制器:UIViewController { UIDocumentInteractionController *docController; }

在 ViewController.m 中添加以下方法: //- 设置 UIDocumentInteraction 控制器并将其委托设置为 self 以便我们可以处理回调事件

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

//- 这里的关键实例方法是presentOptionsMenuFromBarBUttonItem //- 这里假设有一个名为 _btnActions 的 BarButtonItem - (void)showOptionsMenu { NSURL *fileURL = [NSURL fileURLWithPath:@"THE_FILE_URL_PATH"]; docController = [自我 setupControllerWithURL:fileURL usingDelegate:self]; bool didShow = [docController presentOptionsMenuFromBarButtonItem:_btnActions 动画:是]; 如果(!didShow){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 消息:@“对不起。在此设备上找不到合适的应用程序。” 代表:无 取消按钮标题:@“确定” 其他按钮标题:无]; [警报显示]; } }
  1. 当您想显示可以将文件发送到的应用程序时,添加一个调用上述方法的方法 在此示例中,UIBarButton 连接到以下 IBAction:
- (IBAction)ActionButtonClicked:(id)sender { [自我显示选项菜单];}

就是这样。单击按钮时,将出现一个操作表(全部由 Apple 的 UIDocumentInteractionController 类提供支持),其中显示您可以将文件发送到的应用程序(如果有)。

您可以选择实现以下委托方法:

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

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

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2013-07-22
    • 2011-09-01
    • 2014-12-08
    相关资源
    最近更新 更多