首先说明下,下面两种方法均可以获得手机的mac地址,但是有个限制,是在iOS7以下才可以获得。iOS7以后苹果对于sysctl和ioctl进行了技术处理,MAC地址返回的都是02:00:00:00:00:00。官方文档上这样写的“Twolow-level networking APIs that used to return a MAC address now return thefixed value 02:00:00:00:00:00. The APIs in question are sysctl(NET_RT_IFLIST) and ioctl(SIOCGIFCONF). Developers using the value of the MAC address should migrate toidentifiers such as -[UIDevice identifierForVendor].This change affects all apps running on iOS 7”。

所以在iOS7以后想要获取设备的唯一标示Mac地址已经不行了,只能用其他的代替。

下面说下两种方式:

都需要导入几个头文件

#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

方法1:

// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Accidentally munged during previous update. Fixed thanks to mlamb.
- (NSString *) macaddress
{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 = malloc(len)) == 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:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];//    NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];NSLog(@"outString:%@", outstring);free(buf);return [outstring uppercaseString];
}

源自 http://blog.csdn.net/showhilllee

方法2:

- (NSString *)getMacAddress
{int                 mgmtInfoBase[6];char                *msgBuffer = NULL;size_t              length;unsigned char       macAddress[6];struct if_msghdr    *interfaceMsgStruct;struct sockaddr_dl  *socketStruct;NSString            *errorFlag = NULL;// Setup the management Information Base (mib)mgmtInfoBase[0] = CTL_NET;        // Request network subsystemmgmtInfoBase[1] = AF_ROUTE;       // Routing table infomgmtInfoBase[2] = 0;mgmtInfoBase[3] = AF_LINK;        // Request link layer informationmgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces// With all configured interfaces requested, get handle indexif ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)errorFlag = @"if_nametoindex failure";else{// Get the size of the data available (store in len)if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)errorFlag = @"sysctl mgmtInfoBase failure";else{// Alloc memory based on above callif ((msgBuffer = malloc(length)) == NULL)errorFlag = @"buffer allocation failure";else{// Get system information, store in bufferif (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)errorFlag = @"sysctl msgBuffer failure";}}}// Befor going any further...if (errorFlag != NULL){NSLog(@"Error: %@", errorFlag);return errorFlag;}// Map msgbuffer to interface message structureinterfaceMsgStruct = (struct if_msghdr *) msgBuffer;// Map to link-level socket structuresocketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);// Copy link layer address data in socket structure to an arraymemcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);// Read from char array into a string object, into traditional Mac address formatNSString *macAddressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",macAddress[0], macAddress[1], macAddress[2],macAddress[3], macAddress[4], macAddress[5]];NSLog(@"Mac Address: %@", macAddressString);// Release the buffer memoryfree(msgBuffer);return macAddressString;
}

iOS获取手机的Mac地址相关推荐

  1. 获取手机mac php,IOS_iOS如何获取手机的Mac地址,首先说明下,下面两种方法均 - phpStudy...

    iOS如何获取手机的Mac地址 首先说明下,下面两种方法均可以获得手机的mac地址,但是有个限制,是在iOS7以下才可以获得.iOS7以后苹果对于sysctl和ioctl进行了技术处理,MAC地址返回 ...

  2. Android中获取手机 IMEI Mac地址 IP地址

    一.获取手机IMEI 手机在生产时,每部手机均有一个唯一的标识(ID),国际上采用国际移动设备身份码(IMEI, International Mobile Equipment Identity).IM ...

  3. iOS 获取手机的ip地址 并传给后台(三步搞定)

    第一步 创建一个NSObject 文件 .h 文件 写 #import <Foundation/Foundation.h> @interface NSObject (GetIP) + (N ...

  4. php获取手机的mac地址,Android手机获取Mac地址的方法

    [导读]这篇文章主要为大家详细介绍了Android手机获取Mac地址的方法,具有一定的参考价值 最常用的方法,通过WiFiManager获取:/** * 通过WiFiManager获取mac地址 *  ...

  5. linux获得蓝牙外设mac,iOS获取蓝牙外设Mac地址

    #pragma mark 发现周边蓝牙服务里的特征 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsFo ...

  6. 小程序中,iOS设备获取蓝牙设备的Mac地址

    遇到的问题 在使用蓝牙的过程中,我们需要获取蓝牙设备的Mac地址.在Android设备上,onBluetoothDeviceFound方法中,deviceId是蓝牙设备的Mac地址.而在 iOS设备上 ...

  7. 第三方机构能获取我的MAC地址吗?

    有关浏览器指纹识别,我们最常被问到的问题是,我们是否可以隐藏MAC地址?对注重隐私的业务来说,这意味着什么?换一种更直接的说法--网站或第三方机构是否可以获取我的MAC地址?就这一问题,我们展开了调查 ...

  8. 获取远程网卡MAC地址

    出自: http://blog.joycode.com/liuhuimiao/ 朋友mingal急问我有关获取远程网卡MAC地址的ASP.net实现.我一开始以为是获取本机MAC地址,说了几种方法给他 ...

  9. android 获取网卡mac_Android获取本机Mac地址及IP地址方法

    1.Android  获取本机Mac 地址方法: 需要在AndroidManifest.xml文件中添加权限: public String getLocalMacAddress() { WifiMan ...

最新文章

  1. .NET Core EntityFramework生成自动增长的主键
  2. 这几家公司有个梦想:开发AI操作系统,让外行也成为人工智能大师
  3. 深入理解JS中的变量作用域
  4. [网络安全自学篇] 六十四.Windows安全缺陷利用之SMBv3服务远程代码执行(CVE-2020-0796)复现及防御机理
  5. “约见”面试官系列之常见面试题之第九十五篇之vue-router的组件组成(建议收藏)
  6. Java线程--BlockingQueue使用
  7. 浏览器填写数据,跳转页面返回数据不消失
  8. linux哪个文件夹不能乱改,Linux的文件夹权限如何更改
  9. bzoj3482: [COCI2013]hiperprostor
  10. ehcache 在web项目中使用
  11. 5.26. sysvmsg
  12. GAMP|Visual Studio 2019环境下配置GAMP
  13. 常用编程语言的介绍及特点
  14. 线性分类器定义和局限性
  15. matlab中电流继电器,电流电压继电器特性实验的数字仿真
  16. Facebook更名Meta,扎克伯格押注元宇宙
  17. 吴恩达-机器学习-简单决策树
  18. Java Shiro 设置 anon 无效
  19. 数模国赛计算机要学什么,数学建模国赛经验分享
  20. 微信自定义菜单和个性化菜单添加emoji表情(兼容ios和安卓,防止小方框)

热门文章

  1. 离职了,可以要求原公司补交公积金与少交的四险吗
  2. java将cst时间格式_CST 时间格式转换成普通的时间格式yyyy-MM-dd HH:mm:ss
  3. c语言程序设计和windows编程区别是什么呢。
  4. php区块链源码带语音播报|区块链理财|区块链游戏l抽奖功能|自动分红
  5. 阿里云服务器ECS选款利器,性能测试PTS详解
  6. 006-Sigle-基于blockstack去中心化博客
  7. Python【8】-分析json文件
  8. 看图识元件 硬件高手必备电子知识
  9. 化药有本黄皮书......
  10. matlab中索引超过维度,索引超出维度,怎么处理