//
// LGGuangDianTong.m
// Test3
//
// Created by lagou on 15/10/26.
// Copyright © 2015年 lagou. All rights reserved.
//
#import "LGGuangDianTong.h"
#import <CommonCrypto/CommonCrypto.h>
#import <AdSupport/AdSupport.h>
#import "GYDConnect.h"
#import "GYDSystemInfo.h"
#import "LGDataBaseOfSetting.h"
#import "LGBaseHead.h"
#define gdt_encrypt_key @"xxxxx"
#define gdt_sign_key @"xxxx";
#define gdt_app_id @"xxxx"
#define gdt_advertiser_id @"xxxx"
@interface LGGuangDianTong()
@end
@implementation LGGuangDianTong
//激活发送
+ (void)send {
LGGuangDianTong *gdt = [[LGGuangDianTong alloc] init];
NSData *data = [LGDataBaseOfSetting dataForKey:KEY_DB_ADVERTISER_ACTIVE];
NSMutableArray *adArray = [GYDConnect mutableArrWithJSonData:data] ?: [NSMutableArray arrayWithObjects:@"0", @"0", nil];
//只有第一次才发送
if ([[adArray objectAtIndex:0] isEqualToString:@"1"]) {
return;
}
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[gdt generateUrl]]];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSError *error = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if ([[[dic objectForKey:@"ret"] stringValue] isEqualToString:@"0"]) {
// NSLog(@"发送成功");
//标记为已发送
[adArray replaceObjectAtIndex:0 withObject:@"1"];
NSData *newData = [GYDConnect dataWithJSonObj:adArray];
[LGDataBaseOfSetting setData:newData forKey:KEY_DB_ADVERTISER_ACTIVE];
} else {
// NSLog(@"发送失败");
}
}];
}
//生成url
- (NSString *)generateUrl {
NSString *result = @"";
NSString *encrypt_key = gdt_encrypt_key;
NSString *sign_key = gdt_sign_key;
NSString *app_id = gdt_app_id;
NSString *idfa = [LGBaseHead idfaString];
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, [GYDConnect urlValueStrOfStr:encode_page]];
NSString *signature = [[GYDConnect MD5OfString:temp] lowercaseString];
NSString* base_data = [query_string stringByAppendingFormat:@"&sign=%@", signature];
NSString* v_data = [GYDConnect urlValueStrOfStr:[GYDConnect base64Encode:[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 = [[GYDConnect MD5OfString:idfa] lowercaseString];
NSLog(@"muid--%@", result);
return result;
}
//异或操作
- (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);
}
//这一定要用strlen(infoBytes),不要用strlen(bytes),因为bytes里面有\0,而strlen只计算\0之前数,所以最终的长度偏小
NSData *data = [NSData dataWithBytes:bytes length:strlen(infoBytes)];
return data;
}
@end
广点通 (iOS完整版)
最新推荐文章于 2019-04-18 16:44:18 发布