iOS UIDocumentPickerViewController使用 以及右上角按钮显示

不叨叨直接搞。

1.首先是在info.plist文件配置一下(增加两个key):

Supports opening documents in place

Application supports iTunes file sharing

2.因为自己项目的[UINavigationBar appearance]的字体等设置的白色 所以导致present到UIDocumentPickerViewController时 虽然点右上角有反应但看不到按钮(因为UIDocumentPickerViewController的导航栏也是白色的)。基于此需要新建一个继承于UIDocumentPickerViewController的子类DocumentViewController。

#import <UIKit/UIKit.h>

@interface DocumentViewController : UIDocumentPickerViewController

@end

#import "DocumentViewController.h"

@interface DocumentViewController ()

@end

@implementation DocumentViewController

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    //跟随系统颜色
    [UINavigationBar appearance].tintColor = self.view.tintColor;
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:self.view.tintColor}];
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    //跟随app颜色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

@end

当然你也可以像微信那样在子类中完成自己的(原谅色)风格定制。

3.接下来就简单了 我只需要上传pdf 所以... 拿到文件data 新建一个文件夹写到本地沙盒

#pragma mark -- 选择文件
- (void)goSelcetPdf  {
    NSArray *documentTypes = @[@"com.adobe.pdf"];
    DocumentViewController *documentPickerViewController = [[DocumentViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
    documentPickerViewController.delegate = self;
    [self presentViewController:documentPickerViewController animated:YES completion:nil];
}

#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(nonnull NSArray<NSURL *> *)urls {
    BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
    if(fileUrlAuthozied) {
        //通过文件协调工具来得到新的文件地址,以此得到文件保护功能
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError*error;     
      [fileCoordinatorcoordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
            //读取文件
            fileName= [newURLlastPathComponent];
            NSError*error =nil;
            NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];

            if(error) {

                //读取出错
            }else{
                //保存                              
                pdfData= fileData;
               NSArray*paths  =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString*path = [pathsobjectAtIndex:0];
                path = [pathstringByAppendingString:@"/YWBG"];
                if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
                }
                pathUrl = [path stringByAppendingPathComponent:fileName];
                [pdfData writeToFile:pathUrl atomically:YES];
            }
            [self dismissViewControllerAnimated:YES completion:NULL];
        }];
        [urls.firstObject stopAccessingSecurityScopedResource];
    }else{
        //授权失败
    }
}

水平有限 不喜来喷。。。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容