ios上传log日志文件txt 最详细使用方法
2020-11-19 本文已影响0人
一不小心灬
ios上传log日志文件txt
保存本地
首先,我们可以先获取document
文件的路径 , 第一次创建的时候可以加个@"crash"
的提示 然后就可以放我们想要存储的东西方了.
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fieldPath = [documentPath stringByAppendingPathComponent:@"logFiles.txt"];
NSFileManager * manager = [NSFileManager defaultManager];
if(![manager fileExistsAtPath:fieldPath]){
NSString *str = @"crash:\n";
[str writeToFile:fieldPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:fieldPath];
// 准备要写入的字符串
[handle seekToEndOfFile];
NSData *data = [[NSString stringWithFormat:@"%@",errorInfoDic] dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:data];
[handle closeFile];
获取本地文件
在我们想要上传的地方 比如设置中某个按钮上传日志
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSFileHandle * srcFile;// 输入文件、输出文件
NSData * buffer;// 读取的缓冲数据
// NSFileManager * manager = [NSFileManager defaultManager];
NSString * sourcePath = [documentPath stringByAppendingPathComponent:@"logFiles.txt"];// 源文件路径
// 覆盖数据
// [manager createFileAtPath:sourcePath contents:[@"Hello" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
srcFile = [NSFileHandle fileHandleForReadingAtPath:sourcePath];
buffer = [srcFile readDataToEndOfFile];// 读取数据
NSLog(@"%@",sourcePath);
上传log
根据需求把我们的logFiles.txt
上传给服务器, 代码中的 logFile.txt
是我们给服务器的名字 logFiles.txt
是在本地存储的名字 不要弄混了
NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
[dic setValue:SEEKPLISTTHING(USER_ID) forKey:@"userId"];
[dic setValue:self.viewModel.configId forKey:@"configId"];
NSString *endStr = [NSString stringWithFormat:@"%@mobileLog/upload",HTTP_LOOK_HOST];
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
NSString *encodeUrl =[NSString stringWithFormat:@"Bearer %@",[IF_NULL_TO_STRING(SEEKPLISTTHING(USER_TOKEN)) stringByURLEncode]];
[session.requestSerializer setValue:encodeUrl forHTTPHeaderField:@"Authorization"];
ShowHud();
self.uploadTask = [session POST:endStr parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:buffer name:@"logFile" fileName:@"logFile.txt" mimeType:@"image/txt"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%f",uploadProgress.fractionCompleted);
// process(uploadProgress.fractionCompleted,@"",@"");
[uploadProgress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
HideHud();
NSDictionary *resDic = responseObject;
NSLog(@"txt日志查看地址-----%@",[resDic objectForKey:@"data"]);
ShowTip([resDic objectForKey:@"msg"]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
HideHud();
}];
[self.uploadTask resume];
可以实现观察者方法,当progress
对象的fractionCompleted
属性值发生变化时回调此方法,在主线程中更新 UI
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void*)context {
NSProgress*progress =nil;
if([object isKindOfClass:[NSProgressclass]]) {
progress = (NSProgress*)object;
}
if(progress) {dispatch_async(dispatch_get_main_queue(), ^{
// 在主线程中更新
UIself.progressHUD.progress = progress.fractionCompleted;
}
);
}}