yunyang088's blog


  • 首页

  • 分类

  • 归档

  • 标签

  • 公益404

  • 搜索
close

使用AOP实现iOS应用内的埋点计数

发表于 2016-08-24   |   分类于 iOS技术积累   |     |   阅读次数

目前做的项目中,需要使用友盟统计来做埋点,统计app内功能的使用次数,友盟SDK中是[MobClick event:xx];这种令人蛋疼的代码来埋点。

不需要动脑子的方式就是在每个需要埋点的地方,写一遍这个代码。

这种方式虽然省脑子,但是既耗时间,也不便于后期维护。

因为对AOP有一定的了解,所以笔者就查阅相关资料,想通过AOP来实现只在一个地方写一遍[MobClick event:xx];就能达到挂钩的埋点方法。

在阅读了Method Swizzling和AOP(面向切面编程)实践这篇文章后,感觉到这正是我想要的方式,遂下载了该作者提供的Demo代码,并对其做了修改。

AOP的框架使用的是 Aspects,使用plist记录需要挂钩的视图控制器名,事件说明,事件名,方法名。

Aspects

Aspects是iOS平台一个轻量级的面向切面编程(AOP)框架,只包括两个方法:一个类方法,一个实例方法。

1
2
3
4
5
6
7
8
9
+ (id<AspectToken>)aspect_hookSelector:(SEL)selector
withOptions:(AspectOptions)options
usingBlock:(id)block
error:(NSError **)error;
- (id<AspectToken>)aspect_hookSelector:(SEL)selector
withOptions:(AspectOptions)options
usingBlock:(id)block
error:(NSError **)error;

函数使用方式简单易懂,挂钩的方式为三种:

1
2
3
4
5
6
7
typedef NS_OPTIONS(NSUInteger, AspectOptions) {
AspectPositionAfter = 0, /// 在原始方法后调用(默认)
AspectPositionInstead = 1, /// 替换原始方法
AspectPositionBefore = 2, /// 在原始方法前调用
AspectOptionAutomaticRemoval = 1 << 3 /// 在执行1次后自动移除
};

看一段代码:

1
2
3
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) {
NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated);
} error:NULL];

应该很容易能看出这段代码是对UIViewController的viewWillAppear:进行挂钩,在原始方法执行完成后,打印字符串。

plist记录需要挂钩的功能对应的VC和Function

plist预览

结构为Dictionary->Array-> Dictionary

首先是对应视图控制器的名字 如图中的ViewController

GLLoggingTrackedEvents 为固定的字段名,内容为Array

Array中包含多个Dictionary, Dictionary中的字段名如下:

  • GLLoggingFuncDesc 是事件描述
  • GLLoggingFuncName 是事件名
  • GLLoggingSelectorName 是方法名

注意: 挂钩使用的是SEL,所以如果方法是需要传入参数的,方法名末尾需要增加”:“

挂钩的函数如下(在AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions调用):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
+ (void)setupWithConfiguration{
NSDictionary *configs = [self dictionaryFromUserStatisticsConfigPlist];
for (NSString *className in configs) {
Class clazz = NSClassFromString(className);
NSDictionary *config = configs[className];
if (config[GLLoggingTrackedEvents]) {
for (NSDictionary *event in config[GLLoggingTrackedEvents]) {
SEL selector = NSSelectorFromString(event[GLLoggingSelectorName]);
[clazz aspect_hookSelector:selector
withOptions:AspectPositionAfter
usingBlock:^(id<AspectInfo> aspectInfo) {
[MobClick event:event[GLLoggingFuncName]];
} error:NULL];
}
}
}
}
+ (NSDictionary *)dictionaryFromUserStatisticsConfigPlist
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Logging" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
return dic;
}

注:

  • 这种方式也适用于其他统计SDK需要运行单行函数进行计数的情况。
  • 这种方法需要维护一个plist,并且要将埋点的功能单独写作函数,仍有不方便的地方,目前未想到更好的方法。
  • 笔者没有对这种方式的性能进行严谨的测试,笔者开发的APP中计数了30+,没有影响到应用启动的速度。

3D Touch 简单介绍与接入指南(动图演示)

发表于 2016-04-14   |   分类于 iOS技术积累   |     |   阅读次数

0x00 前言

本文内容包括

  • 3D Touch 简单介绍
  • Home Screen Quick Actions(动态生成)
  • Peek&Pop
  • Peek上拉快捷操作

文中的代码均取自本人的Github项目 For3DTouchDemo

阅读全文 »

个人使用的iOS开发宏定义

发表于 2015-10-23   |   分类于 iOS技术积累   |     |   阅读次数

都是项目中常用的,16进制色值转换,默认字体设置字号大小,获取当前Version,Build,沙盒路径,缓存路径,屏幕物理宽、高,基于iPhone5尺寸的缩放值,当前手机版本,weakself等等。

  • 2015.04.27 更新
    新增比较当前系统版本号与输入值(NSString)
1
2
3
4
5
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

需要的就直接拷走好啦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*--------------------------------开发中常用到的宏定义-----------------------------------*/
// 颜色的转换
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
//16进制色值
#define HexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HexRGBAlpha(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]
//设置系统默认字体的字号
#define FONT(x) [UIFont systemFontOfSize:x]
//当前build号
#define Current_Build [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
#define Current_Version [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
//常用系统库
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]
#define NOTIFICATION_CENTER [NSNotificationCenter defaultCenter]
//沙盒存储路径
#define HomeDirectory [NSHomeDirectory() stringByAppendingString:@"/Documents/"]
//缓存路径
#define APP_CACHES_PATH [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
//屏幕物理宽、高
#define APP_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define APP_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
//与iPhone5比较的尺寸缩放值
#define kScaleFrom_iPhone5_Desgin(_X_) (_X_ * (APP_SCREEN_WIDTH/320))
//手机版本
#define Current_device_vesion [[[UIDevice currentDevice] systemVersion] floatValue]
//按屏幕尺寸确定手机
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
//比较当前系统版本号与输入值(NSString)
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
//weakself 用与防止循环引用
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

以后遇到新的好用的宏定义也会添加到这里,谢谢您的阅读。

yunyang088

yunyang088

Stay hungry, Stay foolish

3 日志
1 分类
4 标签
GitHub Twitter Facebook 微博
© 2017 yunyang088
由 Hexo 强力驱动
主题 - NexT.Pisces