不叨叨直接搞忍些。
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];
//跟隨系統(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
當然你也可以像微信那樣在子類中完成自己的(原諒色)風格定制蓖扑。
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) {
//通過文件協(xié)調(diào)工具來得到新的文件地址唉铜,以此得到文件保護功能
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{
//授權失敗
}
}
水平有限 不喜來噴。律杠。潭流。