Search code examples
iosobjective-cuidocumentinteraction

Filter apps that UIDocumentInteractionController suggests to Open In


Is it possible to manage the list of apps that UIDocumentInteractionController is going to present for opening my file in? Actually, I need to create black and white lists of apps. I took a look at UIDocumentInteractionControllerDelegate but it doesn't contain the needed methods..only
- (void) documentInteractionController: (UIDocumentInteractionController *) controller willBeginSendingToApplication: (NSString *) application

and I need something like

-(BOOL)shouldPresentForOpenInApplication: (NSString *)


Solution

  • Actually I figured out how to do it. This is a bit like a trick but it works. All you need to do is to implement a delegate's method, like so

    - (void) documentInteractionController: (UIDocumentInteractionController *) controller willBeginSendingToApplication: (NSString *) application {
        BOOL isAppAllowed = YES;
        // Make a decision based on app bundle identifier whether to open it or not
        if (isAppAllowed) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showRejectionMessage];
            });
            [self stopSendingFileUsingDocController:controller toApplication:application];
        }
    }
    

    And the actual method in which we deceive the doc controller by substituting bad url.

    -(void)stopSendingFileUsingDocController:(UIDocumentInteractionController *)controller toApplication:(NSString *)application
    {
        NSURL *documentURL = controller.URL;
        NSString *filePath = [documentURL path];
        filePath = [filePath stringByDeletingLastPathComponent];
        NSString *filename = [filePath lastPathComponent];
        NSMutableString *carets = [NSMutableString string];
        for (NSUInteger i = 0; i < [filename length]; i++)
        {
            [carets appendString:@"^"];
        }
    
    #ifndef __clang_analyzer__
        filePath = [filePath stringByAppendingPathComponent:carets];
        NSURL *newURL = [[NSURL alloc] initFileURLWithPath:filePath];
        controller.URL = newURL;
    #endif
        [self documentInteractionController:controller didEndSendingToApplication:application];
    }