UIDocumentInteractionController中没有选项

Bad*_*ate 3 iphone uidocumentinteraction

是否可以检查设备上有多少应用程序可以处理特定文件类型?基本上,我的应用程序中有一个按钮,允许用户打开另一个应用程序,但如果没有可能的应用程序打开文档,我不想显示它.

Bad*_*ate 5

好吧,所以想出了一个丑陋丑陋的黑客,但似乎工作......很想找到一个更好的方法.

为扩展创建头文件:

//  UIDocumentInteractionController+willShowOpenIn.h

#import <Foundation/Foundation.h>

@interface UIDocumentInteractionController (willShowOpenIn)
- (BOOL)willShowOpenIn;
+ (BOOL)willShowOpenInForURL:(NSURL *)filePathURL;
@end
Run Code Online (Sandbox Code Playgroud)

并实施:

//  UIDocumentInteractionController+willShowOpenIn.m

#import "UIDocumentInteractionController+willShowOpenIn.h"

@implementation UIDocumentInteractionController (willShowOpenIn)
- (BOOL)willShowOpenIn
{
    id <UIDocumentInteractionControllerDelegate> oldDelegate = self.delegate;
    self.delegate = nil;
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    if([self presentOpenInMenuFromRect:window.bounds inView:window
                           animated:NO])
    {
        [self dismissMenuAnimated:NO];
        self.delegate = oldDelegate;
        return YES;
    }
    else {
        self.delegate = oldDelegate;
        return NO;
    }
}

+ (BOOL)willShowOpenInForURL:(NSURL *)filePathURL
{
    UIDocumentInteractionController *tempController = [UIDocumentInteractionController interactionControllerWithURL:filePathURL];
    return [tempController willShowOpenIn];
}
@end
Run Code Online (Sandbox Code Playgroud)

然后使用:

#import "UIDocumentInteractionController+willShowOpenIn.h"
Run Code Online (Sandbox Code Playgroud)

...

NSURL *testURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"]];
NSLog(@"Will show - %@",[UIDocumentInteractionController willShowOpenInForURL:testURL] ? @"YES" : @"NO");
Run Code Online (Sandbox Code Playgroud)

请告诉我有更好的方法:)