公司最近需要对APP进行推广,选择广点通增加下载量.需要在APP中增加相应的代码.
广点通官网
源码使用:OC+AFNetworking 3.0
一.关键参数:
1.Apple ID: 在iTunesConnect中获取
2.UID:广点通后台账号ID
3.EncryptKey 和 SignKey:在广点通后台获取
配置已结束,以下是相关代码
YGGuangDianTong.h
#import
@interface YGGuangDianTong : NSObject
+ (instancetype)shareGuangDianTong;
- (void)send;
@end
YGGuangDianTong.m
#import "YGGuangDianTong.h"
#import
#import
// github 上下载,三个文件且是MRC,需要配置
#import "GTMBase64.h"
#define gdt_encrypt_key @"XXXXXX"
#define gdt_sign_key @"XXXXXXX"
#define gdt_app_id @"XXXXXX" // APPID
#define gdt_advertiser_id @"XXXXX" // 广点通的账户 ID
#define CC_MD5_DIGEST_LENGTH 16
// 是否已经上报广点通
#define kIsReportGuangDianTongKey @"kIsReportGuangDianTongKey"
static id _guangDianTong = nil;
@implementation YGGuangDianTong
+ (instancetype)shareGuangDianTong {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_guangDianTong = [[self alloc] init];
});
return _guangDianTong;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_guangDianTong = [super allocWithZone:zone];
});
return _guangDianTong;
}
- (id)copyWithZone:(NSZone *)zone
{
return _guangDianTong;
}
#pragma IDFA
- (NSString *)GetIDFA{
return [ASIdentifierManager sharedManager].advertisingIdentifier.UUIDString;
}
//激活发送
- (void)send {
//只有第一次才发送
if([[[NSUserDefaults standardUserDefaults] objectForKey:kIsReportGuangDianTongKey] boolValue]){
DLog(@"已经上报广点通")
return;
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer=[AFJSONRequestSerializer serializer];
// @"application/x-javascript" 添加类型,否则报错
manager.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html",@"application/x-javascript", nil];
[manager GET:[self generateUrl] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
DLog(@"JSON: %@", responseObject);
NSMutableString *message = [NSMutableString stringWithString:@"广点通上报:"];
switch ([responseObject[@"ret"] integerValue]) {
case 0:
[message appendString:@"成功"];
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:kIsReportGuangDianTongKey];
[[NSUserDefaults standardUserDefaults] synchronize];
break;
case -1:
[message appendString:@"请求非法参数"];
break;
case -2:
[message appendString:@"参数解析失败"];
break;
case -3:
[message appendString:@"参数解码失败"];
break;
case -12:
[message appendString:@"获取密钥失败"];
break;
case -13:
[message appendString:@"非法的应用类型"];
break;
case -14:
[message appendString:@"非法的转化时间"];
break;
case -15:
[message appendString:@"非法的广点通移劢设备标识"];
break;
case -17:
[message appendString:@"获取转化规则失败"];
break;
default:
break;
}
DLog(@"%@", message);
} failure:^(NSURLSessionTask *operation, NSError *error) {
DLog(@"Error: %@", error);
}];
}
- (NSString *)generateUrl{
NSString *result = @"";
NSString *encrypt_key = gdt_encrypt_key;
NSString *sign_key = gdt_sign_key;
NSString *app_id = gdt_app_id;
NSString *idfa = [self GetIDFA];
NSString *conv_type = @"MOBILEAPP_ACTIVITE";
NSString *app_type = @"IOS";
NSString *advertiser_id = gdt_advertiser_id;
NSString *conv_time = [NSString stringWithFormat:@"%.0lf", [NSDate date].timeIntervalSince1970];
NSString *muid = [self generateMuid:idfa];
NSString *query_string = [NSString stringWithFormat:@"muid=%@&conv_time=%@", muid, conv_time];
NSString *encode_page = [NSString stringWithFormat:@"http://t.gdt.qq.com/conv/app/%@/conv?%@", app_id, query_string];
NSString *temp = [NSString stringWithFormat:@"%@&GET&%@", sign_key, [self encodeToPercentEscapeString:encode_page]];
NSString *signature = [self getMd5_32Bit_String:temp];
NSString* base_data = [query_string stringByAppendingFormat:@"&sign=%@", signature];
NSString* v_data = [self encodeToPercentEscapeString:[GTMBase64 stringByEncodingData:[self simpleXor:base_data key:encrypt_key]]];
NSLog(@"query_string: %@", query_string);
NSLog(@"v_data: %@", v_data);
result = [NSString stringWithFormat:@"http://t.gdt.qq.com/conv/app/%@/conv?v=%@&conv_type=%@&app_type=%@&advertiser_id=%@", app_id, v_data, conv_type, app_type, advertiser_id];
NSLog(@"\n%@", result);
return result;
}
//生成muid
- (NSString *)generateMuid:(NSString *)idfa {
idfa = [idfa uppercaseString];
NSString* result = [self getMd5_32Bit_String:idfa];
NSLog(@"muid--%@", result);
return result;
}
//urlEncode
- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
NSString *outputStr = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return outputStr;
}
//异或操作
- (NSData *)simpleXor:(NSString *)info key:(NSString *)key {
const char *infoBytes = [info UTF8String];
const char *keyBytes = [key UTF8String];
int i = 0, j = 0;
char *bytes = malloc(strlen(infoBytes));
for (i = 0; i < strlen(infoBytes); i++) {
bytes[i] = (char)(infoBytes[i]^keyBytes[j]);
j++;
j = j%(key.length);
}
NSData *data = [NSData dataWithBytes:bytes length:strlen(infoBytes)];
return data;
}
- (NSString *)getMd5_32Bit_String:(NSString *)srcString{
const char *cstr = [srcString UTF8String];
uint8_t digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(cstr, (int)strlen(cstr), digest);
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[result appendFormat:@"%02x",digest[i]];
}
return [result lowercaseString];
}
@end
调用:
AppDelegate.m
// 引入头文件
#import "YGGuangDianTong.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 广点通接入
[[YGGuangDianTong shareGuangDianTong] send];
return YES;
}
二.提审注意:
由于使用了IDFA,提审需要相应选择,以免被拒.
[完]如有漏洞,请留言!!!
相关教程:
iOS 集成广点通移动 App 激活数据统计 API 上报方案
腾讯-----广点通iOS端接入实现
如何防止应用因获取IDFA被AppStore拒绝