Skip to content

Commit

Permalink
Release 2.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujg-00 committed May 11, 2021
1 parent 822f740 commit 4a04394
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SensorsAnalyticsSDK"
s.version = "2.6.1"
s.version = "2.6.2"
s.summary = "The official iOS SDK of Sensors Analytics."
s.homepage = "http://www.sensorsdata.cn"
s.source = { :git => 'https://github.com/sensorsdata/sa-sdk-ios.git', :tag => "v#{s.version}" }
Expand Down
12 changes: 8 additions & 4 deletions SensorsAnalyticsSDK/Core/HookDelegate/NSObject+DelegateProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ NS_ASSUME_NONNULL_BEGIN

@interface NSObject (DelegateProxy)

/// 用于记录创建子类时的原始父类名称
@property (nonatomic, copy, nullable) NSString *sensorsdata_className;
/// 用于记录创建子类所继承的父类信息
@property (nonatomic, copy, nullable) NSString *sensorsdata_superClassName;
/// 用于记录 - class 方法需要返回的信息
@property (nonatomic, copy, nullable) NSString *sensorsdata_delegateClassName;
@property (nonatomic, copy, nullable) NSSet<NSString *> *sensorsdata_selectors;
@property (nonatomic, copy, nullable) NSSet<NSString *> *sensorsdata_optionalSelectors;
@property (nonatomic, strong, nullable) id sensorsdata_delegateProxy;
Expand All @@ -50,8 +52,10 @@ NS_ASSUME_NONNULL_BEGIN

@interface NSProxy (DelegateProxy)

/// 用于记录创建子类时的原始父类名称
@property (nonatomic, copy, nullable) NSString *sensorsdata_className;
/// 用于记录创建子类所继承的父类信息
@property (nonatomic, copy, nullable) NSString *sensorsdata_superClassName;
/// 用于记录 - class 方法需要返回的信息
@property (nonatomic, copy, nullable) NSString *sensorsdata_delegateClassName;
@property (nonatomic, copy, nullable) NSSet<NSString *> *sensorsdata_selectors;
@property (nonatomic, copy, nullable) NSSet<NSString *> *sensorsdata_optionalSelectors;
@property (nonatomic, strong, nullable) id sensorsdata_delegateProxy;
Expand Down
35 changes: 26 additions & 9 deletions SensorsAnalyticsSDK/Core/HookDelegate/NSObject+DelegateProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
#import <objc/runtime.h>

