如何检测用户是否在iPhone 4或3G / 3GS上运行应用程序?
我需要检测硬件,而不是iOS版本。
感谢您的帮助。
答案 0 :(得分:11)
随意使用此课程 - 我发现它here
用法
UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];
[h release];
UIDeviceHardware.h
//
// UIDeviceHardware.h
//
// Used to determine EXACT version of device software is running on.
#import <Foundation/Foundation.h>
@interface UIDeviceHardware : NSObject
- (NSString *) platform;
- (NSString *) platformString;
@end
UIDeviceHardware.m
//
// UIDeviceHardware.m
//
// Used to determine EXACT version of device software is running on.
#import "UIDeviceHardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDeviceHardware
- (NSString *) platform{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine];
free(machine);
return platform;
}
- (NSString *) platformString{
NSString *platform = [self platform];
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:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
return platform;
}
@end
答案 1 :(得分:6)
#import <sys/utsname.h>
+ (NSString*)deviceModelName {
struct utsname systemInfo;
uname(&systemInfo);
NSString *modelName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if([modelName isEqualToString:@"i386"]) {
modelName = @"iPhone Simulator";
}
else if([modelName isEqualToString:@"iPhone1,1"]) {
modelName = @"iPhone";
}
else if([modelName isEqualToString:@"iPhone1,2"]) {
modelName = @"iPhone 3G";
}
else if([modelName isEqualToString:@"iPhone2,1"]) {
modelName = @"iPhone 3GS";
}
else if([modelName isEqualToString:@"iPhone3,1"]) {
modelName = @"iPhone 4";
}
else if([modelName isEqualToString:@"iPhone4,1"]) {
modelName = @"iPhone 4S";
}
else if([modelName isEqualToString:@"iPod1,1"]) {
modelName = @"iPod 1st Gen";
}
else if([modelName isEqualToString:@"iPod2,1"]) {
modelName = @"iPod 2nd Gen";
}
else if([modelName isEqualToString:@"iPod3,1"]) {
modelName = @"iPod 3rd Gen";
}
else if([modelName isEqualToString:@"iPad1,1"]) {
modelName = @"iPad";
}
else if([modelName isEqualToString:@"iPad2,1"]) {
modelName = @"iPad 2(WiFi)";
}
else if([modelName isEqualToString:@"iPad2,2"]) {
modelName = @"iPad 2(GSM)";
}
else if([modelName isEqualToString:@"iPad2,3"]) {
modelName = @"iPad 2(CDMA)";
}
return modelName;
}
答案 2 :(得分:1)
您可以使用此代码检测iPhone OS版本。 float version = [[[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.0) {
// Only executes on version 3 or above.
}