2011-07-20 40 views
3

是否可以檢查設備上有多少應用程序可以處理特定的文件類型?基本上,我在我的應用程序中有一個按鈕,允許用戶打開另一個應用程序,但是如果沒有可能的應用程序打開文檔,我不想顯示它。UIDocumentInteractionController中沒有選項

回答

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"); 

請告訴我有一個更好的方法:)

+0

你有沒有把她放到應用商店? – FaddishWorm

+0

App Store中沒有問題。但是就像任何黑客一樣,任何一天都可能破壞:) – BadPirate

相關問題