对于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
分享到:
相关推荐
ASP实例开发源码—苹果(iphone)序列号和IMEI码查询系统.zip
ASP源码—苹果(iphone)序列号和IMEI码查询系统.zip
基于ASP的苹果(iphone)序列号和IMEI码查询系统.zip
iphone开发自动获取本地实时天气~~
苹果(iphone)序列号和IMEI码查询系统 本程序操作简单 将整站数据上传到网站根目录即可 图片与css js文件都在index_files文件夹 index.html首页修改
iPhone开发实战 iPhone开发 iPhone iPhone4 iPhone开发实战 iPhone开发 iPhone iPhone4
Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码——Iphone主题源码Iphone开发系列源码...
这本书也写得非常好,里面非常系统地讲解了开发iphone和ipad上面的游戏需要注意的一些问题,比如内存很少的问题等。同时书里还涉及了cocos2d开发中大部分的内容,基本上可以说是所有的内容。同时,这本书的作者的...
Iphone开发系列源码——图表和报表的统计功能Iphone开发系列源码——图表和报表的统计功能Iphone开发系列源码——图表和报表的统计功能Iphone开发系列源码——图表和报表的统计功能Iphone开发系列源码——图表和报表...
IPHONE开发教程之IPHONE系统讲解,PPT教程,方便做教案
Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码——iPhone版Wordpress源代码Iphone开发系列源码...
iphone开发之xml解析iphone开发之xml解析iphone开发之xml解析iphone开发之xml解析iphone开发之xml解析
Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image图片缩放随着手指Iphone开发系列源码——Image...
Iphone开发系列源码——长按实现图标抖动和删除的代码Iphone开发系列源码——长按实现图标抖动和删除的代码Iphone开发系列源码——长按实现图标抖动和删除的代码Iphone开发系列源码——长按实现图标抖动和删除的代码...
《iPhone开发基础教程》是一部关于iPhone SDK和iPhone开发的基础教程,内容翔实、语言生动。书中结合消费类设备上常见的实例,循序渐进地讲解了iPhone开发的基本流程,并介绍了最先进、时尚、受欢迎的iPhone 特性。
iPhone开发,iPhone开发教程,iPhone开发基础教程PDF版
知名的Head First系列丛书之一,风格与其他Head First系列一脉相承,一定能让读者轻松学会iPhone开发,《深入浅出iPhone开发》是针对iPhone开发的初学者设计的,以几个应用实例的开发为例,循序渐进地对iPhone开发的...
本书全面探讨了iPhone平台的两种编程方式——Web开发和SDK编程。全在Web开发方面,分别介绍了三个iPhone Web库,即WebKit、iUI和Canvas,并讨论了Web开发环境Dashcode,最后阐述Web应用程序的调试。在SDK开发方面,...
详细描述获取iphone手机获取device id即UDID的方法的文档
资源名称:iPhone开发入门到精通视频教程资源目录:【】iOS开发源码系列---工具【】iOS开发源码系列---应用【】iOS开发源码系列---游戏【】iOS开发源码系列---类库与框架【】iOS开发真机测试与发布【】iOS开发视频...