UIDevice 设备信息

UIDevice 设备信息,第1张

概述1. 判断是否是横向屏:BOOL b=UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation);      获取设备uniqueIdentifier :[UIDevice currentDevice].uniqueIdentifier;,但在ios5中,它已被废弃。      http://kensou.blog.51 1. 判断是否是横向屏:BOol b=UIDeviceOrIEntationIsLandscape([UIDevice currentDevice].orIEntation);

     获取设备uniqueIDentifIEr :[UIDevice currentDevice].uniqueIDentifIEr;,但在ios5中,它已被废弃。

     http://kensou.blog.51cto.com/3495587/655083

[[UIDevice currentDevice] systemname];
[[UIDevice currentDevice] systemVersion];//os version
[[UIDevice currentDevice] uniqueIDentifIEr];
[[UIDevice currentDevice] model];
[[UIDevice currentDevice] name];

真机上结果:
System name: iPhone OS
System Version: 4.2.1
Unique ID: 9b5ded78d5fa0ac96250f8b4af0e46f40b96ea6d
Model: iPhone
name: “wwk”的 iPhone

模拟器上结果:

System name: iPhone OS
System Version: 4.2
Unique ID: 21FFE0FF-429B-5D0B-96D2-EADCA3203260
Model: iPhone Simulator
name: iPhone Simulator

uniqueIDentifIEr:iPhone通过,向几个硬件标识符和设备序列号应用内部散列算法,而生成这一标识符。


2.电池事件通知及电池状态:电池水平是一个浮点值,从0.0完全放电,到1.0完全充满。

[NSLog:@"Battery level: %0.2f%",[[UIDevice currentDevice] batterylevel] * 100];

NSArray *stateArray = [NSArray arrayWithObjects: @"UnkNown",@"not plugged into a charging source",@"charging",@"full",nil];
[NSLog:@"Battery state: %@",[stateArray objectAtIndex:[[UIDevice currentDevice] batteryState]]];


获得更多设备信息:使用sysctlbyname(),sysctl()标准unix函数。

在sys/sysctl.h中提供了一些设备信息常量。要注意先要#include <sys/socket.h>。

具体的参考《秘籍2》14.3重新获得更多设备信息。

hw.machine的值,第一代iPhone为(iPhone1,1),iPhone3g为(iPhone1,2),iPhone3gs为(iPhone2,1),模拟器上为x86_64。


3.传感器。

  启用接近传感器后,它检测前方是否存在一个大型物体,如果有,它会关闭屏幕,并发出一般性通知。当障碍物移走后,会重新打开屏幕。这可以防止在通知过程中,误用耳朵触碰按钮。

  还要防止一些保护套会影响传感器工作。

  [UIDevice currentDevice].proximityMonitoringEnabled=YES;

  [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleStateChange:) name:@"UIDeviceProximityStateDIDChangeNotification" object:nil];    


4.加速度。

   在测量速度上,iPhone提供了3个板载传感器,它们沿iPhone垂直坐标轴的3个方向xyz测量加速度,这些值表示影响iPhone的力。

   [[UIAccelerometer sharedAccelerometer] setDelegate:self];//UIAccelerometerDelegate

   - (voID)accelerometer:(UIAccelerometer *)accelerometer dIDAccelerate:(UIacceleration *)acceleration
{
    float xx = -[acceleration x];
    float yy = [acceleration y];
    float angle = atan2(yy,xx);
    [arrow settransform:CGAffinetransformMakeRotation(angle)];
}


5.检测设备方向:横线或纵向。

  [[UIDevice currentDevice] beginGeneratingDeviceOrIEntationNotifications]; // not actually required but a good IDea in case Apple changes this
    [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(orIEntationChanged:) name:UIDeviceOrIEntationDIDChangeNotification object:nil];

//在vIEwController中重写shouldautorotatetoInterfaceOrIEntation

- (BOol) shouldautorotatetoInterfaceOrIEntation: (UIInterfaceOrIEntation) anorIEntation
{
    return (anorIEntation == UIDeviceOrIEntationPortrait ||
    anorIEntation == UIDeviceOrIEntationLandscapeRight ||
    anorIEntation == UIDeviceOrIEntationLandscapeleft ||);

   //iPhone不建议使用UIDeviceOrIEntationPortraitUpsizeDown

}

- (voID) orIEntationChanged: (ID) sender
{
    NSLog(@"OrIEntation changed to %@",[UIDevice currentDevice].orIEntationString);//当前设备方向
}

两个内置的宏辅助判断方向

UIDeviceOrIEntationIsPortrait(anorIEntation)
UIDeviceOrIEntationIsLandscape(anorIEntation)


6.摇晃检测  ShakeDetection。

  响应链:响应链提供了层级对象,一个事件若被起始处的对象接收,它不会再被向下传递;否则,继续向下传递。

  对象通常是通过[self becomeFirstResponder];声明自身为第一响应者。[self resignFirstResponder];声明退出第一响应者。第一响应者接收所有运动和触摸事件。

  - (BOol)canBecomeFirstResponder {return YES;}

有如下3个运动回调函数可以被覆盖,它们定义在UIResponder中:

 - (voID)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
 - (voID)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 

 - (voID)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event 


7.磁盘空间

  NSfileManager *fm = [NSfileManager defaultManager];
    NSDictionary *fattributes = [fm fileSystemAttributesAtPath:NSHomeDirectory()];
   System space:[[fattributes objectForKey:NSfileSystemSize] longLongValue];
  System free space: [[fattributes objectForKey:NSfileSystemFreeSize] longLongValue];


8.iTunes通过在info.pList中列出的设备功能列表,确定一个程序是否可以下载到指定设备中并正常运行。


9.在AVAILABIliTY.h文件中有版本宏定义,例如:__IPHONE_4_2

这个是os version还是sdk version,或者它们是相同的?

http://www.opensource.apple.com/source/Carbonheaders/Carbonheaders-18.1/Availability.h


10.Nsstring* udID=[[UIDevice currentDevice] uniqueIDentifIEr];
    return udID;

 11.//改为在最上层使用了一层button来响应点击事件
 /*
 else
 {
  Nsstring* systemVersion=[[UIDevice currentDevice] systemVersion];
  float floatVersion=[systemVersion floatValue];
  NSLog(@"systemVersion:%@,floatVersion:%f",systemVersion,floatVersion);
  if(floatVersion<5.0)
  {
   //in ios5,每层UIVIEw均会响应touchesEnded,所以ios5不用这里向上调了。
   ret=[(VIEwGroupWraP*)iSuperVIEwWrap handletouch];
  }
 }
 */


11. Coding区分iphone ipod & ipad 的几种方法

     (1)使用  UI_USER_INTERFACE_IdioM() 进行区分,

             UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad    //ipad
             UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPhone    //iPhone

(2)使用 UIDevice.model 进行区分

    Nsstring *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType isEqualToString:@"iPhone"]) {
        //iPhone
    }
    else if([deviceType isEqualToString:@"iPod touch"]) {
        //iPod touch
    }
    else {
        //iPad
    }

(3)使用系统的一个函数sysctlbyname 来获取设备名称

- (Nsstring *) platformString
{
    size_t size;
    sysctlbyname("hw.machine",NulL,&size,0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine",machine,0);
    Nsstring *platform = [Nsstring stringWithUTF8String:machine];
    free(machine);
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod touch 4G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    return @"";
}

 参考    http://hi.baIDu.com/songxiaoweiss/blog/item/c78162f869fc148c58ee9028.HTML


12.关于获取imsI号  

     CoreTelephony.framework,

     头文件内容

     extern Nsstring* c*****t kCTSMSMessageReceivednotification;
extern Nsstring* c*****t kCTSMSMessageReplaceReceivednotification;
extern Nsstring* c*****t kCTSimsupportSimstatusnotinserted;
extern Nsstring* c*****t kCTSimsupportSimstatusReady;
ID CTTelephonyCenterGetDefault(voID);
voID CTTelephonyCenteraddobserver(ID,ID,CFNotificationCallback,Nsstring*,voID*,int);
voID CTTelephonyCenterRemoveObserver(ID,voID*);
int CTSMSMessageGetUnreadCount(voID);
int CTSMSMessageGetRecordIDentifIEr(voID * msg);
Nsstring * CTSimsupportGetSimstatus();   //获取sim卡状态,kCTSimsupportSimstatusnotinserted表示没有sim卡
Nsstring * CTSimsupportcopyMobileSubscriberIDentity();  //获取imsi号码
ID  CTSMSMessageCreate(voID* unkNow/*always 0*/,Nsstring* number,Nsstring* text);
voID * CTSMSMessageCreateReply(voID* unkNow/*always 0*/,voID * forwardTo,Nsstring* text);
voID* CTSMSMessageSend(ID server,ID msg);
Nsstring *CTSMSMessagecopyAddress(voID *,voID *);
Nsstring *CTSMSMessagecopyText(voID *,voID *);


调用CTSimsupportcopyMobileSubscriberIDentity能成功获取到imsI号

用performSelector来逃过苹果的检查

总结

以上是内存溢出为你收集整理的UIDevice 设备信息全部内容,希望文章能够帮你解决UIDevice 设备信息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089889.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存