Commit d71e537d authored by Thierry's avatar Thierry

完善SDK,将userId作为参数暴露出来

parent a9d1d618
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* *
* @return * @return
* *
* @since <#1.8.0#> * @since 0.0.1
*/ */
+ (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId; + (Phobos *)clientWithAppName:(NSString *)appName channelId:(NSString *)channelId;
...@@ -32,10 +32,18 @@ ...@@ -32,10 +32,18 @@
* *
* @param value 设置为YES,phobos SDK 会输出log信息,记得release产品时要设置回NO. * @param value 设置为YES,phobos SDK 会输出log信息,记得release产品时要设置回NO.
* *
* @since <#1.8.0#> * @since 0.0.1
*/ */
- (void)setLogEnabled:(BOOL)value; - (void)setLogEnabled:(BOOL)value;
/**
* @brief 设置当前登录用户的ID,如果没有默认为@""
*
* @param userId
*
* @since 0.0.2
*/
- (void)setUserId:(NSInteger)userId;
/** /**
* @brief 自定义事件,数量统计. * @brief 自定义事件,数量统计.
...@@ -44,7 +52,7 @@ ...@@ -44,7 +52,7 @@
* @attributes 参数 * @attributes 参数
* @sendNow 是否实时发送,默认为NO * @sendNow 是否实时发送,默认为NO
* *
* @since <#1.8.0#> * @since 0.0.1
*/ */
- (void)track:(NSString *)eventId; - (void)track:(NSString *)eventId;
- (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes; - (void)track:(NSString *)eventId attributes:(NSDictionary *)attributes;
......
...@@ -8,9 +8,15 @@ ...@@ -8,9 +8,15 @@
#import "Phobos.h" #import "Phobos.h"
#import <AdSupport/AdSupport.h> #import <AdSupport/AdSupport.h>
#import "zlib.h" #import <zlib.h>
#import "WMCacheService.h" #import "WMCacheService.h"
#ifdef DEBUG
#define phobosLog(...) NSLog(@"[Phobos] %@",__VA_ARGS__)
#else
#define phobosLog(...)
#endif
#define PhobosHaveOpenApp @"PhobosHaveOpenApp" //是否打开过APP #define PhobosHaveOpenApp @"PhobosHaveOpenApp" //是否打开过APP
#define PhobosBeginTime @"PhobosBeginTime" //记录APP打开|从后台启动时的时间戳 #define PhobosBeginTime @"PhobosBeginTime" //记录APP打开|从后台启动时的时间戳
#define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳 #define PhobosEndTime @"PhobosEndTime" //记录APP退出|退到后台时的时间戳
...@@ -18,7 +24,7 @@ ...@@ -18,7 +24,7 @@
#define PhobosShardCount 50 //收集数据分段发送的个数 #define PhobosShardCount 50 //收集数据分段发送的个数
static Phobos *sharedClient = nil; static Phobos *sharedClient = nil;
static NSString *sdkVersion = @"103"; static NSString *sdkVersion = @"110";
@interface Phobos () @interface Phobos ()
...@@ -26,6 +32,7 @@ static NSString *sdkVersion = @"103"; ...@@ -26,6 +32,7 @@ static NSString *sdkVersion = @"103";
@property (strong, nonatomic) NSString *appName; @property (strong, nonatomic) NSString *appName;
@property (strong, nonatomic) NSString *channelId; @property (strong, nonatomic) NSString *channelId;
@property (strong, nonatomic) NSString *appVersion; @property (strong, nonatomic) NSString *appVersion;
@property (assign, nonatomic) NSInteger userId;
@property (assign, nonatomic) BOOL logEnabled; @property (assign, nonatomic) BOOL logEnabled;
@end @end
...@@ -50,9 +57,12 @@ static NSString *sdkVersion = @"103"; ...@@ -50,9 +57,12 @@ static NSString *sdkVersion = @"103";
_appName = appName; _appName = appName;
_channelId = channelId; _channelId = channelId;
_logEnabled = NO; _logEnabled = NO;
_userId = 0;
_appVersion = [self getAppVersion]; _appVersion = [self getAppVersion];
[self setupNotification]; [self setupNotification];
[self handleEventAfterInit]; [self handleEventAfterInit];
phobosLog(@"starts to orbit");
} }
return self; return self;
} }
...@@ -61,6 +71,10 @@ static NSString *sdkVersion = @"103"; ...@@ -61,6 +71,10 @@ static NSString *sdkVersion = @"103";
_logEnabled = value; _logEnabled = value;
} }
- (void)setUserId:(NSInteger)userId{
_userId = userId;
}
- (void)handleEventAfterInit{ - (void)handleEventAfterInit{
WMCacheService *cache = [WMCacheService sharedInstance]; WMCacheService *cache = [WMCacheService sharedInstance];
...@@ -79,36 +93,69 @@ static NSString *sdkVersion = @"103"; ...@@ -79,36 +93,69 @@ static NSString *sdkVersion = @"103";
/** /**
* @brief 设置对APP的通知监听 * @brief 设置对APP的通知监听
* *
* @since <#1.8.0#> * @since 0.0.1
*/ */
- (void)setupNotification{ - (void)setupNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppFinishLaunch:) name:UIApplicationDidFinishLaunchingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppInForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppInForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppInBackgound:) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppInBackgound:) name:UIApplicationDidEnterBackgroundNotification object:nil];
} }
/**
* @brief 每次打开APP或返回前台,即Session开始的时候的处理
* @notification didFinishLaunch和willEnterForeground的时候都需要记录
*
* @since 0.0.1
*/
- (void)handleSessionStart{
WMCacheService *cache = [WMCacheService sharedInstance];
[cache storeObjectAtDiskWithkey:PhobosBeginTime object:[self currentTime]];
}
/**
* @brief 应用打开时的处理
*
* @param sender
*
* @since 0.0.1
*/
- (void)handleAppFinishLaunch:(id)sender{
phobosLog(@"handleAppFinishLaunch");
[self handleSessionStart];
}
/** /**
* @brief 应用进入前台的处理 * @brief 应用进入前台的处理
* *
* @param sender <#sender description#> * @param sender
* *
* @since <#1.8.0#> * @since 0.0.1
*/ */
- (void)handleAppInForeground:(id)sender{ - (void)handleAppInForeground:(id)sender{
phobosLog(@"handleAppInForeground");
//记录每次打开APP的时间,也包括从后台进入前台 [self handleSessionStart];
WMCacheService *cache = [WMCacheService sharedInstance];
[cache storeObjectAtDiskWithkey:PhobosBeginTime object:[self currentTime]];
[self fetchDataAndSend]; [self fetchDataAndSend];
} }
/** /**
* @brief 应用进入后台的处理 * @brief 应用进入后台的处理
* *
* @param sender <#sender description#> * @param sender
* *
* @since <#1.8.0#> * @since 0.0.1
*/ */
- (void)handleAppInBackgound:(id)sender{ - (void)handleAppInBackgound:(id)sender{
phobosLog(@"handleAppInBackgound");
[self handleSessionOver];
[self fetchDataAndSend];
}
/**
* @brief 会话结束时候的处理
*
* @since 0.0.1
*/
- (void)handleSessionOver{
WMCacheService *cache = [WMCacheService sharedInstance]; WMCacheService *cache = [WMCacheService sharedInstance];
//进入后台的同时,把记录时间同步到服务端,把已使用时间清空 //进入后台的同时,把记录时间同步到服务端,把已使用时间清空
double beginTime = [[cache fetchObjectAtDiskWithkey:PhobosBeginTime] doubleValue]; double beginTime = [[cache fetchObjectAtDiskWithkey:PhobosBeginTime] doubleValue];
...@@ -121,7 +168,6 @@ static NSString *sdkVersion = @"103"; ...@@ -121,7 +168,6 @@ static NSString *sdkVersion = @"103";
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:usedTime,@"duration",nil]; NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:usedTime,@"duration",nil];
[self track:@"on_app_session_over" attributes:dict]; [self track:@"on_app_session_over" attributes:dict];
[cache removeObjectAtDiskWithkey:PhobosBeginTime]; [cache removeObjectAtDiskWithkey:PhobosBeginTime];
[self fetchDataAndSend];
} }
...@@ -155,13 +201,12 @@ static NSString *sdkVersion = @"103"; ...@@ -155,13 +201,12 @@ static NSString *sdkVersion = @"103";
* *
* @return * @return
* *
* @since <#1.8.0#> * @since 1.8.0
*/ */
- (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{ - (NSDictionary *)prepareDictionaryForEvent:(NSString *)eventId attributes:(NSDictionary *)attributes{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
@try { @try {
NSString *currentTime = [self currentTime]; NSString *currentTime = [self currentTime];
// long userId = [WMLoginManager shareInstance].user.userId;
NSMutableDictionary *deviceParams = [NSMutableDictionary dictionaryWithObjectsAndKeys: NSMutableDictionary *deviceParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString],@"device_id", [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString],@"device_id",
@"ios",@"device_type",nil]; @"ios",@"device_type",nil];
...@@ -173,12 +218,12 @@ static NSString *sdkVersion = @"103"; ...@@ -173,12 +218,12 @@ static NSString *sdkVersion = @"103";
[dict setObject:appParams forKey:@"app"]; [dict setObject:appParams forKey:@"app"];
[dict setObject:sdkVersion forKey:@"version"]; [dict setObject:sdkVersion forKey:@"version"];
[dict setObject:deviceParams forKey:@"device"]; [dict setObject:deviceParams forKey:@"device"];
// [dict setObject:@(userId) forKey:@"user_id"]; [dict setObject:@(_userId) forKey:@"user_id"];
[dict setObject:currentTime forKey:@"create_at"]; [dict setObject:currentTime forKey:@"create_at"];
[dict setObject:attributes forKey:@"params"]; [dict setObject:attributes forKey:@"params"];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"%@",exception.reason); phobosLog(exception);
} }
return dict; return dict;
} }
...@@ -188,12 +233,12 @@ static NSString *sdkVersion = @"103"; ...@@ -188,12 +233,12 @@ static NSString *sdkVersion = @"103";
* *
* @param data 数据 * @param data 数据
* *
* @since <#1.8.0#> * @since 1.8.0
*/ */
- (void)save:(NSDictionary *)data - (void)save:(NSDictionary *)data
{ {
if (_logEnabled) { if (_logEnabled) {
NSLog(@"phobos save dictionary: %@",data); phobosLog([NSString stringWithFormat:@"save dictionary: %@",data]);
} }
WMCacheService *cache = [WMCacheService sharedInstance]; WMCacheService *cache = [WMCacheService sharedInstance];
NSMutableArray *dataArray = [cache fetchObjectAtDiskWithkey:PhobosCacheKey]; NSMutableArray *dataArray = [cache fetchObjectAtDiskWithkey:PhobosCacheKey];
...@@ -208,11 +253,13 @@ static NSString *sdkVersion = @"103"; ...@@ -208,11 +253,13 @@ static NSString *sdkVersion = @"103";
/** /**
* @brief 从缓存中获取数据,并发送 * @brief 从缓存中获取数据,并发送
* *
* @since <#1.8.0#> * @since 1.8.0
*/ */
- (void)fetchDataAndSend{ - (void)fetchDataAndSend{
NSArray *paramsArray = [[WMCacheService sharedInstance] fetchObjectAtDiskWithkey:PhobosCacheKey]; NSArray *paramsArray = [[WMCacheService sharedInstance] fetchObjectAtDiskWithkey:PhobosCacheKey];
if (paramsArray.count>0) {
[self sendArray:paramsArray]; [self sendArray:paramsArray];
}
} }
...@@ -221,12 +268,11 @@ static NSString *sdkVersion = @"103"; ...@@ -221,12 +268,11 @@ static NSString *sdkVersion = @"103";
* *
* @param array * @param array
* *
* @since <#1.8.0#> * @since 1.8.0
*/ */
- (void)sendArray:(NSArray *)array { - (void)sendArray:(NSArray *)array {
if (array.count>0) {
if (_logEnabled) { if (_logEnabled) {
NSLog(@"phobos send array:%@",array); phobosLog([NSString stringWithFormat:@"array prepare to fly --✈: %@",array]);
} }
@try { @try {
NSData *JSON = [self encodeJSON:array]; NSData *JSON = [self encodeJSON:array];
...@@ -234,8 +280,7 @@ static NSString *sdkVersion = @"103"; ...@@ -234,8 +280,7 @@ static NSString *sdkVersion = @"103";
[self sendData:compressedData]; [self sendData:compressedData];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"%@",exception); phobosLog(exception);
}
} }
} }
...@@ -244,7 +289,7 @@ static NSString *sdkVersion = @"103"; ...@@ -244,7 +289,7 @@ static NSString *sdkVersion = @"103";
* *
* @param data * @param data
* *
* @since <#1.8.0#> * @since 1.8.0
*/ */
- (void)sendData:(NSData *)data { - (void)sendData:(NSData *)data {
#ifdef DEBUG #ifdef DEBUG
...@@ -261,12 +306,13 @@ static NSString *sdkVersion = @"103"; ...@@ -261,12 +306,13 @@ static NSString *sdkVersion = @"103";
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response; NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
NSInteger code = res.statusCode; NSInteger code = res.statusCode;
if (code == 200) { if (code == 200) {
phobosLog(@"✈ ---------- ✈ data arrived Mars");
[[WMCacheService sharedInstance] removeObjectAtDiskWithkey:PhobosCacheKey]; [[WMCacheService sharedInstance] removeObjectAtDiskWithkey:PhobosCacheKey];
} }
}]; }];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"%@",exception); phobosLog(exception);
} }
} }
...@@ -278,13 +324,13 @@ static NSString *sdkVersion = @"103"; ...@@ -278,13 +324,13 @@ static NSString *sdkVersion = @"103";
* *
* @return 压缩后的数据 * @return 压缩后的数据
* *
* @since <#1.8.0#> * @since 1.8.0
*/ */
- (NSData *)compressData:(NSData *)originData{ - (NSData *)compressData:(NSData *)originData{
if (!originData || [originData length] == 0) if (!originData || [originData length] == 0)
{ {
if (_logEnabled) { if (_logEnabled) {
NSLog(@"%s: Error: Can't compress an empty or null NSData object.", __func__); phobosLog(@"Error: Can't compress an empty or null NSData object.");
} }
return nil; return nil;
} }
...@@ -369,12 +415,28 @@ static NSString *sdkVersion = @"103"; ...@@ -369,12 +415,28 @@ static NSString *sdkVersion = @"103";
} }
#pragma mark - helpers #pragma mark - helpers
/**
* @brief 将对象转成JSON格式数据
*
* @param obj
*
* @return
*
* @since
*/
- (NSData *)encodeJSON:(id)obj { - (NSData *)encodeJSON:(id)obj {
NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:0 error:nil]; NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:0 error:nil];
return data; return data;
} }
/**
* @brief 获取当前时间的毫秒数
*
* @return
*
* @since 1.8.0
*/
- (NSString *)currentTime { - (NSString *)currentTime {
NSDate *date = [NSDate date]; NSDate *date = [NSDate date];
NSTimeInterval interval = [date timeIntervalSince1970]; NSTimeInterval interval = [date timeIntervalSince1970];
...@@ -382,6 +444,13 @@ static NSString *sdkVersion = @"103"; ...@@ -382,6 +444,13 @@ static NSString *sdkVersion = @"103";
return timeIntervalStr; return timeIntervalStr;
} }
/**
* @brief 获取当前APP的版本号
*
* @return
*
* @since 1.8.0
*/
- (NSString *)getAppVersion{ - (NSString *)getAppVersion{
NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
if (buildVersion) { if (buildVersion) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment