获取IOS设备的系统版本,硬件名称,mac地址
2013-07-01 15:37
375 查看
获取IOS设备的系统版本,硬件名称,mac地址
今天介绍的是如何获取ios设备的系统版本,硬件设备名称,和mac地址,我们做的游戏打算用mac地址来作为快速登录的账号,所以需要获取设备的mac地址,苹果设备的uuid现在苹果不让用了,所以用mac地址是一个比较好的选择,这里我们获取系统版本和设备名称则是为了统计使用,这里就不多做介绍了。我现在做的项目是cocos2d-x的项目,所以这里就涉及到在C++代码里边调用OC代码,那么就会有一个新语言的名字,叫做Objective-C++,这其实就是OC和C++的混合写法,这种代码,我们需要把.cpp或者.m改成.mm文件,这种文件里,就可以实现C++与OC的混编了。好了,我这就把代码贴出来,大家一看就知道是怎么回事了,实现起来很简单的。
// // GetMacAddress.h // MacTest // // Created by 江南岸 on 13-7-1. // // #ifndef __GetMacAddress_H__ #define __GetMacAddress_H__ enum DeviceType { iphone1G = 0, iphone3G, iphone3GS, iphone4, iphone4Version, iphone4CDMA, iphone4S, iphone4SVersion, iphone4SCDMA, iphone5, iphone5Version, iphone5CDMA, ipodTouch1, ipodTouch2, ipodTouch3, ipodTouch4, ipodTouch5, ipad1, ipad2WIFI, ipad2GSM, ipad2CDMA, ipad3WIFI, ipad3GSM, ipad3CDMA, ipad4WIFI, ipad4GSM, ipad4CDMA, iosNull }; class GetMacAddress { public: static const char* getMacAddress(); static const char* getSystem(); static DeviceType getDeviceType(); }; #endif
// // GetMacAddress.m // MacTest // // Created by 江南岸 on 13-7-1. // // #include "GetMacAddress.h" #include <sys/socket.h> #include <sys/sysctl.h> #include <net/if.h> #include <net/if_dl.h> #include <iostream> #import <Foundation/Foundation.h> #import"sys/utsname.h" const char *GetMacAddress::getMacAddress() { int mib[6]; size_t len; char *buf; unsigned char *ptr; struct if_msghdr *ifm; struct sockaddr_dl *sdl; mib[0] = CTL_NET; mib[1] = AF_ROUTE; mib[2] = 0; mib[3] = AF_LINK; mib[4] = NET_RT_IFLIST; if ((mib[5] = if_nametoindex("en0")) == 0) { printf("Error: if_nametoindex error/n"); return NULL; } if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { printf("Error: sysctl, take 1/n"); return NULL; } if ((buf = (char*)malloc(len*sizeof(char))) == NULL) { printf("Could not allocate memory. error!/n"); return NULL; } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { printf("Error: sysctl, take 2"); return NULL; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned char *)LLADDR(sdl); NSString *outstring = [NSString stringWithFormat:@"%hhu:%hhu:%hhu:%hhu:%hhu:%hhu", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)]; std::string str([outstring cStringUsingEncoding:NSUTF8StringEncoding]); return str.c_str(); } const char* GetMacAddress::getSystem() { NSString *systemName = [[UIDevice currentDevice]systemName]; NSString *systemVersion = [[UIDevice currentDevice]systemVersion]; NSLog(@"系统名字 %@",systemName); NSLog(@"系统版本 %@",systemVersion); std::string str([systemVersion cStringUsingEncoding:NSUTF8StringEncoding]); return str.c_str(); } DeviceType GetMacAddress::getDeviceType() { struct utsname systemInfo; uname(&systemInfo); NSLog(@"硬件设备 %@", [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]); char deviceType[1024] = {0}; strcpy(deviceType, systemInfo.machine); if(0==strcmp(deviceType, "iPhone1,1")) { return iphone1G; }else if(0==strcmp(deviceType, "iPhone1,2")) { return iphone3G; }else if(0==strcmp(deviceType, "iPhone2,1")) { return iphone3GS; }else if(0==strcmp(deviceType, "iPhone3,1")) { return iphone4; }else if(0==strcmp(deviceType, "iPhone3,2")) {//CDMA return iphone4CDMA; }else if(0==strcmp(deviceType, "iPhone3,3")) {//Version return iphone4Version;; }else if(0==strcmp(deviceType, "iPhone4,1")) { return iphone4S; }else if(0==strcmp(deviceType, "iPhone4,2")) { return iphone4SVersion; }else if(0==strcmp(deviceType, "iPhone4,3")) { return iphone4SCDMA; }else if(0==strcmp(deviceType, "iPhone5,1")) { return iphone5; }else if(0==strcmp(deviceType, "iPhone5,2")) { return iphone5Version; }else if(0==strcmp(deviceType, "iPhone5,3")) { return iphone5CDMA; }else if(0==strcmp(deviceType, "iPod1,1")) { return ipodTouch1; }else if(0==strcmp(deviceType, "iPod2,1")) { return ipodTouch2; }else if(0==strcmp(deviceType, "iPod3,1")) { return ipodTouch3; }else if(0==strcmp(deviceType, "iPod4,1")) { return ipodTouch4; }else if(0==strcmp(deviceType, "iPod5,1")) { return ipodTouch5; }else if(0==strcmp(deviceType, "iPad1,1")) { return ipad1; }else if(0==strcmp(deviceType, "iPad2,1")) {//wifi return ipad2WIFI; }else if(0==strcmp(deviceType, "iPad2,2")) {//gsm return ipad2GSM; }else if(0==strcmp(deviceType, "iPad2,3")) {//cdma return ipad2CDMA; }else if(0==strcmp(deviceType, "iPad3,1")) { return ipad3WIFI; }else if(0==strcmp(deviceType, "iPad3,2")) { return ipad3GSM; }else if(0==strcmp(deviceType, "iPad3,3")) { return ipad3CDMA; }else if(0==strcmp(deviceType, "iPad4,1")) { return ipad4WIFI; }else if(0==strcmp(deviceType, "iPad4,2")) { return ipad4GSM; }else if(0==strcmp(deviceType, "iPad4,3")) { return ipad4CDMA; } return iosNull; }
好了,代码就是这样了,这几个函数都是静态函数,调用起来很简单
CCLog("%s",GetMacAddress::getMacAddress()); GetMacAddress::getSystem(); GetMacAddress::getDeviceType();
这个功能就是这么实现了,日后做安卓版本的时候,会再把安卓的相应代码贴出来,如有问题,请给我留言
相关文章推荐
- 获取硬件信息的delphi源码(CPUID、操作系统、Mac物理地址、计算机名称、IP地址、用户名)
- iOS 获取当前设备具体型号(当前支持到iPhone7/iPhone7P )、系统版本、电量、序列号、设备名称、应用相关信息获取等
- 奔五的人学iOS:swift获取连接的wifi名称及ap的mac地址
- 获取iOS设备当前连接的wifi名称
- unity基础开发----Unity获取PC,Ios系统的mac地址等信息
- 获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)
- 获取ios的MAC地址
- android 获取设备IP和Mac地址
- ios 获取mac 地址
- ios版本 设备版本名称 获取方法
- iOS,获取手机号,获取手机的型号,系统版本,软件名称,软件版本
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- android设备无线、有线网络下获取MAC地址
- iOS - 3种方法获取设备硬件类型:
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- iOS 获取手机的型号,系统版本,软件名称,软件版本