iphone开发之获取IMEI,serialnumber和系统背光灯亮度

简介:

对于iOS的理解,应该来是就是一个拥有比较完整的内核的BSD UNIX系统,其实很多的东西都是可以问系统的,并不是必须通过又爱又恨的Frameworks的。

这里将介绍如何在iphone下面,通过系统的底层字节获取系统背光灯亮度和设备的IMEI。

 

这是UIDevice的Catagary,需要手动添加IOKit.frameworks(如果你找不到,那算了)。

 

代码部分 Thanks  Erica Sadun。

 

////////////////头文件//////////////////////

/* 
Erica Sadun, http://ericasadun.com 
iPhone Developer's Cookbook, 3.0 Edition 
BSD License, Use at your own risk 
*/ 

/* 
http://broadcast.oreilly.com/2009/04/iphone-dev-iokit---the-missing.html 
  
In Xcode, I was surprised to see that Apple didn't include IOKit header files. When I tried to 
add #import <IOKit/IOKit.h>, the file could not be found. So I manually put together a simple 
header file by hand, added IOKit to my frameworks and attempted to compile. 
  
As you can see from the screenshot at the top of this post, I failed to do so. Xcode complained 
that the IOKit framework could not be found. Despite being filed as public, IOKit is apparently 
not meant to be used by the general public. As iPhone evangelist Matt Drance tweeted, 
"IOKit is not public on iPhone. Lack of headers and docs is rarely an oversight." 
  
In the official docs, I found a quote that described IOKit as such: "Contains interfaces used by 
the device. Do not include this framework directly." So in the end, my desire to access that IOKit 
information was thwarted. For whatever reason, Apple has chosen to list it as a public framework 
but the reality is that it is not. 
*/ 

#import <UIKit/UIKit.h> 

#define SUPPORTS_IOKIT_EXTENSIONS    1 

/* 
* To use, you must add the (semi)public IOKit framework before compiling 
*/ 

#if SUPPORTS_IOKIT_EXTENSIONS 
@interface UIDevice (IOKit_Extensions) 
- (NSString *) imei; 
- (NSString *) serialnumber; 
- (NSString *) backlightlevel; 
@end 
#endif

///////////////实现文件////////////////////

 

/* 
Erica Sadun, http://ericasadun.com 
iPhone Developer's Cookbook, 3.0 Edition 
BSD License, Use at your own risk 
*/ 

#import "UIDevice-IOKitExtensions.h" 
#include <sys/types.h> 
#include <sys/sysctl.h> 
#import <mach/mach_host.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <netdb.h> 
#include <ifaddrs.h> 
#include <sys/socket.h> 
#include <net/if.h> 
#include <net/if_dl.h> 
#include <ifaddrs.h> 

#if SUPPORTS_IOKIT_EXTENSIONS 
#pragma mark IOKit miniheaders 

#define kIODeviceTreePlane        "IODeviceTree" 

enum { 
    kIORegistryIterateRecursively    = 0x00000001, 
    kIORegistryIterateParents        = 0x00000002 
}; 

typedef mach_port_t    io_object_t; 
typedef io_object_t    io_registry_entry_t; 
typedef char        io_name_t[128]; 
typedef UInt32        IOOptionBits; 

CFTypeRef 
IORegistryEntrySearchCFProperty( 
                                io_registry_entry_t    entry, 
                                const io_name_t        plane, 
                                CFStringRef        key, 
                                CFAllocatorRef        allocator, 
                                IOOptionBits        options ); 

kern_return_t 
IOMasterPort( mach_port_t    bootstrapPort, 
             mach_port_t *    masterPort ); 

io_registry_entry_t 
IORegistryGetRootEntry( 
                       mach_port_t    masterPort ); 

CFTypeRef 
IORegistryEntrySearchCFProperty( 
                                io_registry_entry_t    entry, 
                                const io_name_t        plane, 
                                CFStringRef        key, 
                                CFAllocatorRef        allocator, 
                                IOOptionBits        options ); 

kern_return_t   mach_port_deallocate 
(ipc_space_t                               task, 
mach_port_name_t                          name); 


@implementation UIDevice (IOKit_Extensions) 
#pragma mark IOKit Utils 
NSArray *getValue(NSString *iosearch) 

    mach_port_t          masterPort; 
    CFTypeID             propID = (CFTypeID) NULL; 
    unsigned int         bufSize; 
    
    kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort); 
    if (kr != noErr) return nil; 
    
    io_registry_entry_t entry = IORegistryGetRootEntry(masterPort); 
    if (entry == MACH_PORT_NULL) return nil; 
    
    CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (CFStringRef) iosearch, nil, kIORegistryIterateRecursively); 
    if (!prop) return nil; 
    
    propID = CFGetTypeID(prop); 
    if (!(propID == CFDataGetTypeID())) 
    { 
        mach_port_deallocate(mach_task_self(), masterPort); 
        return nil; 
    } 
    
    CFDataRef propData = (CFDataRef) prop; 
    if (!propData) return nil; 
    
    bufSize = CFDataGetLength(propData); 
    if (!bufSize) return nil; 
    
    NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1] autorelease]; 
    mach_port_deallocate(mach_task_self(), masterPort); 
    return [p1 componentsSeparatedByString:@"\0"]; 


- (NSString *) imei 

    NSArray *results = getValue(@"device-imei"); 
    if (results) return [results objectAtIndex:0]; 
    return nil; 


- (NSString *) serialnumber 

    NSArray *results = getValue(@"serial-number"); 
    if (results) return [results objectAtIndex:0]; 
    return nil; 


- (NSString *) backlightlevel 

    NSArray *results = getValue(@"backlight-level"); 
    if (results) return [results objectAtIndex:0]; 
    return nil; 

@end 
#endif










本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/577944,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
编解码 测试技术 iOS开发
iPhone 屏幕尺寸和开发适配
【10月更文挑战第23天】iPhone 的屏幕尺寸变化给开发者带来了一定的挑战,但也为创新提供了机遇。通过深入了解不同屏幕尺寸的特点,遵循适配原则和策略,运用合适的技巧和方法,我们能够为用户提供在不同 iPhone 机型上都具有良好体验的应用。在未来,随着技术的不断进步,我们还需要持续学习和适应,以满足用户对优质应用体验的不断追求。
|
3月前
|
编解码 iOS开发 UED
响应式设计在 iPhone 开发适配中的具体应用
【10月更文挑战第23天】响应式设计在 iPhone 开发适配中扮演着至关重要的角色,它能够帮助我们打造出适应不同屏幕尺寸和用户需求的高质量应用。通过合理运用响应式设计的原则和方法,我们可以在提供良好用户体验的同时,提高开发效率和应用的可维护性。
|
6月前
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
67 0
|
9月前
|
网络虚拟化 iOS开发
苹果手机无法更新系统问题
苹果手机无法更新系统问题
158 0
苹果手机无法更新系统问题
|
9月前
|
Android开发 iOS开发
【教程】如何在苹果手机上查看系统文件?
苹果手机与安卓手机不同,无法直接访问系统文件夹。但是,如果我们想要查看苹果手机的系统文件,可以借助一些工具来实现。本文将介绍一款名为克魔助手的iOS设备管理软件,它能帮助我们轻松查看苹果手机的系统文件。 连接iPhone到电脑,打开克魔助手,用数据线将iPhone连接到电脑上。连接成功后,克魔助手会自动读取iPhone的信息,并显示在软件的主界面上。
【教程】如何在苹果手机上查看系统文件?
|
9月前
|
iOS开发
【怒怼老乔】居然苹果手机IOS系统还不支持css3的transparent属性值,我去~~~~
【怒怼老乔】居然苹果手机IOS系统还不支持css3的transparent属性值,我去~~~~
|
数据管理 文件存储 数据安全/隐私保护
iMazing2023免费版苹果手机系统设备数据传输与备份工具
iMazing需要数据线将你的电脑和iPhone或者是iPad连接,这款软件是itunes的完美替代品,有用iPhone或iPad的朋们友推荐下载使用。只要在同一网络下,就可以轻松管理你的iPhone,可以说是非常的方便。平时在传输文件资料时,可以将iMazing充分利用起来,它可以对iQS设备进行强有力的管理。下载末尾安装包!
265 0
iMazing2023免费版苹果手机系统设备数据传输与备份工具
|
iOS开发
小技巧 - 苹果手机(IOS系统)备忘录如何置顶文件?
小技巧 - 苹果手机(IOS系统)备忘录如何置顶文件?
263 0
小技巧 - 苹果手机(IOS系统)备忘录如何置顶文件?
|
数据安全/隐私保护 iOS开发
小技巧 - iPhone手机(IOS系统)玩游戏时关闭所有消息提醒
小技巧 - iPhone手机(IOS系统)玩游戏时关闭所有消息提醒
927 0
小技巧 - iPhone手机(IOS系统)玩游戏时关闭所有消息提醒
|
编解码 安全 Android开发
iPhone苹果手机如何设置使用非系统自带铃声
自从开始使用苹果手机,如何设置自定义的手机铃声成了困扰我的难题,每次听着系统自带的铃声响起都很不喜,拿到iPhone plus我就开始研究怎么设置自定义的铃声,试了很多办法,都不行。电脑是win10系统,查了很多方法,下了各种软件,都没解决这问题。网上讲的最多的是下载iTunes云云,我卸了装,装了卸的,反复各种试都行不通;就卡在win10环境下的iTunes与iPhone手机连接不上,提示如图的问题:
492 0
iPhone苹果手机如何设置使用非系统自带铃声