【Coding】【7】iOS系统方式分享文件
本文最后更新于:2021年12月22日 上午
实现效果

代码实现
BYShareFileHelper.h
1 2 3 4 5 6 7 8 9 10 11
|
+ (void)shareFileInVC:(UIViewController * _Nullable)triggerVC sourceView:(UIView *)sourceView fileName:(NSString *)fileName;
+ (void)writeFile:(NSString *)fileName withInfo:(NSString *)info; + (void)writeFile:(NSString *)fileName withAutoTimePrefixInfo:(NSString *)info;
+ (void)appendWriteFile:(NSString *)fileName withInfo:(NSString *)info; + (void)appendWriteFile:(NSString *)fileName withAutoTimePrefixInfo:(NSString *)info;
|
BYShareFileHelper.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #pragma mark - Public method + (void)shareFileInVC:(UIViewController * _Nullable)triggerVC sourceView:(UIView *)sourceView fileName:(NSString *)fileName { _s_fileName = fileName; NSString *filePath = [self filePath]; NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[fileUrl] applicationActivities:nil]; if (triggerVC == nil) { triggerVC = [UIApplication sharedApplication].keyWindow.rootViewController; } UIPopoverPresentationController *popover = activity.popoverPresentationController; if (popover) { popover.sourceView = sourceView; popover.permittedArrowDirections = UIPopoverArrowDirectionUp; } [triggerVC presentViewController:activity animated:YES completion:NULL]; }
+ (void)writeFile:(NSString *)fileName withAutoTimePrefixInfo:(NSString *)info { _s_fileName = fileName; NSDateFormatter *formatter = [self timeFormatter]; NSString *str = [NSString stringWithFormat:@"%@ : %@\r\n",[formatter stringFromDate:[NSDate date]], info]; [self writeFile:fileName withInfo:str]; }
+ (void)writeFile:(NSString *)fileName withInfo:(NSString *)info { _s_fileName = fileName; NSData *data = [info dataUsingEncoding:NSUTF8StringEncoding]; [data writeToFile:self.filePath atomically:YES]; }
+ (void)appendWriteFile:(NSString *)fileName withAutoTimePrefixInfo:(NSString *)info { _s_fileName = fileName; NSDateFormatter *formatter = [self timeFormatter]; NSString *str = [NSString stringWithFormat:@"%@ : %@\r\n",[formatter stringFromDate:[NSDate date]], info]; [self appendWriteFile:fileName withInfo:str]; }
+ (void)appendWriteFile:(NSString *)fileName withInfo:(NSString *)info { _s_fileName = fileName; NSData *data = [info dataUsingEncoding:NSUTF8StringEncoding]; [self.sharedFileHandle writeData:data]; }
#pragma mark - Getter static NSString *_s_fileName = nil; static NSString *_s_filePath = nil;
+ (NSString *)filePath { NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentPath = [pathArray objectAtIndex:0]; NSString *directPath = [documentPath stringByAppendingPathComponent:@"BYFile"]; _s_filePath = [directPath stringByAppendingPathComponent:_s_fileName]; if (![[NSFileManager defaultManager] fileExistsAtPath:_s_filePath]) { BOOL suc = [[NSFileManager defaultManager] createDirectoryAtPath:directPath withIntermediateDirectories:YES attributes:nil error:nil]; NSLog(@"Create directPath %@ %@",directPath, suc?@"success":@"fail"); suc = [[NSFileManager defaultManager] createFileAtPath:_s_filePath contents:nil attributes:nil]; NSLog(@"Create file %@ %@",_s_filePath, suc?@"success":@"fail"); } return _s_filePath; }
static NSFileHandle *_s_fileHandle = nil; + (NSFileHandle *)sharedFileHandle { NSString *filePath = [self filePath]; _s_fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath]; [_s_fileHandle seekToEndOfFile]; return _s_fileHandle; }
static NSDateFormatter *_s_yMdHmsFormatter = nil;
+ (NSDateFormatter *)timeFormatter { if (_s_yMdHmsFormatter == nil) { _s_yMdHmsFormatter = [[NSDateFormatter alloc] init]; _s_yMdHmsFormatter.dateFormat = @"yyyy.MM.dd HH:mm:ss:SSS"; } return _s_yMdHmsFormatter; }
|
联系方式
邮箱: adrenine@163.com