iOS11 新出的可以从"文件"中选择附件
1.跳转"文件",选择数据
NSArray *types = @[
@"public.jpeg",
@"public.png",
@"public.mpeg-4",
@"com.apple.quicktime-movie",
@"com.microsoft.powerpoint.ppt",
@"com.microsoft.excel.xls",
@"com.microsoft.powerpoint.pptx",
@"com.microsoft.word.doc",
@"com.microsoft.word.docx",
@"com.microsoft.excel.xlsx",
@"com.microsoft.excel.xls",
@"public.item",
@"public.html",
@"public.avi",
@"public.3gpp",
@"public.mpeg-4",
@"public.plain-text",
@"com.adobe.pdf",
];
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
if (@available(iOS 11.0, *)) {
[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[UIDocumentBrowserViewController.class]].tintColor = kColorDarkRed;
}
if (@available(iOS 11.0, *)) {
documentPicker.allowsMultipleSelection = YES;
}
[controller presentViewController:documentPicker animated:YES completion:nil];
官方文档:https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1
types中是根据文档获取的,里面有各种附件的类型
2.实现代理 UIDocumentPickerDelegate
将文件保存到沙盒目录中,得到url
最后上传,这里没写
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
{
[self handleDocumentPicker:controller didPickDocumentsAtURLs:urls];
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
{
NSLog(@"用户取消了选择!");
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller
didPickDocumentAtURL:(NSURL *)url
{
[self handleDocumentPicker:controller didPickDocumentsAtURLs:@[url]];
}
- (void)handleDocumentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
{
// 将文件复制到Cache文件夹下
NSArray *newUrls = [self copyFilesToCacheWithOriginalUrls:urls];
// 得到文件的沙盒目录地址
}
- (NSArray<NSURL *> *)copyFilesToCacheWithOriginalUrls:(NSArray <NSURL *>*)urls
{
if (urls == nil || urls.count == 0) {
return @[];
}
NSMutableArray<NSURL *> *arrFileUrls = [NSMutableArray new];
long currMillisecond = [[NSDate date] timeIntervalSince1970] * 1000;
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSURL *url in urls) {
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory
, NSUserDomainMask
, YES).lastObject;
NSString *finderPath = [NSString stringWithFormat:@"%@/%ld/", cachePath, currMillisecond];
if ([fileManager fileExistsAtPath:finderPath] == NO) {
[fileManager createDirectoryAtPath:finderPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *filePath = [finderPath stringByAppendingPathComponent:url.lastPathComponent];
if ([fileManager fileExistsAtPath:filePath]) {
NSError *error = nil;
[fileManager removeItemAtPath:filePath error:&error];
if (error) NSLog(@"error : %@", error);
}
NSData *fileData = [NSData dataWithContentsOfURL:url];
BOOL success = [fileData writeToFile:filePath atomically:YES];
if (success) {
NSLog(@"file copy success");
[arrFileUrls addObject:[NSURL fileURLWithPath:filePath]];
}else{
NSLog(@"file copy fail");
}
}
return arrFileUrls;
}
网友评论