IDFV超频超限问题:
原因:检测机构统计IDFV
获取频率的方式,通过检测identifierForVendor
方法被调用次数来判定频率,每调用一次,获取频次+1。
解决思路:
1、添加UIDevice
分类。
2、交换identifierForVendor
方法。
3、在交换的方法中限制调用identifierForVendor
的次数;
代码如下:
@implementation UIDevice (GetIDFV)
+ (void)load {
GetIDFV_SwizzleMethod([self class], @selector(identifierForVendor), @selector(swizzled_identifierForVendor));
}
- (NSUUID *)swizzled_identifierForVendor {
NSString *idfvInfoStr = [Keychain secretForKey:@"SYSTEMIDFV"];
if ([NSObject isNotEmptyString:idfvInfoStr]) {
NSDictionary *idfvInfoDic = [idfvInfoStr JSONValue];
if (idfvInfoDic) {
NSString *idfvStr = [idfvInfoDic stringForKey:@"deviceIdfv"] ?: @"";
NSInteger getIdfvNum = [[idfvInfoDic stringForKey:@"getDeviceIdfvNum"] integerValue];
/// 这里限制一下获取的次数如果超过20次,则重新走系统方法获取,防止idfv发生改变。
if (getIdfvNum < 50 && idfvStr && idfvStr.length > 0) {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:idfvStr];
getIdfvNum += 1;
NSDictionary *newIdfvInfoDic = @{@"deviceIdfv": idfvStr, @"getDeviceIdfvNum": [NSString stringWithFormat:@"%ld", getIdfvNum]};
[Keychain saveSecret:[newIdfvInfoDic JSONRepresentation] forKey:@"SYSTEMIDFV"];
return uuid;
}
}
}
NSUUID *systemUUID = [self swizzled_identifierForVendor];
NSDictionary *newIdfaInfoDic = @{@"deviceIdfv": [systemUUID UUIDString], @"getDeviceIdfvNum": @"1"};
[Keychain saveSecret:[newIdfaInfoDic JSONRepresentation] forKey:@"SYSTEMIDFV"];
return systemUUID;
}
void GetIDFV_SwizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@end
Keychain
为框架,可以自行替换。
每50次去调用一次系统获取方法,主要是为了防止IDFV
发生改变,不能及时更新到服务器。这里可以自行更改次数,只要不超过1秒/次这个频率,检测机构都不会报超频超限。