UIDocumentInteractionController的使用

//
//  ViewController.m
//  pdfViewer
//
//  Created by Johnay  on 17/9/28.
//  Copyright © 2017年 Johnay. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDocumentInteractionController *document;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"pdffile" withExtension:@"pdf"];
    //    NSString *docu = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //    NSString *filePath = [docu stringByAppendingPathComponent:@"004.png"];
    //    NSURL *url = [NSURL fileURLWithPath:filePath];
    
    
    self.document = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.document.delegate = self;
    
    // 不展示可选操作
    //    [self.document presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
    
    
    // 展示可选操作
    // 可结合代理方法documentInteractionControllerViewControllerForPreview:显示预览
    [self.document presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
    
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    return self;
}
/**
 *  文件分享面板退出时调用
 */
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
    NSLog(@"dismiss");
}

/**
 *  文件分享面板弹出的时候调用
 */
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {
    NSLog(@"WillPresentOpenInMenu");
    
}

/**
 *  当选择一个文件分享App的时候调用
 */
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application {
    NSLog(@"begin send : %@", application);
}
@end

你可能感兴趣的:(UIDocumentInteractionController的使用)