埋点:监控用户点击的每一步,它不做页面相关的事情而是把用户当前点击的东西,传到服务器达到记录用户点击的每一步。
本片文章主要用两种方式实现该功能:1、通过改变系统中NSLog的输出信息写入到文件中(参考文章)。2、通过自定义方式将数据写入自定义文件夹中。
1、改变系统中NSLog的输出信息写入到文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.//制定真机调试保存日志文件[self redirectNSlogToDocumentFolder];return YES;}#pragma mark - 用户方法,将NSLog的输出信息写入到.log文件中// 将NSLog打印信息保存到Document目录下的文件中- (void)redirectNSlogToDocumentFolder{//文件路径也可以自定义NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentDirectory = [paths objectAtIndex:0];NSString *fileName = [NSString stringWithFormat:@\"%@.log\",[[NSDate alloc] initWithTimeIntervalSinceNow:8*3600]]; // 注意不是NSData!NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];//debug模式下,将打印输出流改变。release模式则正常#ifdef DEBUG// 将log输入到文件// \"r\"表示“只读访问”、\"w\"表示“只写访问”、\"a\"表示“追加写入”。// 使用定向日志后,Xcode控制台将无法输入日志,如果需要在debug模式下在控制台打印日志,可以注掉下面这句代码freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],\"a+\",stderr);NSLog(@\"~~~~%@\",logFilePath);#elseNSLog(@\"~~~~%@\",logFilePath);#endif}
freopen函数(来源度娘)
2、通过自定义方式将数据写入自定义文件夹中
##1、关于宏调用自定义的方法
先定义一个宏(可带参数或返回值,也可一无所有)
#define FILELOG_SUPPORT(str) [self redirectNSlogToDocumentFolder:str]
然后在自定义类中写一个同名方法
- (NSString *)redirectNSlogToDocumentFolder:(NSString *)parameter
调用
-(void)click:(UIButton *)btn{FILELOG_SUPPORT(@\"2222\");NSString *str = FILELOG_SUPPORT(@\"2222\");NSLog(@\"%@\",str);}
2、通过宏调用自定义的写入文件方法实现简单埋点功能
直接上代码
#import \"ViewController.h\"#define FILELOG_SUPPORT(str) [self redirectNSlogToDocumentFolder:str]@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.UIButton *btn = [UIButton new];[self.view addSubview:btn];btn.backgroundColor = [UIColor redColor];btn.frame = CGRectMake(100, 100, 140, 140);[btn setTitle:@\"客服服务\" forState:(UIControlStateNormal)];// [btn setImage:[UIImage imageNamed:@\"qwas.png\"] forState:(UIControlStateNormal)];[btn addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];}-(void)click:(UIButton *)btn{FILELOG_SUPPORT(@\"2222\");NSString *str = FILELOG_SUPPORT(@\"2222\");NSLog(@\"%@\",str);}#pragma mark 通过宏调用自定义方法 --- 无参数无返回值的宏,带参数的宏,带返回值的宏,- (NSString *)redirectNSlogToDocumentFolder:(NSString *)parameter{NSLog(@\"通过宏定义调用自定义的方法~~~~%@\",parameter);NSString *strReturn = @\"我是返回值\";//需要写入的数据NSString *str = @\"1234567890\";NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];[self writeToFolderDirCache:data folder:@\"12\" file:@\"test1\"];return strReturn;}//写入Cache自定义的文件夹中- (void)writeToFolderDirCache:(NSData *)data folder:(NSString *)folderName file:(NSString *)fileName{//创建文件夹NSString *documentsPath =[self dirCache];NSFileManager *fileManager = [NSFileManager defaultManager];NSString *testDirectory = [documentsPath stringByAppendingPathComponent:folderName];// 创建目录NSError *error;BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:&error];if (res) {NSLog(@\"文件夹创建成功\");}else{NSLog(@\"文件夹创建失败 --- %@\",error);}NSString *FileName=[testDirectory stringByAppendingPathComponent:fileName];//写入文件[data writeToFile:FileName atomically:YES];}//获取Cache目录-(NSString *)dirCache{NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachePath = [cacPath objectAtIndex:0];NSLog(@\"app_home_lib_cache: %@\",cachePath);return cachePath;}@end
关于文件及文件夹的操作参考文件操作demo。
测试demo。
欢迎指点!