n8g*_*ray 11 iphone cocoa-touch objective-c ipad
我的iPhone应用程序不是通用的,但它有一个功能,我想为在iPad上玩的人启用.有没有办法在兼容模式下检测到你在iPad上运行?用于检测机器规格的UIDevice方法都返回您在iPhone上获得的值(至少在模拟器上).我唯一能想到的就是检测OS 3.2,但这种技术不会长时间运行.
mpa*_*zer 20
最初在这里回答:https://stackoverflow.com/a/14864400/577237
转贴,因为它太短了:
如果该应用程序是在iPad上以模拟器模式运行的iPhone应用程序,则它将具有Phone的userInterfaceIdiom,但是具有iPad的型号类型.您可以使用以下代码进行检查:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
[[[UIDevice currentDevice] model] hasPrefix:@"iPad"]) {
// This app is an iPhone app running on an iPad
}
Run Code Online (Sandbox Code Playgroud)
1)使用 Erica Sadun编写的UIDevice-Extension.一个非常全面的课程:http: //github.com/erica/uidevice-extension/blob/master/UIDevice-Hardware.m
2)或者你也可以使用UIDevice类方法:
[[UIDevice currentDevice] name] // eg. "Brock's iPhone"
[[UIDevice currentDevice] model] // eg. @"iPhone", @"iPod Touch"
[[UIDevice currentDevice] localizedModel] // localized version of model
[[UIDevice currentDevice] systemName] // eg. @"iPhone OS"
[[UIDevice currentDevice] systemVersion] // eg. @"3.2"
[[UIDevice currentDevice] uniqueIdentifier] // UDID, a unique string to identify the device
Run Code Online (Sandbox Code Playgroud)
上面的每一行都会返回一个NSString.您可以像这样进行字符串比较:
NSString *model = [[UIDevice currentDevice] model];
NSLog(@"Current device model: \"%@\"", model);
Run Code Online (Sandbox Code Playgroud)
3)另一种方式:
http://www.drobnik.com/touch/2009/07/determining-the-hardware-model/ 您需要对其进行修改才能使用适合iPad的硬件编号.取自上面的链接:
的UIDevice-hardware.h
#import
#define IPHONE_1G_NAMESTRING @"iPhone 1G"
#define IPHONE_3G_NAMESTRING @"iPhone 3G"
#define IPHONE_3GS_NAMESTRING @"iPhone 3GS"
#define IPOD_1G_NAMESTRING @"iPod touch 1G"
#define IPOD_2G_NAMESTRING @"iPod touch 2G"
@interface UIDevice (Hardware)
- (NSString *) platform;
- (NSString *) platformString;
@end
Run Code Online (Sandbox Code Playgroud)
的UIDevice-hardware.m
#import "UIDevice-hardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDevice (Hardware)
/*
Platforms
iPhone1,1 = iPhone 1G
iPhone1,2 = iPhone 3G
iPhone2,1 = iPhone 3GS
iPod1,1 = iPod touch 1G
iPod2,1 = iPod touch 2G
*/
- (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_NAMESTRING;
if ([platform isEqualToString:@"iPhone1,2"]) return IPHONE_3G_NAMESTRING;
if ([platform isEqualToString:@"iPhone2,1"]) return IPHONE_3GS_NAMESTRING;
if ([platform isEqualToString:@"iPod1,1"]) return IPOD_1G_NAMESTRING;
if ([platform isEqualToString:@"iPod2,1"]) return IPOD_2G_NAMESTRING;
return NULL;
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6304 次 |
| 最近记录: |