3

特定のファイル タイプを処理できるデバイスで利用可能なアプリケーションの数を確認することはできますか? 基本的に、アプリケーションにはユーザーが別のアプリケーションで開くことができるボタンがありますが、ドキュメントを開くアプリケーションがない場合は表示したくありません。

4

1 に答える 1

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

もっと良い方法があることを教えてください:)

于 2011-07-21T01:10:23.217 に答える