後來解決了。做記錄。
1,在低於iOS13的系統中。創建文件夾的形式如下:
+(ns string *)tmpLogPath
{
ns string * docPath =【nshomeditory()stringByAppendingPathComponent:@“Library“】;
ns string * dirPath =【docPath stringByAppendingPathComponent:@“my log“】;
ns string * file path =【dirPath stringByAppendingPathComponent:@“tmp log . txt“】;
返回文件路徑;
}
+(void)write tmplog:(ns string *)aMsg
{
ns string * file path =【【self class】tmpLogPath】;
如果(!【【NSFileManager default manager】filexistsatpath:file path】){
BOOL isDir = NO
BOOL hasDir =【【NSFileManager default manager】filexistsatpath:file path is directory:& amp;isDir】;
如果(!hasDir ||!is dir ){
【【NSFileManager default manager】createDirectoryAtPath:intermediated directory中的文件路徑:無屬性:無錯誤:無】;
}
}
NSError *錯誤;
ns string * content =【ns string stringwithcontentsofile:file path
編碼:NSUTF8StringEncoding
錯誤:錯誤】;
ns string * new Content =【ns string string with format:@“% @ \ n % @“,content,aMsg】;
【new content write to file:file path atomic:YES encoding:nsu TF 8 string encoding error:nil】;
}
+(void)clearTmpLog
{
ns string * file path =【【self class】tmpLogPath】;
【@““write to file:file path atomic:YES encoding:nsu TF 8 string encoding error:nil】;
}
2,但是在iOS13上,妳不能這樣寫。系統將創建壹個名為* * * *的文件夾。txt,寫的時候會出錯。
將報告壹個錯誤:NSCocoaErrorDomain:257 is。
nsfilereadnopermission error = 257,//讀取錯誤(權限問題)
所以可以這樣寫:
+(ns string *)tmpLogPath {
ns string * document path =【NSSearchPathForDirectoriesInDomains(nslibrary directory,NSUserDomainMask,YES)lastObject】;
ns string * file path =【document path stringByAppendingPathComponent:@“my log/tmp log . txt“】;
returnfilePath
}
+(void)write tmplog:(ns string *)aMsg {
ns string * field path =【【self class】tmpLogPath】;
NSLog(@“當前文件大小:% LLU“,【self filesize with path:field path】);
NSFileManager * manager =【NSFileManager default manager】;
如果(!【managerfilexistsatpath:field path】){
NSError *錯誤;
【aMsgwriteToFile:field path atomic:YES encoding:nsu TF 8 string encoding error:& amp;錯誤】;
如果(錯誤){
NSLog(@“寫入失敗:% @\ n“,【錯誤本地化描述】);
}
}否則{
NSError *錯誤;
NSError * writeError
ns string * content =【ns string stringwithcontentsofile:field path
?編碼:NSUTF8StringEncoding
錯誤:錯誤】;
如果(錯誤){
NSLog(@“讀取失敗:% @\ n“,【錯誤本地化描述】);
}
ns string * new Content =【ns string string with format:@“% @ \ n % @“,content,aMsg】;
【newContentwriteToFile:field path atomic:YES encoding:nsu TF 8 string encoding error:& amp;write error】;
if(write error ){
NSLog(@“寫入失敗:% @\ n“,【writeerrorlocalized description】);
}
}
}
//獲取文件大小
+(unsignedlonglong)fileSizeWithPath:(ns string *)path {
signedlonglongfileSize = 0;
NSFileManager * file manager =【NSFileManager default manager】;
if(【filemanagerfilexistsatpath:path】){
NSError * error = nil
ns dictionary * file dict =【fileManagerattributesOfItemAtPath:path error:& amp;錯誤】;
如果(!誤差與誤差。& ampfileDict ){
fileSize =【filedict fileSize】;
}
}
returnfileSize
}
+(void)clearTmpLog {
NSError *錯誤;
NSFileManager * manager =【NSFileManager default manager】;
ns string * file path =【【self class】tmpLogPath】;
【managerremoveItemAtPath:filepath error:& amp;錯誤】;
如果(錯誤){
NSLog(@“刪除失敗:% @\ n“,【錯誤本地化描述】);
}
}