是否可以检查设备上可以处理特定文件类型的应用程序数量?基本上,我的应用程序中有一个按钮,允许用户打开另一个应用程序,但如果没有可能的应用程序打开文档,我不想显示它。
答案 0 :(得分:5)
好的,所以想出了一个丑陋丑陋的黑客,但似乎工作......很想找到更好的方法。
为扩展程序创建头文件:
// UIDocumentInteractionController+willShowOpenIn.h
#import <Foundation/Foundation.h>
@interface UIDocumentInteractionController (willShowOpenIn)
- (BOOL)willShowOpenIn;
+ (BOOL)willShowOpenInForURL:(NSURL *)filePathURL;
@end
并实施:
// 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
然后使用:
#import "UIDocumentInteractionController+willShowOpenIn.h"
...
NSURL *testURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"]];
NSLog(@"Will show - %@",[UIDocumentInteractionController willShowOpenInForURL:testURL] ? @"YES" : @"NO");
请告诉我有更好的方法:)