原创文章不易,转载时请保留原始链接:http://blog.csdn.net/ookcode/article/details/41865477 谢谢!!
大概在iOS SDK 3.2的时候,我们可以使用私有库Message.framework的MFNetworkController.h中的IMEI方法获取到设备的IMEI,但之后,这个方法被苹果移除了。
后来github上有一种新方法,使用IOKit_Extensions, 通过读取IO端口的方法, 取 device-imei
这种方法比较麻烦,这里推荐一种新方法,使用CoreTelephony库,它是一个公开库,只是有部分api没有公开罢了。
下面说下操作步骤:
1、导入CoreTelephony.framework
2、创建如下代码,并保存为CoreTelephony.h
struct CTServerConnection
{
int a;
int b;
CFMachPortRef myport;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
};
struct CTResult
{
int flag;
int a;
};
struct CTServerConnection * _CTServerConnectionCreate(CFAllocatorRef, void *, int *);
void _CTServerConnectionCopyMobileIdentity(struct CTResult *, struct CTServerConnection *, NSString **);
3、将创建的CoreTelephony.h导入你的工程里
4、然后使用如下方法获取IMEI
struct CTResult result;
NSString *imei;
struct CTServerConnection *sc = _CTServerConnectionCreate(kCFAllocatorDefault,(__bridge void*)self, NULL);
_CTServerConnectionCopyMobileIdentity(&result, sc, &imei);
if (imei.length == 14) {
imei = [imei stringByAppendingString:@"0"];
}
5、测试结果
iOS 5.11 √
iOS 7.1.2 √
iOS 8.1 最后一位数字(校验码)丢失
这个问题很奇怪,iOS8.1获取到的IMEI只有前14位,好在IMEI的最后一位是校验码,不是特别重要,直接补0即可
6、此方法不需要越狱便能获取到IMEI,如果是越狱环境的话可以参考我的上一篇文章
iOS7获取设备UDID、IMEI、ICCID、序列号、Mac地址等信息
7、此方法使用了CoreTelephony中两个未公开的API,不确保能通过苹果审核哦,各位请谨慎使用!
原创文章不易,转载时请保留原始链接:http://blog.csdn.net/ookcode/article/details/41865477 谢谢!!