后来解决了,做个记录。
1、在低于iOS13的系统中。用创建文件夹的形式可以如下:
+ (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上,不能这样写。系统会默认创建以为***.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])
}
}
在苹果备忘录里编辑txt文件方法如下。1、在APPSTORE中搜索,安装WPSOFFICE软件。
2、安装完成后,进入界面,在主界面点击左上角的选项图标。
3、打开手机设置选项,找到通用选项进入。
4、在通用中找到键盘,继续点击键盘。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)