我有一款应用可以在iPhone和iPod Touch上运行,也可以在Retina iPad和其他设备上运行,但需要做一些调整。我需要检测当前设备是否是iPad。我可以用什么代码来检测用户是否在我的UIViewController中使用iPad,然后相应地改变一些东西?
当前回答
你可以检查rangeOfString来查看单词iPad是否像这样存在。
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound) {
NSLog(@"I am an iPad");
} else {
NSLog(@"I am not an iPad");
}
其他回答
*
在swift 3.0中
*
if UIDevice.current.userInterfaceIdiom == .pad {
//pad
} else if UIDevice.current.userInterfaceIdiom == .phone {
//phone
} else if UIDevice.current.userInterfaceIdiom == .tv {
//tv
} else if UIDevice.current.userInterfaceIdiom == .carPlay {
//CarDisplay
} else {
//unspecified
}
斯威夫特的另一种方式:
//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!
}
这是iOS 3.2的UIDevice的一部分,例如:
[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad
if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.pad)
{
print("This is iPad")
}else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.phone)
{
print("This is iPhone");
}
在Swift中,您可以使用以下等式来确定通用应用程序上的设备类型:
UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad
用法就像这样:
if UIDevice.current.userInterfaceIdiom == .pad {
// Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
// Implement your logic here
}
推荐文章
- 我应该如何从字符串中删除所有的前导空格?- - - - - -斯威夫特
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- Swift:理解// MARK
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器