一開始我用的方法一寫宗挥,但是后來發(fā)現(xiàn)在高系統(tǒng)上報(bào)錯(cuò)(大概報(bào)錯(cuò)內(nèi)容:NSCocoaErrorDomain:257)。
后來解決了种蝶,做個(gè)記錄契耿。
1、在低于iOS13的系統(tǒng)中螃征。用創(chuàng)建文件夾的形式可以如下:
+ (NSString *)tmpLogPath
{
? ? NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
? ? NSString *dirPath = [docPath stringByAppendingPathComponent:@"mylog"];
? ? NSString *filePath = [dirPath stringByAppendingPathComponent:@"tmpLog.txt"];
? ? return filePath;
}
+ (void)writeTmpLog:(NSString *)aMsg
{
? ? NSString *filePath = [[self class] tmpLogPath];
? ? if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
? ? ? ? BOOL isDir = NO;
? ? ? ? BOOL hasDir = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];
? ? ? ? if (!hasDir || !isDir) {
? ? ? ? ? ? [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil];
? ? ? ? }
? ? }
? ? NSError *error;
? ? NSString *content =[NSString stringWithContentsOfFile:filePath
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? encoding:NSUTF8StringEncoding
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:&error];
? ? NSString *newContent = [NSString stringWithFormat:@"%@\n%@",content,aMsg];
? ? [newContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
+ (void)clearTmpLog
{
? ? NSString *filePath = [[self class] tmpLogPath];
? ? [@"" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
2搪桂、但是在iOS13上,不能這樣寫盯滚。系統(tǒng)會默認(rèn)創(chuàng)建以為***.txt的文件夾踢械,在寫入的時(shí)候就出問題了。
會報(bào)錯(cuò):NSCocoaErrorDomain:257 就是
NSFileReadNoPermissionError = 257,/ /讀取錯(cuò)誤(權(quán)限問題)
所以可以這樣寫:
+ (NSString *)tmpLogPath {
? ? NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
? ? NSString *filePath = [documentPath stringByAppendingPathComponent:@"mylog/tmpLog.txt"];
? ? returnfilePath;
}
+ (void)writeTmpLog:(NSString*)aMsg {
? ? NSString*fieldPath = [[self class]tmpLogPath];
? ? NSLog(@"當(dāng)前文件大衅桥骸:%llu",[self fileSizeWithPath:fieldPath]);
? ? NSFileManager *manager = [NSFileManager defaultManager];
? ? if(![managerfileExistsAtPath:fieldPath]){
? ? ? ? NSError*error;
? ? ? ? [aMsgwriteToFile:fieldPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
? ? ? ? if(error) {
? ? ? ? ? ? NSLog(@"寫入失敗:%@\n",[error localizedDescription]);
? ? ? ? }
? ? }else{
? ? ? ? NSError*error;
? ? ? ? NSError*writeError;
? ? ? ? NSString *content =[NSString stringWithContentsOfFile:fieldPath
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? encoding:NSUTF8StringEncoding
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:&error];
? ? ? ? if(error) {
? ? ? ? ? ? NSLog(@"讀取失敗:%@\n",[error localizedDescription]);
? ? ? ? }
? ? ? ? NSString*newContent = [NSString stringWithFormat:@"%@\n%@",content,aMsg];
? ? ? ? [newContentwriteToFile:fieldPath atomically:YES encoding:NSUTF8StringEncoding error:&writeError];
? ? ? ? if(writeError) {
? ? ? ? ? ? NSLog(@"寫入失敗:%@\n",[writeErrorlocalizedDescription]);
? ? ? ? }
? ? }
}
//獲取文件大小
+ (unsignedlonglong)fileSizeWithPath:(NSString*)path {
? ? signedlonglongfileSize =0;
? ? NSFileManager *fileManager = [NSFileManager defaultManager];
? ? if([fileManagerfileExistsAtPath:path]) {
? ? ? ? NSError*error =nil;
? ? ? ? NSDictionary*fileDict = [fileManagerattributesOfItemAtPath:patherror:&error];
? ? ? ? if(!error && fileDict) {
? ? ? ? ? ? fileSize = [fileDictfileSize];
? ? ? ? }
? ? }
? ? returnfileSize;
}
+ (void)clearTmpLog {
? ? NSError*error;
? ? NSFileManager *manager = [NSFileManager defaultManager];
? ? NSString*filePath = [[selfclass]tmpLogPath];
? ? [managerremoveItemAtPath:filePatherror:&error];
? ? if(error) {
? ? ? ? NSLog(@"刪除失敗:%@\n",[error localizedDescription]);
? ? }
}