我有一款应用可以在iPhone和iPod Touch上运行,也可以在Retina iPad和其他设备上运行,但需要做一些调整。我需要检测当前设备是否是iPad。我可以用什么代码来检测用户是否在我的UIViewController中使用iPad,然后相应地改变一些东西?
当前回答
你也可以用这个
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
// iPad
} else {
// iPhone / iPod Touch
}
其他回答
在Swift中有很多方法可以做到这一点:
我们检查下面的模型(这里我们只能进行区分大小写的搜索):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let result: Bool = NSString(string: deviceModel).containsString("iPad")
return result
}
我们检查下面的模型(我们可以在这里进行区分大小写的搜索):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let deviceModelNumberOfCharacters: Int = count(deviceModel)
if deviceModel.rangeOfString("iPad",
options: NSStringCompareOptions.LiteralSearch,
range: Range<String.Index>(start: deviceModel.startIndex,
end: advance(deviceModel.startIndex, deviceModelNumberOfCharacters)),
locale: nil) != nil {
return true
} else {
return false
}
}
UIDevice.currentDevice()。下面的userInterfaceIdiom只返回iPad,如果应用程序是iPad或Universal。如果这是一款在iPad上运行的iPhone应用,那么它就不会这么做。所以你应该检查模型。:
class func isUserUsingAnIpad() -> Bool {
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
return true
} else {
return false
}
}
如果类没有继承UIViewController,下面的代码段不会编译,否则它就可以正常工作。无论如何,UI_USER_INTERFACE_IDIOM()只返回iPad,如果应用程序是iPad或通用。如果这是一款在iPad上运行的iPhone应用,那么它就不会这么做。所以你应该检查模型。:
class func isUserUsingAnIpad() -> Bool {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) {
return true
} else {
return false
}
}
有很多方法可以检查设备是否为iPad。这是我最喜欢的检查设备是否真的是iPad的方法:
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
return YES; /* Device is iPad */
}
我使用它的方式
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
if ( IDIOM == IPAD ) {
/* do something specifically for iPad. */
} else {
/* do something specifically for iPhone or iPod touch. */
}
其他的例子
if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
return YES; /* Device is iPad */
}
#define IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD )
return YES;
有关Swift解决方案,请参阅以下答案:https://stackoverflow.com/a/27517536/2057171
我认为这些答案都不能满足我的需要,除非我从根本上误解了某些东西。
我有一个应用程序(最初是一个iPad应用程序),我想在iPad和Mac上运行,在Catalyst下。我正在使用plist选项来缩放Mac界面以匹配iPad,但如果这是合理的,我想迁移到AppKit。当在Mac上运行时,我相信上述所有方法都告诉我,我是在iPad上运行。Catalyst的伪装相当彻底。
对于大多数问题,我确实理解代码在Mac上运行时应该假装它在iPad上。一个例外是,在Catalyst下滚动选择器在Mac上不可用,但在iPad上可用。我想弄清楚是否在运行时创建一个UIPickerView或做一些不同的事情。运行时选择是至关重要的,因为我希望在iPad和Mac上长期使用一个二进制文件,同时充分利用它们所支持的UI标准。
这些api可能会给普通的pre-Catalyst读者带来误导性的结果。例如:[UIDevice currentDevice]。当在Mac上的Catalyst下运行时,model返回@“iPad”。用户界面惯用的api维持了同样的错觉。
我发现你真的需要更深入地研究。我从以下信息开始:
NSString *const deviceModel = [UIDevice currentDevice].model;
NSProcessInfo *const processInfo = [[NSProcessInfo alloc] init];
const bool isIosAppOnMac = processInfo.iOSAppOnMac; // Note: this will be "no" under Catalyst
const bool isCatalystApp = processInfo.macCatalystApp;
然后,您可以将这些查询与[deviceModel hasPrefix: @"iPad"]这样的表达式结合起来,以整理出我所面临的各种微妙之处。对于我的例子,我明确地想要避免制作一个UIPickerView,如果指示的isCatalystApp为真,独立于“误导”信息的接口习语,或由isIosAppOnMac和deviceModel维持的幻觉。
现在我很好奇,如果我把Mac应用程序移动到我的iPad挎斗上会发生什么……
我发现在Xcode中的模拟器中,有些解决方案并不适合我。相反,这是可行的:
ObjC
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
DebugLog(@"iPad");
} else {
DebugLog(@"iPhone or iPod Touch");
}
斯威夫特
if UIDevice.current.model.hasPrefix("iPad") {
print("iPad")
} else {
print("iPhone or iPod Touch")
}
同样在Xcode中的“其他示例”中,设备模型返回为“iPad模拟器”,因此上述调整应该将其整理出来。
斯威夫特的另一种方式:
//MARK: - Device Check
let iPad = UIUserInterfaceIdiom.Pad
let iPhone = UIUserInterfaceIdiom.Phone
@available(iOS 9.0, *) /* AppleTV check is iOS9+ */
let TV = UIUserInterfaceIdiom.TV
extension UIDevice {
static var type: UIUserInterfaceIdiom
{ return UIDevice.currentDevice().userInterfaceIdiom }
}
用法:
if UIDevice.type == iPhone {
//it's an iPhone!
}
if UIDevice.type == iPad {
//it's an iPad!
}
if UIDevice.type == TV {
//it's an TV!
}
推荐文章
- 为什么Swift的编译时间这么慢?
- iPhone上UIView和UILabels的渐变
- 如何测试等价的Swift枚举与相关的值
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- 在Swift中转换Float为Int
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 我如何在Swift中声明一个弱引用数组?
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?