使用iOS SDK和完整的Cocoa Touch / Objective-C代码确定用户的设备 [英] Determine user's device using iOS SDK and full Cocoa Touch / Objective-C code
问题描述
我根据不同的来源编写了以下UIDevice类别。我已经升级了 platformCode
方法,所以它的水平低于可以看到的水平。
I have written the following UIDevice category based on different sources. I've upgraded the platformCode
method so it's less low-level than can be seen.
这很有效,但 platformCode
方法是低级别的。你知道这种呼叫是否可以用Cocoa Touch代码替换?以下是相关代码:
This works perfectly, but the platformCode
method is low level. Do you know if this kind of call can be replaced with Cocoa Touch code? Here's the relevant code:
UIDevice_enhanced.h
@interface UIDevice (Enhanced)
typedef enum {
kUnknownPlatform = 0,
kiPhone1G,
kiPhone3G,
kiPhone3GS,
kiPhone4,
kiPhone4Verizon,
kiPhone4S,
kiPodTouch1G,
kiPodTouch2G,
kiPodTouch3G,
kiPodTouch4G,
kiPad,
kiPad2Wifi,
kiPad2GSM,
kiPad2CMDA,
kSimulator
} PlatformType;
- (NSString *) platformName;
- (PlatformType) platform;
@end
UIDevice_enhanced.m
#import "UIDevice_enhanced.h"
#include <sys/utsname.h>
@interface UIDevice (Enhanced)
- (NSString *) platformCode;
@end
@implementation UIDevice (Enhanced)
// Utility method (private)
- (NSString*) platformCode {
struct utsname systemInfo;
uname(&systemInfo);
NSString* platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
return platform;
}
// Public method to use
- (NSString*) platformName {
NSString* platform = [self platformCode];
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,2"]) return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
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 platform;
}
// Public method to use
- (PlatformType) platform {
NSString *platform = [self platformCode];
if ([platform isEqualToString:@"iPhone1,1"]) return kiPhone1G;
if ([platform isEqualToString:@"iPhone1,2"]) return kiPhone3G;
if ([platform isEqualToString:@"iPhone2,1"]) return kiPhone3GS;
if ([platform isEqualToString:@"iPhone3,1"]) return kiPhone4;
if ([platform isEqualToString:@"iPhone3,2"]) return kiPhone4Verizon;
if ([platform isEqualToString:@"iPhone4,1"]) return kiPhone4S;
if ([platform isEqualToString:@"iPod1,1"]) return kiPodTouch1G;
if ([platform isEqualToString:@"iPod2,1"]) return kiPodTouch2G;
if ([platform isEqualToString:@"iPod3,1"]) return kiPodTouch3G;
if ([platform isEqualToString:@"iPod4,1"]) return kiPodTouch4G;
if ([platform isEqualToString:@"iPad1,1"]) return kiPad;
if ([platform isEqualToString:@"iPad2,1"]) return kiPad2Wifi;
if ([platform isEqualToString:@"iPad2,2"]) return kiPad2GSM;
if ([platform isEqualToString:@"iPad2,3"]) return kiPad2CMDA;
if ([platform isEqualToString:@"i386"]) return kSimulator;
return kUnknownPlatform;
}
@end
推荐答案
这可以被视为目标-c方式:
This may be considered an "objective-c" way of doing it:
// Utility method (private)
- (NSString *)platformCode {
// This may or not be necessary
// Im not sure if you can have a device thats not currentDevice can you?
// if ([self isEqual:[UIDevice currentDevice]]) {
NSString* platform = [[self.systemName copy] autorelease];
return platform;
// Could probably shorten to just
// return [[self.systemName copy] autorelease];
// or - return [NSString stringWithString:self.systemName];
}
这是 utsname <的对象版本/ code>
machine
(来自此行: NSString * platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
)。
utsname
:
< sys / utsname.h>
header定义结构utsname,其中包括
至少以下成员:
The
<sys/utsname.h>
header defines structure utsname, which includes at least the following members:
char sysname []
此操作系统实现的名称
char nodename []
依赖于实现的
通信网络中此节点的名称
char release []
$当前版本级别b $ b此实现
char版本[]
此
版本的当前版本级别
char machine []
运行
系统的硬件类型名称
char sysname[]
name of this implementation of the operating system
char nodename[]
name of this node within an implementation-dependent
communications network
char release[]
current release level of
this implementation
char version[]
current version level of this
release
char machine[]
name of the hardware type on which the
system is running
systemName
接收器代表的设备
上运行的操作系统的名称。 (只读)
@property(非原子,只读,保留)NSString * system
但是,由于systemName只返回@iPhone OS,要获取实际的设备型号,必须使用c代码。这是另一种方法:
But, since systemName only returns @"iPhone OS", to get the actual device model number, you have to use c code. Here's another way to do it:
#include <sys/types.h>
#include <sys/sysctl.h>
- (NSString *)machine {
size_t size;
// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
// Allocate the space to store name
char *name = malloc(size);
// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);
// Place name into a string
NSString *machine = [NSString stringWithCString:name];
// Done with this
free(name);
return machine;
}
这篇关于使用iOS SDK和完整的Cocoa Touch / Objective-C代码确定用户的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!