iOS将log日志写入文件并开放社交app分享log文件接口

1.

HKWriteLogManager.h文件

//
//  HKWriteLogManager.h
//  HKiOSDemo
//
//  Created by liuhuakun on 2020/3/31.
//  Copyright © 2020 liuhuakun. All rights reserved.
//
/* 头文件中引入这个重写NSLog宏,更有助于查找问题
 /// 重写NSLog,Debug模式下打印日志和当前行数
 #if DEBUG
 #define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
 #else
 #define NSLog(FORMAT, ...) nil
 #endif
 */


#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface HKWriteLogManager : NSObject
/**
 * 单例
 * @return 单例对象
 */
+ (HKWriteLogManager *)writeLog_shareManager;
/**
 * 开启写入Log日志
 */
- (void)writeLog_startWriteLogToFile;
/**
 * 获取log文件路径用于上传或者分享
 * @return log文件路径
 */
- (NSString *)writeLog_logFilePath;
/**
 * 通过社交平台分享log文件
 */
- (void)writeLog_shareLogFile;


@end

NS_ASSUME_NONNULL_END

 

2.

HKWriteLogManager.m文件

//
//  HKWriteLogManager.m
//  HKiOSDemo
//
//  Created by liuhuakun on 2020/3/31.
//  Copyright © 2020 liuhuakun. All rights reserved.
//

#import "HKWriteLogManager.h"
#import <UIKit/UIKit.h>

@implementation HKWriteLogManager


#pragma mark - Instance

+ (HKWriteLogManager *)writeLog_shareManager {
    static HKWriteLogManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HKWriteLogManager alloc] init];
    });
    return instance;
}


#pragma mark _Publish

- (void)writeLog_startWriteLogToFile {
    /// 子线程中操作
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        /// log文件路径
        NSString *logFilePath = [self writeLog_logFilePath];
        /// 删除已经存在的文件
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager fileExistsAtPath:logFilePath]) {
            [defaultManager removeItemAtPath:logFilePath error:nil];
        }
        freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+", stdout);
        freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+", stderr);
        /// 获取手机设备信息
        [self writeLog_iPhoneMessage];
    });
}


/// 获取log文件路径
- (NSString *)writeLog_logFilePath {
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"HKiOSDemo.log"];
    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
    return logFilePath;
}


/// 通过社交app分享log文件
- (void)writeLog_shareLogFile {
    NSURL *fileURL = [NSURL fileURLWithPath:[self writeLog_logFilePath]];
    NSArray *activityItems = @[fileURL];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                                                             applicationActivities:nil];
    /// 根视图中显示分享界面
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVC
                                                                                 animated:YES
                                                                               completion:nil];
}


#pragma mark - Method

/// 当前手机设备信息
- (void)writeLog_iPhoneMessage {
    UIDevice *device = [UIDevice currentDevice];
    NSDictionary *iPhoneMessageDict = @{ @"name" : device.name,
                                         @"model" : device.model,
                                         @"localizedModel" : device.localizedModel,
                                         @"systemName" : device.systemName,
                                         @"systemVersion" : device.systemVersion,
                                         @"identifierForVendor" : device.identifierForVendor};
    NSLog(@"device info = %@", iPhoneMessageDict);
}




@end

 

posted @ 2020-03-31 10:39  isHakan  阅读(563)  评论(0编辑  收藏  举报