我有代码来显示如下文档:
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:self.thisUrl];
NSString *pathExtension = [self.thisUrl pathExtension];
if (pathExtension) {
NSString *UTI = (__bridge NSString*)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)(pathExtension), NULL);
if (UTI) {
documentInteractionController.UTI = UTI;
}
}
documentInteractionController.delegate = self;
[documentInteractionController presentOptionsMenuFromBarButtonItem:shareButton animated:YES];
当显示选项菜单时,它会显示可以打开文档的应用程序列表(例如消息),以及下面的操作列表。
选项菜单显示的列表操作与例如邮件应用程序中显示的菜单不同。
主要区别在于Mail应用程序显示" print"选项,而我的选项菜单没有。如何获取选项菜单以显示打印选项?
编辑: 我进行了进一步的测试,我实施了这些方法:
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action
{
return YES;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action
{
return YES; // or NO, doesn't matter
}
这具有显示" print"," copy"和#34;保存到相机胶卷"弹出视图中的操作。我点击它们时没有发生任何事情,可能是因为我没有正确实施-performAction
。我还在控制台日志中收到有关使用旧方法的警告。
在某些方面这是一个倒退,因为在添加这些方法之前,我无法打印一些能够使用文档交互控制器正确打印的文档。
答案 0 :(得分:2)
Apple鼓励您使用UIActivityViewController
。你可以轻松实现这一目标。但是,仅当共享内容类型支持打印时,“打印”选项才可用。您可以按数据类型here
- (IBAction)shareButton:(UIBarButtonItem *)sender
{
NSString *textToShare = @"Text to share";
NSURL *myWebContent = [NSURL URLWithString:@"http://yourpath.com/yourfile.pdf"]; // set your printable file here!
NSData *myData = [NSData dataWithContentsOfURL:myWebContent];
NSArray *objectsToShare = @[textToShare, myData];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
//Add exclusions here
NSArray *excludeActivities = @[UIActivityTypeAirDrop,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:YES completion:nil];
}
答案 1 :(得分:2)
我使用QuickLook框架工作了。我不知道为什么“打印”选项有时候不会出现在文档交互控制器中,但是再一次,其他人也没有。
QuickLook框架支持预览某些文档类型,但不是全部,所以我留在我以前的视图控制器和文档交互控制器中,用于那些不支持的类型。
以下是我工作代码的摘要。
@interface PreviewItemDataSource ()
@property (nonatomic, retain) NSURL* item;
@end
@implementation PreviewItemDataSource
@synthesize item=_item;
+(PreviewItemDataSource*)dataSourceWithItem:(NSURL*)item
{
PreviewItemDataSource *source = [[PreviewItemDataSource alloc] init];
source.item = item;
return source;
}
-(NSInteger) numberOfPreviewItemsInPreviewController:(QLPreviewController*)controller {
return 1;
}
- (id<QLPreviewItem>) previewController:(QLPreviewController*)controller previewItemAtIndex:(NSInteger)index {
return self.item;
}
@end
@interface AppDelegate ()
@property (nonatomic, retain) PreviewItemDataSource *dataSource;
@end
...
-(void) openExternalFile:(NSString*) filePath withDelegate:(id<ChildBrowserDelegate>)delegate
{
if ([filePath length] == 0)
return;
NSURL *item = [NSURL URLWithString:filePath];
if (item && [QLPreviewController canPreviewItem:item]) {
[self openQuickLookForItem:item];
} else {
// previous method unchanged
}
}
- (void) openQuickLookForItem:(NSURL*)item {
QLPreviewController *controller = [[QLPreviewController alloc] init];
PreviewItemDataSource *dataSource = [PreviewItemDataSource dataSourceWithItem:item];
controller.dataSource = dataSource;
controller.modalPresentationStyle = UIModalPresentationFullScreen;
[controller setCurrentPreviewItemIndex:0];
[self.viewController presentViewController:controller animated:YES completion:nil];
self.dataSource = dataSource;
}