Created
May 17, 2017 11:06
-
-
Save buoge/b5ebfdb7880dc1484d648bf4b3f8d2cd to your computer and use it in GitHub Desktop.
广点通接口调用 swift 版本,调试一些时间终于调用成功!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GDTApi.swift | |
// zaozuo-ios | |
// | |
// Created by wuchuanbo on 16/4/20. | |
// Copyright © 2016年 zaozuo. All rights reserved. | |
// | |
import Foundation | |
class GDTApi { | |
fileprivate static let encrypt_key = "" | |
fileprivate static let sign_key = "" | |
fileprivate static let gdt_uid = | |
fileprivate static let app_type = "IOS" | |
fileprivate static let conv_type = "MOBILEAPP_ACTIVITE" | |
fileprivate static let appid = | |
static func getGDTApiUrlWithWay2() -> String? { | |
if let data = encryptRequestParam() { | |
let url = "http://t.gdt.qq.com/conv/app/\(appid)/conv?v=\(data)&conv_type=\(conv_type)&app_type=\(app_type)&advertiser_id=\(gdt_uid)" | |
return url | |
} | |
return nil | |
} | |
// 广点通参数加密 | |
fileprivate static func encryptRequestParam() -> String? { | |
// 1.组装参数 | |
//click_id , 方案二不需要 | |
// muid | |
let muid = genMuid() | |
//conv_time | |
let conv_time = generateConvTime() | |
//conv_time 获取秒级的时间戳 | |
let queryString = "muid=\(muid)&conv_time=\(conv_time)" | |
// 2.参数签名 | |
let url = "http://t.gdt.qq.com/conv/app/\(appid)/conv?\(queryString)" | |
// 3.url编码,参数加密 | |
if let encodePage = urlEncode(url) { | |
let property = "\(sign_key)&GET&\(encodePage)" | |
let signature = MD5(property) | |
let baseData = "\(queryString)&sign=\(signature)" | |
if let xorData = simpleXor(baseData, encryptKey: encrypt_key) { | |
return Base64(xorData) | |
} | |
} | |
return nil | |
} | |
// urlencode | |
fileprivate static func urlEncode(_ data: String?) -> String? { | |
if let newData = data { | |
let customAllowedSet = CharacterSet( | |
charactersIn: "&:=\"#%/<>?@\\^`{|}" | |
).inverted | |
return newData.addingPercentEncoding( | |
withAllowedCharacters: customAllowedSet | |
) | |
} | |
return nil | |
} | |
// CONV_TIME | |
fileprivate static func generateConvTime() -> String { | |
return String(Int(Date().timeIntervalSince1970)) | |
} | |
//简单异或 | |
fileprivate static func simpleXor(_ source: String, encryptKey: String) -> Data? { | |
var res = [CChar]() | |
if let infoArray = source.cString(using: String.Encoding.utf8), | |
let keyArray = encryptKey.cString(using: String.Encoding.utf8) { | |
var j: Int = 0 | |
for infoEle in infoArray { | |
res.append(infoEle ^ keyArray[j]) | |
j = j + 1 | |
j = (j) % (keyArray.count - 1) | |
} | |
// return Data(bytes: UnsafePointer<UInt8>(res), count: res.count - 1) | |
return Data(bytes: UnsafeRawPointer(res), count: res.count - 1) | |
} | |
return nil | |
} | |
//base64 | |
fileprivate static func Base64(_ data: Data) -> String? { | |
return data.base64EncodedString( | |
options: NSData.Base64EncodingOptions(rawValue: 0) | |
) | |
} | |
//md5 | |
fileprivate static func MD5(_ data: String, | |
length: Int32 = CC_MD5_DIGEST_LENGTH) -> String { | |
let str = data.cString(using: String.Encoding.utf8) | |
let strLen = CC_LONG( | |
data.lengthOfBytes(using: String.Encoding.utf8) | |
) | |
let digestLen = Int(length) | |
// let result = UnsafeMutablePointer<CUnsignedChar>(allocatingCapacity: digestLen) | |
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen) | |
CC_MD5(str!, strLen, result) | |
let hash = NSMutableString() | |
for i in 0..<digestLen { | |
hash.appendFormat("%02x", result[i]) | |
} | |
result.deallocate(capacity: digestLen) | |
return String(hash) | |
} | |
fileprivate static func genMuid() -> String { | |
let muid = MD5(DeviceUtils.getIDFA().uppercased()) | |
return muid | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment