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