Commit 2a14bd2d authored by jz's avatar jz

modify deviceid

parent 3389ab1d
......@@ -15,6 +15,7 @@
#import <mach/mach_time.h>
#import "PhobosDataManager.h"
#import "PhobosSendManager.h"
#import "PhobosDevice.h"
static NewPhobos *_sharedClient;
......@@ -111,7 +112,9 @@ static NewPhobos *_sharedClient;
@"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"uuid": PhobosSafeString([PhobosDevice getDEVICEID]),
@"idfa": PhobosSafeString([PhobosDevice getIDFA]),
@"idfv": PhobosSafeString([PhobosDevice getIDFV]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
};
[NewPhobos track:@"device_opened" attributes:dict sendNow:YES];
......@@ -210,7 +213,9 @@ static NewPhobos *_sharedClient;
@"phone_operator": [PhobosUtil getTelephonyInfo],
@"total_memory": [PhobosUtil getTotalMemorySize],
@"run_time": [PhobosUtil deviceRunTime],
@"uuid": [PhobosUtil deviceId],
@"uuid": PhobosSafeString([PhobosDevice getDEVICEID]),
@"idfa": PhobosSafeString([PhobosDevice getIDFA]),
@"idfv": PhobosSafeString([PhobosDevice getIDFV]),
@"build_version_release": [[UIDevice currentDevice] systemVersion],
};
[NewPhobos trackSessionOverWithAttributes:dict];
......@@ -320,9 +325,9 @@ static NewPhobos *_sharedClient;
NSString *currentTime = [PhobosUtil currentTime];
NSMutableDictionary *deviceParams = [NSMutableDictionary new];
[deviceParams setValue:PhobosSafeString([[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]) forKey:@"idfa"];
[deviceParams setValue:PhobosSafeString([[[UIDevice currentDevice] identifierForVendor] UUIDString]) forKey:@"idfv"];
[deviceParams setValue:PhobosSafeString([PhobosUtil deviceId]) forKey:@"device_id"];
[deviceParams setValue:PhobosSafeString([PhobosDevice getIDFA]) forKey:@"idfa"];
[deviceParams setValue:PhobosSafeString([PhobosDevice getIDFV]) forKey:@"idfv"];
[deviceParams setValue:PhobosSafeString([PhobosDevice getDEVICEID]) forKey:@"device_id"];
[deviceParams setValue:@"ios" forKey:@"device_type"];
[deviceParams setValue:@"Apple" forKey:@"manufacturer"];
[deviceParams setValue:@(self.gps.coordinate.latitude) forKey:@"lat"];
......
......@@ -11,6 +11,7 @@
#import "UIResponder+PhobosPV.h"
#import "PhobosUtil.h"
#import "PhobosCustomVisibleController.h"
#import "PhobosDevice.h"
#import <GMCache/GMCache.h>
#import <mach/mach_time.h>
......@@ -98,14 +99,18 @@ static OldPhobos *sharedClient = nil;
- (void)handleEventDeviceOpened{
/** 每次打开APP埋点 **/
// [deviceParams setValue:PhobosSafeString([PhobosDevice getIDFA]) forKey:@"idfa"];
// [deviceParams setValue:PhobosSafeString([PhobosDevice getIDFV]) forKey:@"idfv"];
// [deviceParams setValue:PhobosSafeString([PhobosDevice getDEVICEID]) forKey:@"device_id"];
NSDictionary *dict = @{@"build_cpu_abi": PhobosSafeString([PhobosUtil currentDeviceCPUType]),
@"cpu_count": PhobosSafeString([PhobosUtil currentDeviceCPUCount]),
@"mac_address": PhobosSafeString([PhobosUtil getMacAddress]),
@"phone_operator": PhobosSafeString([PhobosUtil getTelephonyInfo]),
@"total_memory": PhobosSafeString([PhobosUtil getTotalMemorySize]),
@"run_time": PhobosSafeString([PhobosUtil deviceRunTime]),
@"uuid": PhobosSafeString([PhobosUtil deviceId]),
@"uuid": PhobosSafeString([PhobosDevice getDEVICEID]),
@"idfa": PhobosSafeString([PhobosDevice getIDFA]),
@"idfv": PhobosSafeString([PhobosDevice getIDFV]),
@"build_version_release": PhobosSafeString([[UIDevice currentDevice] systemVersion]),
};
[OldPhobos track:@"device_opened" attributes:dict sendNow:YES];
......@@ -206,7 +211,7 @@ static OldPhobos *sharedClient = nil;
@"phone_operator": [PhobosUtil getTelephonyInfo],
@"total_memory": [PhobosUtil getTotalMemorySize],
@"run_time": [PhobosUtil deviceRunTime],
@"uuid": [PhobosUtil deviceId],
@"uuid": [PhobosDevice getDEVICEID],
@"build_version_release": [[UIDevice currentDevice] systemVersion],
};
[OldPhobos track:@"on_app_session_over" attributes:dict];
......@@ -424,9 +429,9 @@ static OldPhobos *sharedClient = nil;
@try {
NSString *currentTime = [PhobosUtil currentTime];
NSMutableDictionary *deviceParams = [NSMutableDictionary new];
[deviceParams setValue:PhobosSafeString([[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]) forKey:@"idfa"];
[deviceParams setValue:PhobosSafeString([[[UIDevice currentDevice] identifierForVendor] UUIDString]) forKey:@"idfv"];
[deviceParams setValue:PhobosSafeString([PhobosUtil deviceId]) forKey:@"device_id"];
[deviceParams setValue:PhobosSafeString([PhobosDevice getIDFA]) forKey:@"idfa"];
[deviceParams setValue:PhobosSafeString([PhobosDevice getIDFV]) forKey:@"idfv"];
[deviceParams setValue:PhobosSafeString([PhobosDevice getDEVICEID]) forKey:@"device_id"];
[deviceParams setValue:@"ios" forKey:@"device_type"];
[deviceParams setValue:@"Apple" forKey:@"manufacturer"];
[deviceParams setValue:@(self.gps.coordinate.latitude) forKey:@"lat"];
......
//
// PhobosDevice.h
// GMPhobos
//
// Created by Q14 on 2020/9/22.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^PhobosGetIDFACompleteBlock)(NSDictionary *dict);
@interface PhobosDevice : NSObject
/// 获取IDFA,同步获取 从钥匙串中获取
+ (NSString *)getIDFA;
/// 获取IDFA,异步获取IDFA
+ (void)getIDFAString:(PhobosGetIDFACompleteBlock)completeBlock;
/// 获取IDFV
+ (NSString *)getIDFV;
/// 获取DEVICEID,同步获取 取出的是默认第一次的idfv
+ (NSString *)getDEVICEID;
@end
NS_ASSUME_NONNULL_END
//
// PhobosDevice.m
// GMPhobos
//
// Created by Q14 on 2020/9/22.
//
#import "PhobosDevice.h"
#import <AdSupport/ASIdentifierManager.h>
#import <SAMKeychain/SAMKeychain.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
//钥匙串账户
NSString *const keyChainAccount = @"com.wanmeizhensuo.ZhengXing.phobos.keychain";
//钥匙串账户 表示device_id
NSString *const DEVICEID = @"device_id";
//钥匙串账户 表示idfa
NSString *const IDFA = @"idfa";
// 默认获取不到的idfa
NSString *defaultIDFA = @"00000000-0000-0000-0000-000000000000";
@implementation PhobosDevice
#pragma mark - getIDFA
//同步获取IDFA 直接从钥匙串中获取
+ (NSString *)getIDFA {
//直接从钥匙串中获取
NSString *keyChainIDFA = [self getKeyChainService:IDFA account:keyChainAccount];
//判断获取的钥匙串是否为空
if ([self checkIdfaIsNull:keyChainIDFA]) {
if (@available(iOS 14, *)) {
// 如果是iOS14 未从钥匙串中获取到idfa直接按默认的
keyChainIDFA = defaultIDFA;
} else {
keyChainIDFA = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
if ([self checkIdfaIsNull:keyChainIDFA]) {
[self saveKeyChainWithValue:keyChainIDFA service:IDFA account:keyChainAccount];
}
}
}
return keyChainIDFA;
}
//同步获取IDFA
+ (void)getIDFAString:(PhobosGetIDFACompleteBlock)completeBlock {
if (@available(iOS 14, *)) {
__block NSString *idfaString = @"";
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
BOOL isAuthor = NO;
if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
isAuthor = YES;
idfaString = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
}
if (![self checkIdfaIsNull:idfaString]) {
[self saveKeyChainWithValue:idfaString service:IDFA account:keyChainAccount];
}
completeBlock(@{@"idfa": idfaString, @"is_author": @(isAuthor)});
}];
} else {
// 使用原方式访问IDFA
NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
BOOL isAuthor = NO;
if (![self checkIdfaIsNull:idfaString]) {
isAuthor = YES;
[self saveKeyChainWithValue:idfaString service:IDFA account:keyChainAccount];
}
completeBlock(@{@"idfa": idfaString, @"is_author": @(isAuthor)});
}
}
#pragma mark - 对IDFA判空处理
//检查idfa 是否为空
+ (BOOL)checkIdfaIsNull:(NSString *)idfa {
return !idfa || [idfa isEqualToString:@"00000000-0000-0000-0000-000000000000"];
}
#pragma mark - getIDFV
+ (NSString *)getIDFV {
return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
#pragma mark - getDEVICEID
+ (NSString *)getDEVICEID {
NSString *deviceId = [self getKeyChainService:DEVICEID account:keyChainAccount];
if ([self checkIdfaIsNull:deviceId]) {
deviceId = [self getIDFV];
[self saveKeyChainWithValue:deviceId service:DEVICEID account:keyChainAccount];
}
return deviceId;
}
#pragma mark - 钥匙串封装
+ (void)saveKeyChainWithValue:(NSString *)value service:(NSString *)service account:(NSString *)account {
[SAMKeychain setPassword:value forService:service account:account];
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAlwaysThisDeviceOnly];
}
//根据 service 与账户keyChain 获取存储的值
+ (NSString *)getKeyChainService:(NSString *)service account:(NSString *)account {
return [SAMKeychain passwordForService:service account:account];
}
@end
......@@ -10,7 +10,6 @@
typedef void (^SendDataSuccessBlock)(NSInteger code);
typedef void (^PhobosGetIDFACompleteBlock)(NSString *idfa);
@interface PhobosUtil : NSObject
......@@ -54,7 +53,7 @@ typedef void (^PhobosGetIDFACompleteBlock)(NSString *idfa);
+ (NSString *)getAppVersion;
+ (BOOL)isNonEmpty:(NSString *)string;
+ (NSData *)encodeJSON:(id)obj;
+ (NSString *)deviceId;
//+ (NSString *)deviceId;
/** 获取手机型号 */
+ (NSString *)platform;
/**
......@@ -109,26 +108,4 @@ typedef void (^PhobosGetIDFACompleteBlock)(NSString *idfa);
* @since 5.9.3
*/
+ (NSString *)convertToJsonString:(id)object;
/*!
* @author 乔金柱, 20-9-17
*
* @brief 获取当前设备的idfa
* 1. 获取逻辑规则会先钥匙串中取
* 2. 然后根绝API 取出
* @since 7.34.0
*/
+ (void )getIDFAString:(PhobosGetIDFACompleteBlock)completeBlock;
/*!
* @author 乔金柱, 20-9-17
*
* @brief 获取当前设备的idfa
* 1. 获取逻辑规则先钥匙串中取
* @since 7.34.0
*/
+ (NSString *)saveIDFAFromKeyChain:(NSString *)idfaString;
@end
......@@ -23,9 +23,6 @@
#import "Phobos.h"
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/ASIdentifierManager.h>
#import <SAMKeychain/SAMKeychain.h>
#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
......@@ -230,16 +227,16 @@
return [str length] > 0;
}
+ (NSString *)deviceId {
NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
if (idfa != nil && ![idfa isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
return idfa;
}
return idfv;
}
//+ (NSString *)deviceId {
// NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//
// if (idfa != nil && ![idfa isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
// return idfa;
// }
//
// return idfv;
//}
+ (NSString *)getIPAddress:(BOOL)preferIPv4 {
NSArray *searchArray = preferIPv4 ?
......@@ -497,63 +494,4 @@
}
return jsonString;
}
+ (void)getIDFAString:(PhobosGetIDFACompleteBlock)completeBlock {
if (@available(iOS 14, *)) {
__block NSString *idfaString = @"";
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
idfaString = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString;
}
//检测获取到的idfa是否为空 如果为空从钥匙串中取
if ([self checkIdfaIsNull:idfaString]) {
idfaString = [self getIDFAFromKeyChain];
}
//从钥匙串中idfa为空 则去udid
if ([self checkIdfaIsNull:idfaString]) {
idfaString = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
[self saveIDFAFromKeyChain:idfaString];
completeBlock(idfaString);
}];
} else {
// 使用原方式访问IDFA
NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
//检测获取到的idfa是否为空 如果为空从钥匙串中取
if ([self checkIdfaIsNull:idfaString]) {
idfaString = [self getIDFAFromKeyChain];
}
//从钥匙串中idfa为空 则去udid
if ([self checkIdfaIsNull:idfaString]) {
idfaString = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
[self saveIDFAFromKeyChain:idfaString];
completeBlock(idfaString);
}
}
+ (BOOL)checkIdfaIsNull:(NSString *)idfa {
return !idfa || [idfa isEqualToString:@"00000000-0000-0000-0000-000000000000"];
}
//从钥匙串获取idfa
+ (NSString *)getIDFAFromKeyChain{
NSString *idfaStr = [SAMKeychain passwordForService:@"idfa" account:@"com.gengmei.appid"];
return idfaStr;
}
//存储idfa到钥匙串中
+ (NSString *)saveIDFAFromKeyChain:(NSString *)idfaString {
//只在这个设备上获取 如果换设备不记录
[SAMKeychain setPassword:idfaString forService:@"idfa" account:@"com.gengmei.appid"];
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAlwaysThisDeviceOnly];
}
@end
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