Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
GMPhobos
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gengmeiios
GMPhobos
Commits
d71e537d
Commit
d71e537d
authored
Jan 27, 2016
by
Thierry
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善SDK,将userId作为参数暴露出来
parent
a9d1d618
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
125 additions
and
41 deletions
+125
-41
contents.xcworkspacedata
...ds.xcodeproj/project.xcworkspace/contents.xcworkspacedata
+7
-0
Phobos.h
Pod/Classes/Phobos.h
+11
-3
Phobos.m
Pod/Classes/Phobos.m
+107
-38
No files found.
Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata
0 → 100644
View file @
d71e537d
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version =
"1.0"
>
<FileRef
location =
"self:"
>
</FileRef>
</Workspace>
Pod/Classes/Phobos.h
View file @
d71e537d
...
@@ -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
;
...
...
Pod/Classes/Phobos.m
View file @
d71e537d
...
@@ -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
=
@"1
03
"
;
static
NSString
*
sdkVersion
=
@"1
10
"
;
@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
.
reas
on
);
phobosLog
(
excepti
on
);
}
}
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
];
[
self
sendArray
:
paramsArray
];
if
(
paramsArray
.
count
>
0
)
{
[
self
sendArray
:
paramsArray
];
}
}
}
...
@@ -221,21 +268,19 @@ static NSString *sdkVersion = @"103";
...
@@ -221,21 +268,19 @@ 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
)
{
phobosLog
([
NSString
stringWithFormat
:
@"array prepare to fly --✈: %@"
,
array
]);
NSLog
(
@"phobos send array:%@"
,
array
);
}
}
@try
{
@try
{
NSData
*
JSON
=
[
self
encodeJSON
:
array
];
NSData
*
JSON
=
[
self
encodeJSON
:
array
];
NSData
*
compressedData
=
[
self
compressData
:
JSON
];
NSData
*
compressedData
=
[
self
compressData
:
JSON
];
[
self
sendData
:
compressedData
];
[
self
sendData
:
compressedData
];
}
}
@catch
(
NSException
*
exception
)
{
@catch
(
NSException
*
exception
)
{
phobosLog
(
exception
);
NSLog
(
@"%@"
,
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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment