使用iPhone SDK确定设备(iPhoneiPod Touch)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用iPhone SDK确定设备(iPhoneiPod Touch)相关的知识,希望对你有一定的参考价值。

Here is how to determine the device type. How to use it. UIDeviceHardware *h=[[UIDeviceHardware alloc] init]; [self setDeviceModel:[h platformString]]; [h release];
  1. //
  2. // UIDeviceHardware.h
  3. //
  4. // Used to determine EXACT version of device software is running on.
  5.  
  6. #import <Foundation/Foundation.h>
  7.  
  8. @interface UIDeviceHardware : NSObject
  9.  
  10. - (NSString *) platform;
  11. - (NSString *) platformString;
  12.  
  13. @end
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. //
  21. // UIDeviceHardware.m
  22. //
  23. // Used to determine EXACT version of device software is running on.
  24.  
  25. #import "UIDeviceHardware.h"
  26. #include <sys/types.h>
  27. #include <sys/sysctl.h>
  28.  
  29. @implementation UIDeviceHardware
  30.  
  31. - (NSString *) platform{
  32. size_t size;
  33. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  34. char *machine = malloc(size);
  35. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  36. NSString *platform = [NSString stringWithCString:machine];
  37. free(machine);
  38. return platform;
  39. }
  40.  
  41. - (NSString *) platformString{
  42. NSString *platform = [self platform];
  43. if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
  44. if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
  45. if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
  46. if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
  47. if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
  48. if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
  49. if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
  50. if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
  51. return platform;
  52. }
  53.  
  54. @end

以上是关于使用iPhone SDK确定设备(iPhoneiPod Touch)的主要内容,如果未能解决你的问题,请参考以下文章

Iphone 4 sdk - 如何确定 applicationWillEnterForeground 是不是因通知而被触发?

如何以编程方式确定应用程序正在iphone或ipod中运行[重复]

如何使用 iOS 6 sdk 为 iPhone 4s 或 iPhone 4 制作视图控制器?

iphone sdk - Itunes connect 如何更改设备要求?

iPhone SDK:可以通过iPad设备访问Localhost吗? (调试时)

如何在 iPhone sdk 中以编程方式获取设备名称?