我的iPhone 应用程序不是通用的,但它有一个功能,我想要让在iPad上玩的人也能使用。有没有一种方式可以检测出您正在兼容模式下运行的iPad?UIDevice 用于检测机器规格的方法都返回在iPhone上获得的值(至少在模拟器上)。我唯一能想到的是检测OS 3.2,但这种技术不会长久有效。
我的iPhone 应用程序不是通用的,但它有一个功能,我想要让在iPad上玩的人也能使用。有没有一种方式可以检测出您正在兼容模式下运行的iPad?UIDevice 用于检测机器规格的方法都返回在iPhone上获得的值(至少在模拟器上)。我唯一能想到的是检测OS 3.2,但这种技术不会长久有效。
最初的回答在这里:https://dev59.com/C1LTa4cB1Zd3GeqPc7qb#14864400
由于非常简短,因此重新发布:
如果应用程序是在iPad上以模拟器模式运行的iPhone应用程序,则其userInterfaceIdiom将为Phone,但模型类型为iPad。 您可以使用以下代码进行检查:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
[[[UIDevice currentDevice] model] hasPrefix:@"iPad"]) {
// This app is an iPhone app running on an iPad
}
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
以上每行都会返回一个NSString
。您可以进行字符串比较,如下所示:
NSString *model = [[UIDevice currentDevice] model];
NSLog(@"Current device model: \"%@\"", model);
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
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
[[UIDevice currentDevice] model]
返回的是iPhone Simulator
而不是iPad Simulator
。我猜当在设备上运行时,它将返回iPhone
而不是iPad
。这是一个模拟的iPhone环境,所以这并不奇怪。hw.machine
sysctl是一个有趣的建议,但在iPad模拟器上,它只返回i386
。你知道在iPad上运行非通用iPhone应用程序时它返回什么吗? - n8gray如果您需要知道是否在iPad上运行iPhone应用程序,以便将界面缩小到iPhone 4S大小,值得注意的是,在12.9英寸的iPad上运行的iPhone应用程序以iPhone SE大小呈现。添加对主屏幕比例的检查将解决此问题。
var isSmalliPhoneAppRunningOniPad: Bool {
return UIDevice.current.userInterfaceIdiom == .phone &&
UIDevice.current.model.hasPrefix("iPad") &&
UIScreen.main.scale == 2
}
你是否查看了"UIDevice.h" ?它有一个model属性,可以找出iPhone、iPod、iPad设备。
NSString *name; //例如:"我的iPhone"
NSString *model; //例如:@"iPhone",@"iPod Touch"
NSString *localizedModel; //本地化的model
NSString *systemName; //例如:@"iPhone OS"
NSString *systemVersion; //例如:@"2.0"
NSString *uniqueIdentifier; (已弃用) //一个基于各种硬件信息的每个设备唯一标识符的字符串
[[UIDevice currentDevice] model]
返回的是iPhone Simulator
而不是iPad Simulator
。 - n8gray