检测iPhone/iPod Touch/iPad设备类型

检测iPhone/iPod Touch/iPad设备类型,第1张

概述支持iPhone4和iPad,iPod Touch4 #import <Foundation/Foundation.h> #import <sys/utsname.h> enum {     MODEL_UNKNOWN,     MODEL_IPHONE_SIMULATOR,     MODEL_IPOD_TOUCH,     MODEL_IPOD_TOUCH_2G,     MODEL_IPOD 支持iPhone4和iPad,iPod touch4 #import <Foundation/Foundation.h> #import <sys/utsname.h> enum {     MODEL_UNKNowN,    MODEL_IPHONE_SIMulATOR,    MODEL_IPOD_touch,    MODEL_IPOD_touch_2G,    MODEL_IPOD_touch_3G,    MODEL_IPOD_touch_4G,    MODEL_IPHONE,    MODEL_IPHONE_3G,    MODEL_IPHONE_3GS,    MODEL_IPHONE_4G,    MODEL_IPAD }; @interface DeviceDetection : NSObject + (uint) detectDevice; + (int) detectModel; + (Nsstring *) returnDevicename:(BOol)ignoreSimulator; + (BOol) isIPodtouch; + (BOol) isOS4; + (BOol) canSendSms; @end #import "DeviceDetection.h" #import <MessageUI/MessageUI.h> #include <sys/types.h> #include <sys/sysctl.h> @implementation DeviceDetection + (BOol) isIPodtouch {     int model = [DeviceDetection detectDevice];     if (model == MODEL_IPOD_touch || model == MODEL_IPAD){         //|| model == MODEL_IPHONE_SIMulATOR){         return YES;     }         else {         return NO;     } } + (BOol) isOS4 {     // TBD          return YES;      } + (BOol)canSendSms {     return [MFMessageComposeVIEwController canSendText]; } + (Nsstring *)platform{     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);     return platform; } + (int) detectModel{     Nsstring *platform = [DeviceDetection platform];          if ([platform isEqualToString:@"iPhone1,1"])            return MODEL_IPHONE;          if ([platform isEqualToString:@"iPhone1,2"])            return MODEL_IPHONE_3G;          if ([platform isEqualToString:@"iPhone2,1"])         return MODEL_IPHONE_3GS;          if ([platform isEqualToString:@"iPhone3,1"])             return MODEL_IPHONE_4G;          if ([platform isEqualToString:@"iPod1,1"])               return MODEL_IPOD_touch;          if ([platform isEqualToString:@"iPod2,1"])               return MODEL_IPOD_touch_2G;          if ([platform isEqualToString:@"iPod3,1"])               return MODEL_IPOD_touch_3G;          if ([platform isEqualToString:@"iPod4,1"])               return MODEL_IPOD_touch_4G;          if ([platform isEqualToString:@"iPad1,1"])               return MODEL_IPAD;          if ([platform isEqualToString:@"i386"])                  return MODEL_IPHONE_SIMulATOR;          return MODEL_UNKNowN; } + (uint) detectDevice {     Nsstring *model= [[UIDevice currentDevice] model];          // Some iPod touch return "iPod touch",others just "iPod"          Nsstring *iPodtouch = @"iPod touch";     Nsstring *iPodtouchLowerCase = @"iPod touch";     Nsstring *iPodtouchShort = @"iPod";     Nsstring *iPad = @"iPad";          Nsstring *iPhonesimulator = @"iPhone Simulator";          uint detected;          if ([model compare:iPhonesimulator] == NSOrderedSame) {         // iPhone simulator         detected = MODEL_IPHONE_SIMulATOR;     }     else if ([model compare:iPad] == NSOrderedSame) {         // iPad         detected = MODEL_IPAD;     } else if ([model compare:iPodtouch] == NSOrderedSame) {         // iPod touch         detected = MODEL_IPOD_touch;     } else if ([model compare:iPodtouchLowerCase] == NSOrderedSame) {         // iPod touch         detected = MODEL_IPOD_touch;     } else if ([model compare:iPodtouchShort] == NSOrderedSame) {         // iPod touch         detected = MODEL_IPOD_touch;     } else {         // Could be an iPhone V1 or iPhone 3G (model should be "iPhone")         struct utsname u;                  // u.machine Could be "i386" for the simulator,"iPod1,1" on iPod touch,"iPhone1,1" on iPhone V1 & "iPhone1,2" on iPhone3G                  uname(&u);                  if (!strcmp(u.machine,1")) {             detected = MODEL_IPHONE;         } else if (!strcmp(u.machine,2")){             detected = MODEL_IPHONE_3G;         } else if (!strcmp(u.machine,"iPhone2,1")){             detected = MODEL_IPHONE_3GS;         } else if (!strcmp(u.machine,"iPhone3,1")){             detected = MODEL_IPHONE_4G;         }     }     return detected; } + (Nsstring *) returnDevicename:(BOol)ignoreSimulator {     Nsstring *returnValue = @"UnkNown";          switch ([DeviceDetection detectDevice]) {         case MODEL_IPHONE_SIMulATOR:             if (ignoreSimulator) {                 returnValue = @"iPhone 3G";             } else {                 returnValue = @"iPhone Simulator";             }             break;         case MODEL_IPOD_touch:             returnValue = @"iPod touch";             break;         case MODEL_IPHONE:             returnValue = @"iPhone";             break;         case MODEL_IPHONE_3G:             returnValue = @"iPhone 3G";             break;         default:             break;     }          return returnValue; } @end 总结

以上是内存溢出为你收集整理的检测iPhone/iPod Touch/iPad设备类型全部内容,希望文章能够帮你解决检测iPhone/iPod Touch/iPad设备类型所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1091569.html

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

发表评论

登录后才能评论

评论列表(0条)

保存