iOS開發(fā)生成txt文件

一開始我用的方法一寫,但是后來發(fā)現(xiàn)在高系統(tǒng)上報錯(大概報錯內(nèi)容:NSCocoaErrorDomain:257)。

后來解決了,做個記錄。

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)會默認創(chuàng)建以為***.txt的文件夾,在寫入的時候就出問題了。

會報錯:NSCocoaErrorDomain:257 就是
NSFileReadNoPermissionError = 257,/ /讀取錯誤(權限問題)

所以可以這樣寫:

+ (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(@"當前文件大?。?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]);

? ? }

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • iOS開發(fā)-文件管理(一) 一、iOS中的沙盒機制 iOS應用程序只能對自己創(chuàng)建的文件系統(tǒng)讀取文件,這個獨立、封閉...
    MacShare閱讀 1,860評論 0 6
  • 一、iOS中的沙盒機制 iOS應用程序只能對自己創(chuàng)建的文件系統(tǒng)讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它一...
    tzhtodd閱讀 1,340評論 0 2
  • iOS開發(fā)-文件管理(一) 一、iOS中的沙盒機制 iOS應用程序只能對自己創(chuàng)建的文件系統(tǒng)讀取文件,這個獨立、封閉...
    Friez平板支撐閱讀 4,694評論 0 1
  • 一、iOS中的沙盒機制 iOS應用程序只能對自己創(chuàng)建的文件系統(tǒng)讀取文件,這個獨立、封閉、安全的空間,叫做沙盒。它一...
    1d5cb7cff98d閱讀 1,870評論 0 0
  • 文件操作在我們開發(fā)過程中或多或少都會遇到,我一般不會去記這些,每次使用的時候都要去查詢下,有點麻煩,今天索性記錄下...
    蒲公英少年帶我飛閱讀 1,828評論 0 12

友情鏈接更多精彩內(nèi)容