UIDocumentInteractionController不会打开pdf文件

时间:2016-05-05 06:05:55

标签: ios uidocumentinteraction

我正在尝试在UIDocumentInteractionController打开pdf文件。UIDocumentInteractionController打开弹出菜单但是 enter image description here  当我选择任何应用程序而不是我的应用程序时,它会崩溃,但不会显示任何错误

这是我的主题 enter image description here

这是我的.h文件

#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文件

- (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;
}

请帮助我解决问题...

1 个答案:

答案 0 :(得分:2)

尝试在.h文件中使用此行:

@property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;

这些是您的.m文件:

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];