Skip to content

Commit

Permalink
Release 4.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jg zhu committed Mar 4, 2022
1 parent f8c52cd commit e034e22
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 36 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 = "4.2.2"
s.version = "4.2.3"
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ - (void)correctAnonymizationID:(NSString *)anonymizationID {
}
}

- (void)correctDeviceID:(NSString *)deviceID {
if (self.properties[@"$device_id"] && deviceID) {
self.properties[@"$device_id"] = deviceID;
}
}

- (id)sensorsdata_validKey:(NSString *)key value:(id)value error:(NSError *__autoreleasing _Nullable *)error {
if (![key conformsToProtocol:@protocol(SAPropertyKeyProtocol)]) {
*error = SAPropertyError(10004, @"Property Key: %@ must be a string", key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ NS_ASSUME_NONNULL_BEGIN
/// @param anonymizationID anonymizationID
- (void)correctAnonymizationID:(NSString *)anonymizationID;


/// 修正 $device_id
/// @param deviceID deviceID
- (void)correctDeviceID:(NSString *)deviceID;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
NS_ASSUME_NONNULL_BEGIN

extern NSString * const kSADeviceIDPropertyPluginAnonymizationID;
extern NSString * const kSADeviceIDPropertyPluginDeviceID;

@interface SADeviceIDPropertyPlugin : NSObject <SAPropertyPluginProtocol>

@property (nonatomic, assign) BOOL disableDeviceId;

@property (nonatomic, strong) NSDictionary<NSString *, id> *properties;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "SAIdentifier.h"

NSString * const kSADeviceIDPropertyPluginAnonymizationID = @"$anonymization_id";
NSString *const kSADeviceIDPropertyPluginDeviceID = @"$device_id";

@implementation SADeviceIDPropertyPlugin

Expand All @@ -39,9 +40,10 @@ - (SAPropertyPluginPriority)priority {
}

- (void)start {
NSData *data = [[SAIdentifier hardwareID] dataUsingEncoding:NSUTF8StringEncoding];
NSString *hardwareID = [SAIdentifier hardwareID];
NSData *data = [hardwareID dataUsingEncoding:NSUTF8StringEncoding];
NSString *anonymizationID = [data base64EncodedStringWithOptions:0];
self.properties = @{kSADeviceIDPropertyPluginAnonymizationID: anonymizationID};
self.properties = self.disableDeviceId ? @{kSADeviceIDPropertyPluginAnonymizationID: anonymizationID} : @{kSADeviceIDPropertyPluginDeviceID: hardwareID};
}

@end
6 changes: 5 additions & 1 deletion SensorsAnalyticsSDK/Core/SAAppExtensionDataManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ - (BOOL)writeEvent:(NSString *)eventName properties:(NSDictionary *)properties g
NSDictionary *event = @{@"event": eventName, @"properties": properties?properties:@{}};
NSString *path = [self filePathForApplicationGroupIdentifier:groupIdentifier];
if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
NSDictionary *attributes = nil;
#if TARGET_OS_IOS
attributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
#endif
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:attributes];
}
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
if (array.count) {
Expand Down
3 changes: 3 additions & 0 deletions SensorsAnalyticsSDK/Core/SAConfigOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ NS_ASSUME_NONNULL_BEGIN
/// 是否进行 session 切割。默认 NO,不会进行 session 切割;设置 YES,会进行 session 切割
@property (nonatomic, assign) BOOL enableSession;

/// 是否禁用采集 deviceId
@property (nonatomic, assign) BOOL disableDeviceId;

- (void)registerStorePlugin:(id<SAStorePlugin>)plugin;

@end
Expand Down
1 change: 1 addition & 0 deletions SensorsAnalyticsSDK/Core/SAConfigOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ - (id)copyWithZone:(nullable NSZone *)zone {
options.storePlugins = self.storePlugins;
options.loginIDKey = self.loginIDKey;
options.enableSession = self.enableSession;
options.disableDeviceId = self.disableDeviceId;

#if TARGET_OS_IOS
// 支持 https 自签证书
Expand Down
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/Core/SAHTTPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ - (instancetype)init {
_delegateQueue.name = [NSString stringWithFormat:@"cn.sensorsdata.SAHTTPSession.%p", self];
_delegateQueue.maxConcurrentOperationCount = 1;

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.timeoutIntervalForRequest = 30.0;
config.HTTPShouldUsePipelining = NO;
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:_delegateQueue];
Expand Down
6 changes: 5 additions & 1 deletion SensorsAnalyticsSDK/Core/SALogger/SAFileLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ - (nullable NSString *)currentlogFile {
NSLog(@"SAFileLogger file directory created failed");
return nil;
}
BOOL fileCreated = [[NSFileManager defaultManager] createFileAtPath:logfilePath contents:nil attributes:nil];
NSDictionary *attributes = nil;
#if TARGET_OS_IOS
attributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
#endif
BOOL fileCreated = [[NSFileManager defaultManager] createFileAtPath:logfilePath contents:nil attributes:attributes];
if (!fileCreated) {
NSLog(@"SAFileLogger file created failed");
return nil;
Expand Down
6 changes: 3 additions & 3 deletions SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK+Public.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ extern NSString * const SensorsAnalyticsIdentityKeyEmail;
* 如果满足这两个条件之一,则向服务器发送一次数据;如果都不满足,则把数据加入到队列中,等待下次检查时把整个队列的内容一并发送。
* 需要注意的是,为了避免占用过多存储,队列最多只缓存10000条数据。
*/
@property (atomic) UInt64 flushInterval __attribute__((deprecated("已过时,请参考 SAConfigOptions 类的 flushInterval")));
@property (atomic) NSInteger flushInterval __attribute__((deprecated("已过时,请参考 SAConfigOptions 类的 flushInterval")));

/**
* @property
Expand All @@ -789,7 +789,7 @@ extern NSString * const SensorsAnalyticsIdentityKeyEmail;
* 如果同时满足这两个条件,则向服务器发送一次数据;如果不满足,则把数据加入到队列中,等待下次检查时把整个队列的内容一并发送。
* 需要注意的是,为了避免占用过多存储,队列最多只缓存 10000 条数据。
*/
@property (atomic) UInt64 flushBulkSize __attribute__((deprecated("已过时,请参考 SAConfigOptions 类的 flushBulkSize")));
@property (atomic) NSInteger flushBulkSize __attribute__((deprecated("已过时,请参考 SAConfigOptions 类的 flushBulkSize")));

/**
* @abstract
Expand All @@ -799,7 +799,7 @@ extern NSString * const SensorsAnalyticsIdentityKeyEmail;
* 默认为 10000 条事件
*
*/
@property (nonatomic) UInt64 maxCacheSize __attribute__((deprecated("已过时,请参考 SAConfigOptions 类的 maxCacheSize")));
@property (nonatomic) NSInteger maxCacheSize __attribute__((deprecated("已过时,请参考 SAConfigOptions 类的 maxCacheSize")));

/**
* @abstract
Expand Down
43 changes: 23 additions & 20 deletions SensorsAnalyticsSDK/Core/SensorsAnalyticsSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#import "SAUserDefaultsStorePlugin.h"
#import "SASessionProperty.h"

#define VERSION @"4.2.2"
#define VERSION @"4.2.3"

void *SensorsAnalyticsQueueTag = &SensorsAnalyticsQueueTag;

Expand Down Expand Up @@ -283,6 +283,7 @@ - (void)registerPropertyPlugin {
[[SAPropertyPluginManager sharedInstance] registerPropertyPlugin:appVersionPlugin];

SADeviceIDPropertyPlugin *deviceIDPlugin = [[SADeviceIDPropertyPlugin alloc] init];
deviceIDPlugin.disableDeviceId = self.configOptions.disableDeviceId;
[[SAPropertyPluginManager sharedInstance] registerPropertyPlugin:deviceIDPlugin];
});
}
Expand Down Expand Up @@ -629,8 +630,17 @@ - (void)trackEventObject:(SABaseEventObject *)object properties:(NSDictionary *)
// 6. 添加 $event_session_id
[object addSessionPropertiesWithObject:self.sessionProperty];

// 公共属性, 动态公共属性, 自定义属性不允许修改 $anonymization_id 属性, 因此需要将修正逻操作放在所有属性添加后
[object correctAnonymizationID:dic[kSADeviceIDPropertyPluginAnonymizationID]];
// 公共属性, 动态公共属性, 自定义属性不允许修改 $anonymization_id、$device_id 属性, 因此需要将修正逻操作放在所有属性添加后
if (self.configOptions.disableDeviceId) {
[object correctAnonymizationID:dic[kSADeviceIDPropertyPluginAnonymizationID]];
//不允许客户设置 $device_id
[object.properties removeObjectForKey:kSADeviceIDPropertyPluginDeviceID];
} else {
[object correctDeviceID:dic[kSADeviceIDPropertyPluginDeviceID]];
//不允许客户设置 $anonymization_id
[object.properties removeObjectForKey:kSADeviceIDPropertyPluginAnonymizationID];
}


// 7. trackEventCallback 接口调用
if (![self willEnqueueWithObject:object]) {
Expand Down Expand Up @@ -1362,49 +1372,42 @@ - (void)deleteUser {
#pragma mark - Deprecated
@implementation SensorsAnalyticsSDK (Deprecated)

- (UInt64)flushInterval {
- (NSInteger)flushInterval {
@synchronized(self) {
return self.configOptions.flushInterval;
}
}

- (void)setFlushInterval:(UInt64)interval {
- (void)setFlushInterval:(NSInteger)interval {
@synchronized(self) {
if (interval < 5 * 1000) {
interval = 5 * 1000;
}
self.configOptions.flushInterval = (NSInteger)interval;
self.configOptions.flushInterval = interval;
}
[self flush];
[self stopFlushTimer];
[self startFlushTimer];
}

- (UInt64)flushBulkSize {
- (NSInteger)flushBulkSize {
@synchronized(self) {
return self.configOptions.flushBulkSize;
}
}

- (void)setFlushBulkSize:(UInt64)bulkSize {
- (void)setFlushBulkSize:(NSInteger)bulkSize {
@synchronized(self) {
//加上最小值保护,50
NSInteger newBulkSize = (NSInteger)bulkSize;
self.configOptions.flushBulkSize = newBulkSize >= 50 ? newBulkSize : 50;
self.configOptions.flushBulkSize = bulkSize;
}
}

- (void)setMaxCacheSize:(UInt64)maxCacheSize {
- (void)setMaxCacheSize:(NSInteger)maxCacheSize {
@synchronized(self) {
//防止设置的值太小导致事件丢失
UInt64 temMaxCacheSize = maxCacheSize > 10000 ? maxCacheSize : 10000;
self.configOptions.maxCacheSize = (NSInteger)temMaxCacheSize;
self.configOptions.maxCacheSize = maxCacheSize;
};
}

- (UInt64)maxCacheSize {
- (NSInteger)maxCacheSize {
@synchronized(self) {
return (UInt64)self.configOptions.maxCacheSize;
return self.configOptions.maxCacheSize;
};
}

Expand Down
8 changes: 8 additions & 0 deletions SensorsAnalyticsSDK/Core/Tracker/SADatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ - (BOOL)execUpdateSQL:(NSString *)sql {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(_database, sql.UTF8String, -1, &stmt, NULL) != SQLITE_OK) {
SALogError(@"Prepare update records query failure: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NO;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
SALogError(@"Failed to update records from database, error: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NO;
}
sqlite3_finalize(stmt);
Expand Down Expand Up @@ -280,6 +282,7 @@ - (BOOL)deleteRecords:(NSArray<NSString *> *)recordIDs {

if (sqlite3_prepare_v2(_database, query.UTF8String, -1, &stmt, NULL) != SQLITE_OK) {
SALogError(@"Prepare delete records query failure: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NO;
}
BOOL success = YES;
Expand All @@ -305,6 +308,7 @@ - (BOOL)deleteFirstRecords:(NSUInteger)recordSize {

if (sqlite3_prepare_v2(_database, query.UTF8String, -1, &stmt, NULL) != SQLITE_OK) {
SALogError(@"Prepare delete records query failure: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NO;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
Expand Down Expand Up @@ -363,6 +367,7 @@ - (sqlite3_stmt *)dbCacheStmt:(NSString *)sql {
int result = sqlite3_prepare_v2(_database, sql.UTF8String, -1, &stmt, NULL);
if (result != SQLITE_OK) {
SALogError(@"sqlite stmt prepare error (%d): %s", result, sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NULL;
}
CFDictionarySetValue(_dbStmtCache, (__bridge const void *)(sql), stmt);
Expand All @@ -385,6 +390,7 @@ - (BOOL)columnExists:(NSString *)columnName inTable:(NSString *)tableName {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(_database, query.UTF8String, -1, &stmt, NULL) != SQLITE_OK) {
SALogError(@"Prepare PRAGMA table_info query failure: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return columns;
}

Expand Down Expand Up @@ -412,10 +418,12 @@ - (BOOL)createColumn:(NSString *)columnName inTable:(NSString *)tableName {

if (sqlite3_prepare_v2(_database, query.UTF8String, -1, &stmt, NULL) != SQLITE_OK) {
SALogError(@"Prepare create column query failure: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NO;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
SALogError(@"Failed to create column, error: %s", sqlite3_errmsg(_database));
sqlite3_finalize(stmt);
return NO;
}
sqlite3_finalize(stmt);
Expand Down
2 changes: 2 additions & 0 deletions SensorsAnalyticsSDK/Core/Tracker/SAEventFlush.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ - (NSString *)buildFlushEncryptJSONStringWithEventRecords:(NSArray<SAEventRecord
// 用于保存当前存在的所有 ekey
NSMutableArray *ekeys = [NSMutableArray arrayWithCapacity:records.count];
for (SAEventRecord *record in records) {
if (!record.ekey) continue;

NSInteger index = [ekeys indexOfObject:record.ekey];
if (index == NSNotFound) {
[record removePayload];
Expand Down
4 changes: 3 additions & 1 deletion SensorsAnalyticsSDK/Core/Tracker/SAEventRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ - (void)setSecretObject:(NSDictionary *)obj {
}

- (void)removePayload {
if (!_event[SAEncryptRecordKeyPayload]) {
return;
}
_event[SAEncryptRecordKeyPayloads] = [NSMutableArray arrayWithObject:_event[SAEncryptRecordKeyPayload]];
[_event removeObjectForKey:SAEncryptRecordKeyPayload];
}
Expand All @@ -107,7 +110,6 @@ - (BOOL)mergeSameEKeyRecord:(SAEventRecord *)record {
return NO;
}
[(NSMutableArray *)_event[SAEncryptRecordKeyPayloads] addObject:record.event[SAEncryptRecordKeyPayload]];
[_event removeObjectForKey:SAEncryptRecordKeyPayload];
return YES;
}

Expand Down
1 change: 1 addition & 0 deletions SensorsAnalyticsSDK/Encrypt/SARSAEncryptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ - (SecKeyRef)addPublicKey:(NSString *)aymmetricPublicKey {
kSecAttrKeyClass];
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)
kSecReturnPersistentRef];
[publicKey setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlock forKey:(__bridge id)kSecAttrAccessible];

CFTypeRef persistKey = nil;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
Expand Down
21 changes: 15 additions & 6 deletions SensorsAnalyticsSDK/Store/SAAESCrypt.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ - (nullable NSString *)encryptData:(NSData *)obj {
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

unsigned char buf[16];
arc4random_buf(buf, sizeof(buf));
NSData *ivData = [NSData dataWithBytes:buf length:sizeof(buf)];
NSMutableData *iv = [NSMutableData dataWithLength:kCCBlockSizeAES128];
int result = SecRandomCopyBytes(kSecRandomDefault, kCCBlockSizeAES128, iv.mutableBytes);
if (result != errSecSuccess) {
return nil;
}

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[self.key bytes],
kCCBlockSizeAES128,
[ivData bytes],
[iv bytes],
[data bytes],
dataLength,
buffer,
Expand All @@ -86,7 +88,7 @@ - (nullable NSString *)encryptData:(NSData *)obj {
if (cryptStatus == kCCSuccess) {
// 获得加密内容后,在内容前添加 16 位随机字节,增加数据复杂度
NSData *encryptData = [NSData dataWithBytes:buffer length:numBytesEncrypted];
NSMutableData *ivEncryptData = [NSMutableData dataWithData:ivData];
NSMutableData *ivEncryptData = [NSMutableData dataWithData:iv];
[ivEncryptData appendData:encryptData];

free(buffer);
Expand All @@ -112,12 +114,19 @@ - (nullable NSData *)decryptData:(NSData *)obj {
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;

NSMutableData *iv = [NSMutableData dataWithLength:kCCBlockSizeAES128];
int result = SecRandomCopyBytes(kSecRandomDefault, kCCBlockSizeAES128, iv.mutableBytes);
if (result != errSecSuccess) {
return nil;
}

CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[self.key bytes],
kCCBlockSizeAES128,
NULL,
[iv bytes],
[encryptedData bytes],
[encryptedData length],
buffer,
Expand Down

0 comments on commit e034e22

Please sign in to comment.