BadPirate
BadPirate

Reputation: 26187

No options in UIDocumentInteractionController

Is it possible to check how many applications are available on the device that are able to handle a particular file type? Basically, I have a button in my application that allows users to open-in another application, but I don't want to show it if there are no possible applications to open the document in.

Upvotes: 3

Views: 1616

Answers (1)

BadPirate
BadPirate

Reputation: 26187

Okay, so figured out an ugly ugly hack, but seemed to work... would love to find a better way.

Create header file for extension:

//  UIDocumentInteractionController+willShowOpenIn.h

#import <Foundation/Foundation.h>

@interface UIDocumentInteractionController (willShowOpenIn)
- (BOOL)willShowOpenIn;
+ (BOOL)willShowOpenInForURL:(NSURL *)filePathURL;
@end

And implement:

//  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

Then use:

#import "UIDocumentInteractionController+willShowOpenIn.h"

...

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

Please tell me there is a better way :)

Upvotes: 5

Related Questions