Main/Assets/Plugins/iOS/GetDeviceAddress.mm
2025-01-25 04:38:09 +08:00

217 lines
5.7 KiB
Plaintext

//
// GetDeviceAddress.cpp
// wzxy_gz
//
// Created by Mac on 12-9-22.
//
//
//获取手机MAC地址(硬件地址登录)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#import <Foundation/Foundation.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <iostream>
#import <AdSupport/ASIdentifierManager.h>
#import <UIKit/UIKit.h>
#define IFT_ETHER 0x6
char * getIosDeviceAddress(char* macAddress, const char* ifName) {
int success;
struct ifaddrs * addrs;
struct ifaddrs * cursor;
const struct sockaddr_dl * dlAddr;
const unsigned char* base;
int i;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != 0) {
if ( (cursor->ifa_addr->sa_family == AF_LINK)
&& (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER) && strcmp(ifName, cursor->ifa_name)==0 ) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen];
strcpy(macAddress, "");
for (i = 0; i < dlAddr->sdl_alen; i++) {
if (i != 0) {
strcat(macAddress, ":");
}
char partialAddr[3];
sprintf(partialAddr, "%02X", base[i]);
strcat(macAddress, partialAddr);
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
// if ( [[UIDevice currentDevice].systemVersion floatValue] >= 7.0){
// if([ASIdentifierManager sharedManager].advertisingTrackingEnabled){
// sprintf(macAddress, "%s",[[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString] UTF8String]);
// }
// }
return macAddress;
}
//系统版本为7.0以后则会返回广告标识符
char * getDeviceAddressOrIdfa(char* macAddress, const char* ifName)
{
int success;
struct ifaddrs * addrs;
struct ifaddrs * cursor;
const struct sockaddr_dl * dlAddr;
const unsigned char* base;
int i;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != 0) {
if ( (cursor->ifa_addr->sa_family == AF_LINK)
&& (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER) && strcmp(ifName, cursor->ifa_name)==0 ) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen];
strcpy(macAddress, "");
for (i = 0; i < dlAddr->sdl_alen; i++) {
if (i != 0) {
strcat(macAddress, ":");
}
char partialAddr[3];
sprintf(partialAddr, "%02X", base[i]);
strcat(macAddress, partialAddr);
}
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
if ( [[UIDevice currentDevice].systemVersion floatValue] >= 7.0){
if([ASIdentifierManager sharedManager].advertisingTrackingEnabled){
sprintf(macAddress, "%s",[[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString] UTF8String]);
}
}
return macAddress;
}
void getDeviceAllIdnefier(std::string &mac, std::string &udid, std::string &idfa)
{
char charmac[64] = {0};
mac = getIosDeviceAddress(charmac, "en0");
if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0) {
udid = "0";
idfa = "0";
}else if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){
udid = [[[UIDevice currentDevice].identifierForVendor UUIDString] UTF8String];
idfa = "0";
}else if([[UIDevice currentDevice].systemVersion floatValue] >= 7.0){
udid = [[[UIDevice currentDevice].identifierForVendor UUIDString] UTF8String];
if([ASIdentifierManager sharedManager].advertisingTrackingEnabled){
idfa= [[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString] UTF8String];
}else
idfa = "0";
}
}
void get_app_version( std::string& out_ver )
{
NSString* str = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
out_ver = [str UTF8String];
}
//static char s_buf[512] = {0};
extern "C"
{
extern void SetSocketHandle(int handle);
// temp
// handle: client socket handle.
// msg_id: heartbeat msg id
// haed_size: head size
void set_net_handle(int handle, int msg_id, int head_size)
{
// SetSocketHandle(handle);
}
const char* GetUID()
{
char* buf = new char[512];
std::string ret;
std::string mac, udid, idfa;
getDeviceAllIdnefier(mac, udid, idfa);
if(idfa == "0"){
if(udid == "0"){
ret = mac;
}else{
ret = udid;
}
}else{
ret = idfa;
}
memccpy(buf, ret.data(), sizeof(char), ret.length());
buf[ret.length()] = '\0';
return buf;
}
const char* GetImie()
{
char* buf = new char[512];
std::string mac, udid, idfa;
getDeviceAllIdnefier(mac, udid, idfa);
memccpy(buf, udid.data(), sizeof(char), udid.length());
buf[udid.length()] = '\0';
return buf;
}
const char* GetIdfa()
{
char* buf = new char[512];
std::string mac, udid, idfa;
getDeviceAllIdnefier(mac, udid, idfa);
memccpy(buf, idfa.data(), sizeof(char), idfa.length());
buf[idfa.length()] = '\0';
return buf;
}
const char* GetMac()
{
char* buf = new char[512];
std::string mac, udid, idfa;
getDeviceAllIdnefier(mac, udid, idfa);
memccpy(buf, mac.data(), sizeof(char), mac.length());
buf[mac.length()] = '\0';
return buf;
}
const char* get_app_version()
{
char* buf = new char[512];
std::string ver;
get_app_version( ver );
memccpy(buf, ver.data(), sizeof(char), ver.length());
buf[ver.length()] = '\0';
return buf;
}
}
#endif