How to check if UIDocumentInteractionController will fail to open document due to missing external application on iPad?

后端 未结 5 1500
独厮守ぢ
独厮守ぢ 2020-12-13 20:58

I am using UIDocumentInteractionController for showing popover menu \"Open In...\" so that user can open a document in other application.

Method p

相关标签:
5条回答
  • 2020-12-13 21:30

    I came up with a less hacky way of doing things, but there is a limitation that you can only detect whether there's a compatible app after the user has selected to open in an app. This will enable you to provide the same user experience as the Dropbox app.

    All you need to do is set up the UIDocumentInteractionControllerDelegate and create a boolean flag property that holds whether or not the menu was presented.

    In the interface:

    /**
     The document interaction controller used to present the 'Open with' dialogue.
     */
    @property (nonatomic,strong) UIDocumentInteractionController *documentInteractionController;
    
    /**
     Boolen that holds whether or not there are apps installed that can open the URL.
     */
    @property (nonatomic) BOOL hasCompatibleApps;
    

    In the implementation:

    - (void)shareFileAtURL:(NSURL*)fileURL
    {
        [self setDocumentInteractionController:[UIDocumentInteractionController interactionControllerWithURL:fileURL]];
        [[self documentInteractionController] setDelegate:self];
    
        [self setHasCompatibleApps:NO];
    
        [[self documentInteractionController] presentOpenInMenuFromRect:[self popoverRect] inView:[self popoverView] animated:YES];
    
        if (![self hasCompatibleApps])
        {
            // Show an error message to the user.
        }
    }
    
    #pragma mark - UIDocumentInteractionControllerDelegate methods
    
    - (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
    {
        [self setHasCompatibleApps:YES];
    }
    

    I hope that helps some people.

    0 讨论(0)
  • 2020-12-13 21:31

    This works for me:

       self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
       UIView *v = [[UIView alloc] init];
       BOOL isAnAppAvalaible = [self.docController presentOpenInMenuFromRect:CGRectZero inView:v animated:NO];
    
    0 讨论(0)
  • 2020-12-13 21:39

    -[UIApplication canOpenURL:] should do the job.

    0 讨论(0)
  • 2020-12-13 21:43

    [EDIT] Not working for iOS 6.0 (see comment)

    It seems that dismissMenuAnimated (with no animation at all) is the key:

    -(BOOL)canOpenDocumentWithURL:(NSURL*)url inView:(UIView*)view {
        BOOL canOpen = NO;
        UIDocumentInteractionController* docController = [UIDocumentInteractionController 
                                                       interactionControllerWithURL:url];
        if (docController)
        {
            docController.delegate = self;
            canOpen = [docController presentOpenInMenuFromRect:CGRectZero
                                       inView:self.view animated:NO];                   
            [docController dismissMenuAnimated:NO];
        }
        return canOpen;
    }
    

    It will return YES if at least one application is able to open the file pointed by url. At least it's working in my case (KMZ files), testing with/without Dropbox app on iPhone iOS 4.3.
    Actually, it seems to work even if url is not pointing to an actual file (i.e. @"test.kmz"), but I wouldn't rely on it for all file types.

    0 讨论(0)
  • 2020-12-13 21:45
    NSURL *url = [NSURL URLWithString:@"path_to_the_file"];
    UIDocumentInteractionController *controller =
        [UIDocumentInteractionController interactionControllerWithURL:url];
    BOOL openResult = [controller presentPreviewAnimated:NO];
    

    If you use presentPreviewAnimated: for showing files you can use openResult to detect if it was opened successfully.

    0 讨论(0)
提交回复
热议问题