我正在为网站开发一个包装器应用程序。基本上,它在UIWebView中打开网站的移动版本。网站上的一些链接指向PDF。
当在Safari中打开同一站点并点击PDF链接时,在PDF上显示带有“在iBooks中打开”的漂亮黑条纹。如下图所示:
我怎样才能在我的应用中实现相同的条纹?
编辑:
我不询问如何在半透明背景上创建黑色按钮。
我有兴趣重现整个工作流程:
答案 0 :(得分:24)
要检查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的应用程序!
如果您真的只想让选项让我们在iBooks中打开PDF,您可能想尝试将文件的URL附加到@“ibooks://”方案或其他两个方案之一iBooks提供(适用于iBook商店中的书籍,但我不确定它是否适用于其他网址)@ @ 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];
答案 1 :(得分:1)
(因为我以前的回答没有包括代码而再次回答。道歉)
对于修复我的问题的解决方案,我找到了一个很好的例子here。
我已将它剪切并粘贴在此处,以帮助某人。完全归功于absoluteripple.com
假设您的类名为ViewController,则在ViewController.h文件中:
@interface ViewController : UIViewController { UIDocumentInteractionController *docController; }
在ViewController.m中添加以下方法: // - 设置UIDocumentInteraction控制器并将其委托设置为self,以便我们可以处理回调事件
// - 这里的关键实例方法是presentOptionsMenuFromBarBUttonItem // - 这里假设有一个名为_btnActions的BarButtonItem- (UIDocumentInteractionController *) setupControllerWithURL:(NSURL *)fileURL usingDelegate:(id <UIDocumentInteractionControllerDelegate>) interactionDelegate { UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; interactionController.delegate = interactionDelegate; return interactionController; }
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
- (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]; } }
就是这样。单击该按钮时,将显示一个操作表(全部由Apple的UIDocumentInteractionController类提供),该操作表显示可以将文件发送到的应用程序(如果有)。
您可以选择实现以下委托方法:
- (IBAction)ActionButtonClicked:(id)sender { [self showOptionsMenu];}