在UIWebView中添加“在iBooks中打开”
我正在开发一个网站的包装应用程序。 基本上,它在UIWebView中打开移动版本的网站。 网站上的某些链接指向PDF。
当在Safari中打开同一网站并点击PDF链接时,PDF上会显示一个带有“在iBook中打开”的漂亮的黑色条纹。 就像下面的图片一样:
我怎么能在我的应用程序中实现相同的条纹?
编辑:
我不问如何在半透明的背景上创build一个黑色的button。
我有兴趣重现整个工作stream程:
- 用户导航到PDF
- 当且仅当安装有iBooks应用程序(或任何其他PDF查看器)时,条纹(查看)popup窗口。
- 点击popupbutton将文档传输到该应用程序,并打开应用程序。
要检查是否安装了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模拟器,除非您创build了一个读取PDF的应用程序!
如果您确实只想让PDF在iBooks中打开,您可能想尝试将文件的URL附加到@“ibooks://”scheme或iBook提供的另外两个scheme之一(哪些工作在iBook商店的书,但我不知道它是否也适用于其他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];
(再次回答,因为我以前的答案不包括代码。
对于解决我的问题的解决scheme,我在这里find一个很好的例子。
我已经剪切和粘贴在这里帮助别人。 充分信贷absoluteripple.com
假设你的类叫做ViewController,那么在ViewController.h文件中:
@interface ViewController:UIViewController { UIDocumentInteractionController * docController; }
在ViewController.m中添加以下方法://设置UIDocumentInteraction控制器并将其委托设置为self,以便处理callback事件
- (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 = [self setupControllerWithURL:fileURL usingDelegate:自]; bool didShow = [docController presentOptionsMenuFromBarButtonItem:_btnActions animation:YES]; if(!didShow){ UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@“” 消息:@“对不起,在这个设备上找不到合适的应用程序。” 代表:无 cancelButtonTitle:@ “OK” otherButtonTitles:nil]; [alert alert]; } }
- 当你想显示你可以发送文件的应用程序时,添加一个方法来调用上述在这个例子中,一个UIBarButton连接到以下IBActions:
- (IBAction)ActionButtonClicked:(id)sender { [self showOptionsMenu];}
这就对了。 点击button时,将出现一个操作表(所有这些都由Apple的UIDocumentInteractionController类提供),显示可以将文件发送到的应用程序(如果有的话)。
你可以select实现下面的委托方法:
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller