如何检查UIDocumentInteractionController是否因iPad上缺少外部应用程序而无法打开文档?

pal*_*lob 19 iphone objective-c ipad

UIDocumentInteractionController用于显示弹出菜单"Open In ...",以便用户可以在其他应用程序中打开文档.

如果没有应用程序能够打开给定文档,则presentOpenInMenuFromBarButtonItem:animated:返回方法NO(菜单不显示).但是我等到目前为止已经为时已晚.我想禁用启动该按钮的按钮,如果不可能而不是提高用户的期望然后说"对不起,就不可能打开它".

是否可以查询系统以查看是否至少有一个应用程序注册了特定的文档类型?我已签canPreviewItem:QLPreviewController,但似乎它不支持UIDocumentInteractionController可以处理的相同文档类型.

FKD*_*Dev 11

[编辑]不适用于iOS 6.0(请参阅注释)

似乎dismissMenuAnimated(根本没有动画)是关键:

-(BOOL)canOpenDocumentWithURL:(NSURL*)url inView:(UIView*)view {
    BOOL canOpen = NO;
    UIDocumentInteractionController* docController = [UIDocumentInteractionController 
                                                   interactionControllerWithURL:url];
    if (docController)
    {
        docController.delegate = self;
        canOpen = [docController presentOpenInMenuFromRect:CGRectZero
                                   inView:self.view animated:NO];                   
        [docController dismissMenuAnimated:NO];
    }
    return canOpen;
}
Run Code Online (Sandbox Code Playgroud)

如果至少有一个应用程序能够打开url指向的文件,它将返回YES.至少它在我的情况下工作(KMZ文件),在iPhone iOS 4.3上使用/不使用Dropbox应用程序进行测试.
实际上,即使url没有指向实际文件(即@"test.kmz"),它似乎也能工作,但我不会依赖它来处理所有文件类型.


Luk*_*ers 5

我提出了一种不那么苛刻的做法,但是有一个限制,你只能在用户选择在应用程序中打开后检测是否有兼容的应用程序.这将使您能够提供与Dropbox应用程序相同的用户体验.

您需要做的就是设置UIDocumentInteractionControllerDelegate并创建一个布尔标志属性,该属性保存菜单是否显示.

在界面中:

/**
 The document interaction controller used to present the 'Open with' dialogue.
 */
@property (nonatomic,strong) UIDocumentInteractionController *documentInteractionController;

/**
 Boolen that holds whether or not there are apps installed that can open the URL.
 */
@property (nonatomic) BOOL hasCompatibleApps;
Run Code Online (Sandbox Code Playgroud)

在实施中:

- (void)shareFileAtURL:(NSURL*)fileURL
{
    [self setDocumentInteractionController:[UIDocumentInteractionController interactionControllerWithURL:fileURL]];
    [[self documentInteractionController] setDelegate:self];

    [self setHasCompatibleApps:NO];

    [[self documentInteractionController] presentOpenInMenuFromRect:[self popoverRect] inView:[self popoverView] animated:YES];

    if (![self hasCompatibleApps])
    {
        // Show an error message to the user.
    }
}

#pragma mark - UIDocumentInteractionControllerDelegate methods

- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
{
    [self setHasCompatibleApps:YES];
}
Run Code Online (Sandbox Code Playgroud)

我希望能帮助一些人.