iOS UIDocumentPickerViewController使用 以及右上角按鈕顯示

不叨叨直接搞。

1.首先是在info.plist文件配置一下(增加兩個(gè)key):

Supports opening documents in place

Application supports iTunes file sharing

2.因?yàn)樽约喉?xiàng)目的[UINavigationBar appearance]的字體等設(shè)置的白色 所以導(dǎo)致present到UIDocumentPickerViewController時(shí) 雖然點(diǎn)右上角有反應(yīng)但看不到按鈕(因?yàn)閁IDocumentPickerViewController的導(dǎo)航欄也是白色的)。基于此需要新建一個(gè)繼承于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];
    //跟隨系統(tǒng)顏色
    [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

當(dāng)然你也可以像微信那樣在子類中完成自己的(原諒色)風(fēng)格定制。

3.接下來(lái)就簡(jiǎn)單了 我只需要上傳pdf 所以... 拿到文件data 新建一個(gè)文件夾寫到本地沙盒

#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) {
        //通過(guò)文件協(xié)調(diào)工具來(lái)得到新的文件地址,以此得到文件保護(hù)功能
        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) {

                //讀取出錯(cuò)
            }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{
        //授權(quán)失敗
    }
}

水平有限 不喜來(lái)噴。。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容