UIDocumentInteractionController 无法打开 pdf 文件

Posted

技术标签:

【中文标题】UIDocumentInteractionController 无法打开 pdf 文件【英文标题】:UIDocumentInteractionController does not open pdf file 【发布时间】:2016-05-05 06:05:54 【问题描述】:

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

这是我的主题

这是我的 .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】:

尝试在您的 .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];

【讨论】:

以上是关于UIDocumentInteractionController 无法打开 pdf 文件的主要内容,如果未能解决你的问题,请参考以下文章