Commit 3389ab1d authored by jz's avatar jz

获取idfa 钥匙串

parent 0418bbd6
...@@ -441,6 +441,7 @@ ...@@ -441,6 +441,7 @@
"${BUILT_PRODUCTS_DIR}/GMCache/GMCache.framework", "${BUILT_PRODUCTS_DIR}/GMCache/GMCache.framework",
"${BUILT_PRODUCTS_DIR}/GMPhobos/GMPhobos.framework", "${BUILT_PRODUCTS_DIR}/GMPhobos/GMPhobos.framework",
"${BUILT_PRODUCTS_DIR}/MJExtension/MJExtension.framework", "${BUILT_PRODUCTS_DIR}/MJExtension/MJExtension.framework",
"${BUILT_PRODUCTS_DIR}/SAMKeychain/SAMKeychain.framework",
"${BUILT_PRODUCTS_DIR}/TMCache/TMCache.framework", "${BUILT_PRODUCTS_DIR}/TMCache/TMCache.framework",
); );
name = "[CP] Embed Pods Frameworks"; name = "[CP] Embed Pods Frameworks";
...@@ -449,6 +450,7 @@ ...@@ -449,6 +450,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMCache.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMCache.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMPhobos.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GMPhobos.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MJExtension.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MJExtension.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SAMKeychain.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TMCache.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TMCache.framework",
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>NSUserTrackingUsageDescription</key>
<string>申请使用idfa</string>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>en</string> <string>en</string>
<key>CFBundleDisplayName</key> <key>CFBundleDisplayName</key>
......
...@@ -52,6 +52,10 @@ NSString *const MockCityId = @"beijing"; ...@@ -52,6 +52,10 @@ NSString *const MockCityId = @"beijing";
_tableView.dataSource = self; _tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.view addSubview:_tableView]; [self.view addSubview:_tableView];
[PhobosUtil getIDFAString:^(NSString *idfa) {
}];
} }
......
...@@ -4,11 +4,13 @@ PODS: ...@@ -4,11 +4,13 @@ PODS:
- FMDB/standard (2.7.5) - FMDB/standard (2.7.5)
- GMCache (1.0.1): - GMCache (1.0.1):
- TMCache (= 2.1.0) - TMCache (= 2.1.0)
- GMPhobos (3.0.0): - GMPhobos (3.0.1):
- FMDB - FMDB
- GMCache - GMCache
- MJExtension - MJExtension
- SAMKeychain
- MJExtension (3.2.1) - MJExtension (3.2.1)
- SAMKeychain (1.5.3)
- TMCache (2.1.0) - TMCache (2.1.0)
DEPENDENCIES: DEPENDENCIES:
...@@ -20,6 +22,7 @@ SPEC REPOS: ...@@ -20,6 +22,7 @@ SPEC REPOS:
https://github.com/CocoaPods/Specs.git: https://github.com/CocoaPods/Specs.git:
- FMDB - FMDB
- MJExtension - MJExtension
- SAMKeychain
- TMCache - TMCache
EXTERNAL SOURCES: EXTERNAL SOURCES:
...@@ -29,8 +32,9 @@ EXTERNAL SOURCES: ...@@ -29,8 +32,9 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS: SPEC CHECKSUMS:
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
GMCache: b78d8e46db864405e91d226ce640cc80d966c611 GMCache: b78d8e46db864405e91d226ce640cc80d966c611
GMPhobos: 77ef31a7d5d8e183f40b022d613d5fefc4f9b981 GMPhobos: 5218b3f57cc68aab78e6fc309c58a2290d625698
MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545 MJExtension: 635f2c663dcb1bf76fa4b715b2570a5710aec545
SAMKeychain: 483e1c9f32984d50ca961e26818a534283b4cd5c
TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed TMCache: 95ebcc9b3c7e90fb5fd8fc3036cba3aa781c9bed
PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad PODFILE CHECKSUM: ea0fac2144ac80baf8f21576cde49526c19991ad
......
...@@ -28,6 +28,7 @@ Pod::Spec.new do |s| ...@@ -28,6 +28,7 @@ Pod::Spec.new do |s|
s.dependency 'MJExtension' s.dependency 'MJExtension'
s.dependency 'FMDB' s.dependency 'FMDB'
s.library = 'z' s.library = 'z'
s.dependency 'SAMKeychain'
# s.resource = 'GMPhobos/*.xcdatamodeld' # s.resource = 'GMPhobos/*.xcdatamodeld'
end end
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
typedef void (^SendDataSuccessBlock)(NSInteger code); typedef void (^SendDataSuccessBlock)(NSInteger code);
typedef void (^PhobosGetIDFACompleteBlock)(NSString *idfa);
@interface PhobosUtil : NSObject @interface PhobosUtil : NSObject
/** /**
...@@ -108,4 +110,25 @@ typedef void (^SendDataSuccessBlock)(NSInteger code); ...@@ -108,4 +110,25 @@ typedef void (^SendDataSuccessBlock)(NSInteger code);
*/ */
+ (NSString *)convertToJsonString:(id)object; + (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 @end
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
#import "Phobos.h" #import "Phobos.h"
#import <CommonCrypto/CommonCryptor.h> #import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonDigest.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/ASIdentifierManager.h>
#import <SAMKeychain/SAMKeychain.h>
#define IOS_CELLULAR @"pdp_ip0" #define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0" #define IOS_WIFI @"en0"
...@@ -494,4 +497,63 @@ ...@@ -494,4 +497,63 @@
} }
return jsonString; 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 @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