iOS如何获取手机的Mac地址

首先说明下,下面两种方法均可以获得手机的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

#include

#include

方法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];

}

方法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 subsystem

mgmtInfoBase[1] = AF_ROUTE; // Routing table info

mgmtInfoBase[2] = 0;

mgmtInfoBase[3] = AF_LINK; // Request link layer information

mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces

// With all configured interfaces requested, get handle index

if ((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 call

if ((msgBuffer = malloc(length)) == NULL)

errorFlag = @"buffer allocation failure";

else

{

// Get system information, store in buffer

if (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 structure

interfaceMsgStruct = (struct if_msghdr *) msgBuffer;

// Map to link-level socket structure

socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);

// Copy link layer address data in socket structure to an array

memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);

// Read from char array into a string object, into traditional Mac address format

NSString *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 memory

free(msgBuffer);

return macAddressString;

}

以上就是iOS获取手机的Mac地址的两种方法,希望对大家的学习有所帮助。相关阅读:

jQuery通过点击行来删除HTML表格行的实现示例

通用无限极下拉菜单的实现代码

JS获取月的最后一天与JS得到一个月份最大天数的实例代码

oracle 实际值超过数据库某个字段指定长度报错解决

C#多线程学习之(五)使用定时器进行多线程的自动管理

jQuery插件PageSlide实现左右侧栏导航菜单

整理Oracle数据库中数据查询优化的一些关键点

win8.1睡眠后断网的解决方法

C#类中的属性使用总结(详解类的属性)

Linux系统中使用fdupes来查找并删除重复文件

Mac强制关机的4种方法以备不时之需

Linux系统中用于复制的cp和scp命令使用解析

深入解析Linux系统下的进程切换

yii2.0实现验证用户名与邮箱功能

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

  1. linux虚拟网卡修改mac地址,Win10秘笈:两种方式修改网卡物理地址(MAC)

    每台能够上网的电脑都有网卡,不管是有线还是无线,网卡本身都得有物理地址,也就是MAC(Media Access Control 或 Medium Access Control)地址.这个地址理论上是固 ...

  2. Mac OS 解决 remote: Permission to xxx/xxx.git denied to xxx. 的两种方案

    出现remote: Permission to xxx/xxx.git denied to xxx的具体原因我就不解释了,在网上搜索以上错误提示基本能找到很多关于这个报错的解释. 大概意思就是说我的电 ...

  3. jvm两种方式获取对象所占用的内存

    在开发过程中,我们有时需要来获取某个对象的大小,以方便我们参考,来决定开发的技术方案.jvm中提供了两种方式来获取一个对象的大小. 通过Instrumentation来计算对象的大小 编写计算代码: ...

  4. C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法。

    C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法. 原文:C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法. 一般我们在开发Windows Ph ...

  5. js文件里获取路由 vue_【源码拾遗】从vue-router看前端路由的两种实现

    本文由浅入深观摩vue-router源码是如何通过hash与History interface两种方式实现前端路由,介绍了相关原理,并对比了两种方式的优缺点与注意事项.最后分析了如何实现可以直接从文件 ...

  6. android 获取当前时间精确到毫秒的两种方法

    1.概述 在android app开发中,在项目中有些功能需求要求要获取当前时间精确到毫秒,已便于完成功能开发的需要,而在android 的系统api中提供了SimpleDateFormat和Cale ...

  7. Wordpress开发 - 获取作者头像的两种方法

    写在前面 今天又被一个头像坑搞惨了,一般我们获取作者的信息是都需要在循环判断中才可以使用的!比如获取作者名称 <?php the_author(); ?> .如果没有在 <?php ...

  8. 计算机与u盘连接使用,两种解决手机与U盘连接问题的方法!

    一些粉丝最近问了一个有趣的问题:手机可以连接到USB闪存盘吗?对于这个问题,我相信许多手机发烧友都非常感兴趣.许多朋友可能会认为手机的USB接口与USB闪存驱动器接口不同,因此无法将手机连接到USB闪 ...

  9. android 6.0获取手机imei,android获取手机信息大全,android获取大全,IMEI号,IESI号,...

    android获取手机信息大全,android获取大全,IMEI号,IESI号, IMEI号,IESI号,手机型号: [java] view plaincopyprint?private void g ...

最新文章

  1. web服务枚举组件不可用
  2. 将整数拆分为勾股数的问题解决
  3. Java学习笔记13(equals()方法;toString()方法)
  4. 2020-12-2(详细解释neg指令 以及SCAS ,STOS的运用)
  5. LInux 安全测试 2
  6. ng: Can't bind to 'ngModel' since it isn't a known property of 'input'. - Angular 6
  7. java实现对大文件切割下载_Java实现大文件的切割与合并操作示例
  8. 20-400-040-高可用-Flink集群的高可用搭建
  9. python 启动参数_python启动参数
  10. color ui的使用
  11. js一键批量打印_前端vue项目实现批量打印功能
  12. J2Cache简单使用
  13. 日语词频分析——mecab使用
  14. 智能体:华为给时代炼一炉钢
  15. Webots学习笔记(四)---舵轮模型
  16. 视频的帧率和分辨率以及码率的关系
  17. 【HaaS Python 硬件积木】 BMP280气压传感器
  18. 分享快速检测肖特基二极管的小窍门
  19. 什么是单反相机,和普通相机有什么区别
  20. mysql中怎么查询出昨天,明天,五天,一周内,三个月内,半年内的数

热门文章

  1. 第一次觉得学习学错了(献给数学专业的战友们)
  2. iodine_Iodine消除了一些限制Java开发人员的限制
  3. CSS学习(上)前端入门很简单
  4. 【Delphi】实现登陆教务系统并获取课表的艰苦历程
  5. eHR管理系统品牌排名,eHR人力资源系统主要功能有哪些呢
  6. jxl.read.biff.BiffException: Unable to recognize OLE stream:
  7. python发送带表格的邮件_PYTHON自动发送报表邮件
  8. 清华大学成功卫冕ASC18世界超算总决赛冠军,黑马上海科大斩获AI大奖
  9. 《百面机器学习》大纲思维导图
  10. 我今年二十三四岁(转)