Search code examples
iosuidocumentinteraction

UIDocumentInteractionController does not open pdf file


I am trying to open pdf file in UIDocumentInteractionController.UIDocumentInteractionController open pop up menu but enter image description here When I select the any app than my application it is crashed but not displaying any error

here is my thread enter image description here

here is my .h file

#import <UIKit/UIKit.h>
#import "REFrostedViewController.h"
@interface download : UIViewController<UITableViewDelegate,UITableViewDataSource,UIDocumentInteractionControllerDelegate>
- (IBAction)menu:(id)sender;
@property (strong, nonatomic) IBOutlet UITableView *tbl_download;

@end

.m file

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *pdfFilePath= [documentsDir stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:pdfFilePath error:NULL];
    NSLog(@"pdf path=%@",pdfFilePath);// your yourPdfFile file here
    NSURL *url = [NSURL fileURLWithPath:pdfFilePath];

    //create documentInteractionController here
    UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    //set delegate
     [docController setDelegate:self];
    //provide button's frame from where popover will be lauched

    CGRect rect = CGRectMake(0, 0, 0, 0);
    [docController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;
    return interactionController;
}


- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
    return self.view;
}

pls help me to solve problem...


Solution

  • Try using this line in your .h file:

    @property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;
    

    And these to your .m file:

    self.documentInteractionController= [UIDocumentInteractionController interactionControllerWithURL:url];
    //set delegate
    [self.documentInteractionController setDelegate:self];
    //provide button's frame from where popover will be lauched
    
    [self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];