不叨叨直接搞。
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)噴。。。