美文网首页
iOS UIDocumentPickerViewControll

iOS UIDocumentPickerViewControll

作者: 張浪浪Coder | 来源:发表于2019-12-12 14:38 被阅读0次

不叨叨直接搞。

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{
        //授权失败
    }
}

水平有限 不喜来喷。。。

相关文章

网友评论

      本文标题:iOS UIDocumentPickerViewControll

      本文链接:https://www.haomeiwen.com/subject/wnhtnctx.html