static void *const kSANSObjectDelegateProxyParasiteKey = (void *)&kSANSObjectDelegateProxyParasiteKey;
static void *const kSANSObjectDelegateProxyClassNameKey = (void *)&kSANSObjectDelegateProxyClassNameKey;
static void *const kSANSObjectDelegateProxySuperClassNameKey = (void *)&kSANSObjectDelegateProxySuperClassNameKey;
static void *const kSANSObjectDelegateProxyDelegateClassNameKey = (void *)&kSANSObjectDelegateProxyDelegateClassNameKey;
static void *const kSANSObjectDelegateSelectorsKey = (void *)&kSANSObjectDelegateSelectorsKey;
static void *const kSANSObjectDelegateOptionalSelectorsKey = (void *)&kSANSObjectDelegateOptionalSelectorsKey;
static void *const kSANSObjectDelegateProxyKey = (void *)&kSANSObjectDelegateProxyKey;
Expand Down Expand Up @@ -55,12 +56,20 @@ - (void)setSensorsdata_parasite:(SADelegateProxyParasite *)sensorsdata_parasite
objc_setAssociatedObject(self, kSANSObjectDelegateProxyParasiteKey, sensorsdata_parasite, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)sensorsdata_className {
return objc_getAssociatedObject(self, kSANSObjectDelegateProxyClassNameKey);
- (NSString *)sensorsdata_superClassName {
return objc_getAssociatedObject(self, kSANSObjectDelegateProxySuperClassNameKey);
}

- (void)setSensorsdata_className:(NSString *)sensorsdata_className {
objc_setAssociatedObject(self, kSANSObjectDelegateProxyClassNameKey, sensorsdata_className, OBJC_ASSOCIATION_COPY);
- (void)setSensorsdata_superClassName:(NSString *)sensorsdata_superClassName {
objc_setAssociatedObject(self, kSANSObjectDelegateProxySuperClassNameKey, sensorsdata_superClassName, OBJC_ASSOCIATION_COPY);
}

- (NSString *)sensorsdata_delegateClassName {
return objc_getAssociatedObject(self, kSANSObjectDelegateProxyDelegateClassNameKey);
}

- (void)setSensorsdata_delegateClassName:(NSString *)sensorsdata_delegateClassName {
objc_setAssociatedObject(self, kSANSObjectDelegateProxyDelegateClassNameKey, sensorsdata_delegateClassName, OBJC_ASSOCIATION_COPY);
}

- (NSSet<NSString *> *)sensorsdata_selectors {
Expand Down Expand Up @@ -117,12 +126,20 @@ - (void)setSensorsdata_parasite:(SADelegateProxyParasite *)sensorsdata_parasite
objc_setAssociatedObject(self, kSANSProxyDelegateProxyParasiteKey, sensorsdata_parasite, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)sensorsdata_className {
return objc_getAssociatedObject(self, kSANSProxyDelegateProxyClassNameKey);
- (NSString *)sensorsdata_superClassName {
return objc_getAssociatedObject(self, kSANSObjectDelegateProxySuperClassNameKey);
}

- (void)setSensorsdata_superClassName:(NSString *)sensorsdata_superClassName {
objc_setAssociatedObject(self, kSANSObjectDelegateProxySuperClassNameKey, sensorsdata_superClassName, OBJC_ASSOCIATION_COPY);
}

- (NSString *)sensorsdata_delegateClassName {
return objc_getAssociatedObject(self, kSANSObjectDelegateProxyDelegateClassNameKey);
}

- (void)setSensorsdata_className:(NSString *)sensorsdata_className {
objc_setAssociatedObject(self, kSANSProxyDelegateProxyClassNameKey, sensorsdata_className, OBJC_ASSOCIATION_COPY);
- (void)setSensorsdata_delegateClassName:(NSString *)sensorsdata_delegateClassName {
objc_setAssociatedObject(self, kSANSObjectDelegateProxyDelegateClassNameKey, sensorsdata_delegateClassName, OBJC_ASSOCIATION_COPY);
}

- (NSSet<NSString *> *)sensorsdata_selectors {
Expand Down
24 changes: 14 additions & 10 deletions SensorsAnalyticsSDK/Core/HookDelegate/SADelegateProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ @implementation SADelegateProxy

/// Overridden instance class method
- (Class)class {
if (self.sensorsdata_className) {
return NSClassFromString(self.sensorsdata_className);
if (self.sensorsdata_delegateClassName) {
return NSClassFromString(self.sensorsdata_delegateClassName);
}
return [super class];
}
Expand All @@ -49,7 +49,7 @@ + (void)proxyDelegate:(id)delegate selectors:(NSSet<NSString *> *)selectors {
NSMutableSet *delegateSelectors = [NSMutableSet setWithSet:selectors];

// 当前代理对象已经处理过
if ([delegate sensorsdata_className]) {
if ([delegate sensorsdata_superClassName]) {
NSMutableSet *currentSelectors = [NSMutableSet setWithSet:[delegate sensorsdata_selectors]];
if (currentSelectors.count > 0) {
[delegateSelectors minusSet:currentSelectors];
Expand All @@ -72,8 +72,10 @@ + (void)proxyDelegate:(id)delegate selectors:(NSSet<NSString *> *)selectors {
Class realClass = [SAClassHelper realClassWithObject:delegate];
// 如果当前代理对象归属为 KVO 创建的类, 则无需新建子类
if ([self isKVOClass:realClass]) {
// 记录 KVO 的父类(KVO 会重写 class 方法, 返回父类)
[delegate setSensorsdata_className:NSStringFromClass([delegate class])];
// KVO 的类, SDK 未新建子类
[delegate setSensorsdata_superClassName:NSStringFromClass([SAClassHelper realSuperClassWithClass:realClass])];
// 记录 - class 方法所返回的信息, 用于重写 - class 方法
[delegate setSensorsdata_delegateClassName:NSStringFromClass([delegate class])];
if ([realClass isKindOfClass:[NSObject class]]) {
// 在移除所有的 KVO 属性监听时, 系统会重置对象的 isa 指针为原有的类; 因此需要在移除监听时, 重新为代理对象设置新的子类, 来采集点击事件
[SAMethodHelper addInstanceMethodWithSelector:@selector(removeObserver:forKeyPath:) fromClass:proxyClass toClass:realClass];
Expand All @@ -99,8 +101,10 @@ + (void)proxyDelegate:(id)delegate selectors:(NSSet<NSString *> *)selectors {
[SAMethodHelper addInstanceMethodWithSelector:@selector(addObserver:forKeyPath:options:context:) fromClass:proxyClass toClass:dynamicClass];
}

// 记录对象的原始类名 (因为 class 方法需要使用, 所以在重写 class 方法前设置)
[delegate setSensorsdata_className:NSStringFromClass(realClass)];
// 记录新建子类所继承的父类信息, 用于向父类发送消息
[delegate setSensorsdata_superClassName:NSStringFromClass(realClass)];
// 记录 - class 方法所返回的信息, 用于重写 - class 方法
[delegate setSensorsdata_delegateClassName:NSStringFromClass([delegate class])];
// 重写 - (Class)class 方法,隐藏新添加的子类
[SAMethodHelper addInstanceMethodWithSelector:@selector(class) fromClass:proxyClass toClass:dynamicClass];

Expand All @@ -126,7 +130,7 @@ + (void)addInstanceMethodWithSelectors:(NSSet<NSString *> *)selectors fromClass:
}

+ (void)invokeWithTarget:(NSObject *)target selector:(SEL)selector, ... {
Class originalClass = NSClassFromString(target.sensorsdata_className) ?: target.superclass;
Class originalClass = NSClassFromString(target.sensorsdata_superClassName) ?: target.superclass;

va_list args;
va_start(args, selector);
Expand Down Expand Up @@ -174,7 +178,7 @@ @implementation SADelegateProxy (KVO)

- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {
[super addObserver:observer forKeyPath:keyPath options:options context:context];
if (self.sensorsdata_className) {
if (self.sensorsdata_superClassName) {
// 由于添加了 KVO 属性监听, KVO 会创建子类并重写 Class 方法,返回原始类; 此时的原始类为神策添加的子类,因此需要重写 class 方法
[SAMethodHelper replaceInstanceMethodWithDestinationSelector:@selector(class) sourceSelector:@selector(class) fromClass:SADelegateProxy.class toClass:[SAClassHelper realClassWithObject:self]];
}
Expand All @@ -190,7 +194,7 @@ - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath {
// 有多个属性监听时, 在最后一个监听被移除后, 对象的 isa 发生变化, 需要重新为代理对象添加子类
if (oldClassIsKVO && !newClassIsKVO) {
// 清空已经记录的原始类
self.sensorsdata_className = nil;
self.sensorsdata_superClassName = nil;
Class delegateProxy = [self.sensorsdata_delegateProxy class];
if ([delegateProxy respondsToSelector:NSSelectorFromString(@"proxyDelegate:selectors:")]) {
[delegateProxy proxyDelegate:self selectors:self.sensorsdata_selectors];
Expand Down
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#import "SAModuleManager.h"
#import "SAReferrerManager.h"

#define VERSION @"2.6.1"
#define VERSION @"2.6.2"

static NSUInteger const SA_PROPERTY_LENGTH_LIMITATION = 8191;

Expand Down

0 comments on commit 4a04394

Please sign in to comment.