iOS端如何选手机里的文件发送并查看手机里的文件file
用户选手机里的文件发送:
用 UIDocumentPickerViewController,完整实现如下:
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
// 弹出文件选择器
- (void)selectFileToSend {
UIDocumentPickerViewController *picker;
if (@available(iOS 14.0, *)) {
// iOS 14+ 用 UTType
picker = [[UIDocumentPickerViewController alloc]
initForOpeningContentTypes:@[UTTypeItem] asCopy:YES];
} else {
// iOS 11-13
picker = [[UIDocumentPickerViewController alloc]
initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeImport];
}
picker.delegate = self;
picker.allowsMultipleSelection = NO;
// iPad 需要设置 sourceView
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
picker.modalPresentationStyle = UIModalPresentationPopover;
picker.popoverPresentationController.sourceView = self.view;
picker.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2, 0, 0);
}
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller
didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
NSURL *fileURL = urls.firstObject;
if (!fileURL) return;
NSString *fileName = fileURL.lastPathComponent;
NSString *filePath = fileURL.path;
unsigned long long fileSize = [[[NSFileManager defaultManager]
attributesOfItemAtPath:filePath error:nil][NSFileSize] unsignedLongLongValue];
if (fileSize > 200 * 1024 * 1024) {
[CommonUtil showCommonToastWithStr:@"文件不能超过200M内存大小" hideAfterSeconds:1.5 andAfterPostAction:nil postObject:nil];
return;
}
// NSLog(@"文件名: %@", fileName);
// NSLog(@"文件路径: %@", filePath);
// NSLog(@"文件大小: %llu bytes", fileSize);
NSData *data = [NSData dataWithContentsOfFile:filePath];
kWeakSelf
[self sendMessage:[NTESSessionMsgConverter msgWithFileData:data extension:filePath.pathExtension] completion:^(NSError *err) {
if(weakSelf) {
[CommonUtil showCommonToastWithStr:@"文件发送中..." hideAfterSeconds:1.0 andAfterPostAction:nil postObject:nil];
// [weakSelf.navigationController popViewControllerAnimated:YES];
}
}];
/*
文件名: xxx.pdf
文件路径: /private/var/mobile/Containers/Data/Application/B2607605-97FC-4E9E-8C96-8866C2DA6283/tmp/com.expert.xx-Inbox/xxx.pdf
文件大小: 3058494 bytes
*/
// 发送文件
// [self sendFile:filePath fileName:fileName];
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
NSLog(@"用户取消选择");
}
注意 asCopy:YES 很重要,会把文件复制到 app 沙盒里,这样你就有完整的读写权限,不用担心 security-scoped URL 访问权限的问题。
Info.plist 不需要额外添加权限,UIDocumentPickerViewController 是系统级选择器,用户主动操作即视为授权。
查看文件:
用 QLPreviewController(Quick Look),系统原生支持预览 PDF、Office 文档、图片、视频等几十种格式:
#import <QuickLook/QuickLook.h>
// 预览文件
- (void)showFile:(NIMMessage *)message{
NIMFileObject *object = message.messageObject;
NTESFilePreViewController *vc = [[NTESFilePreViewController alloc] initWithFileObject:object];
// [self.navigationController pushViewController:vc animated:YES];
// self.fileNameLabel.text = self.fileObject.displayName;
NSString *path = object.path;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[self previewFileAtPath:path];
}else{
[[NIMSDK sharedSDK].chatManager fetchMessageAttachment:message error:nil];
[self previewFileAtPath:path];
}
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id)previewController:(QLPreviewController *)controller
previewItemAtIndex:(NSInteger)index {
return [NSURL fileURLWithPath:self.self.previewFilePath];
}
// 在任意 ViewController 里,传入文件路径直接预览
- (void)previewFileAtPath:(NSString *)filePath {
self.previewFilePath = filePath;
QLPreviewController *ql = [[QLPreviewController alloc] init];
ql.dataSource = self;
[self presentViewController:ql animated:YES completion:nil];
}
Ai给我的拿走不谢