iOS检测用户是否在iPad上

Alb*_*haw 248 objective-c device ipad ios swift

我有一个在iPhone和iPod Touch上运行的应用程序,它可以在Retina iPad上运行,但除此之外需要进行一次调整.我需要检测当前设备是否是iPad.我可以使用哪些代码来检测用户是否在我的iPad中使用iPad UIViewController然后相应地更改了某些内容?

Wri*_*sCS 578

有很多方法可以检查设备是否是iPad.这是我最喜欢检查设备是否实际上是iPad的方法:

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    return YES; /* Device is iPad */
}
Run Code Online (Sandbox Code Playgroud)

我使用它的方式

#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. */
}   
Run Code Online (Sandbox Code Playgroud)

其他例子

if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
    return YES; /* Device is iPad */
}

#define IPAD     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD ) 
     return YES;
Run Code Online (Sandbox Code Playgroud)

有关Swift解决方案,请参阅以下答案:https://stackoverflow.com/a/27517536/2057171

  • 你使用它的方式不尽如人意.`UI_USER_INTERFACE_IDIOM()`相当于`([[[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]?[[UIDevice currentDevice] userInterfaceIdiom]:UIUserInterfaceIdiomPhone)`.在某处缓存结果可能会更好:`BOOL iPad = UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad; ......如果(iPad)......` (22认同)
  • Swift:`if UIDevice.currentDevice().userInterfaceIdiom == .Pad` (16认同)
  • 我会在你的上一个方法中使用hasPrefix而不是isEqualToString.通过这种方式,代码也可以在模拟器上运行. (7认同)
  • 精彩!谢谢!正是我在寻找的!:) (2认同)
  • 宏的惊人使用。混淆代码的好方法。 (2认同)
  • @ gnasher729 500多个人倾向于不同意你。为何您不提供自己的答案,而不是the昧的评论,因为您认为自己有更好的方法。 (2认同)

Jee*_*hut 150

Swift中,您可以使用以下等式来确定通用应用程序上的设备类型:

UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad
Run Code Online (Sandbox Code Playgroud)

用法将是这样的:

if UIDevice.current.userInterfaceIdiom == .pad {
    // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
    // Implement your logic here
}
Run Code Online (Sandbox Code Playgroud)

  • 我正在编辑您回答的链接到已接受的答案中.(这样你也得到了信任).尽管这是一个客观问题,但很多观看此问题的人都来自Google,可能正在寻找Swift解决方案!:d (3认同)

Ric*_*ard 33

这是iOS 3.2中UIDevice的一部分,例如:

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad
Run Code Online (Sandbox Code Playgroud)

  • 成语通常更好,但如果你在iPad上运行iphone应用程序,那将返回UIUserInterfaceIdiomPhone. (4认同)

小智 25

你也可以用这个

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
   // iPad
} else {
   // iPhone / iPod Touch
}
Run Code Online (Sandbox Code Playgroud)


mal*_*hal 20

如果应用程序适用于iPad或Universal,UI_USER_INTERFACE_IDIOM()仅返回iPad.如果它的iPhone应用程序在iPad上运行,那么它就不会.所以你应该检查模型.


小智 15

小心:如果您的应用仅针对iPhone设备,则使用iphone兼容模式运行的iPad将在以下声明中返回false:

#define IPAD     UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
Run Code Online (Sandbox Code Playgroud)

检测物理iPad设备的正确方法是:

#define IS_IPAD_DEVICE      ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])
Run Code Online (Sandbox Code Playgroud)


And*_*ies 13

我发现在Xcode中的模拟器中,某些解决方案对我不起作用.相反,这有效:

ObjC

NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;

if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
    DebugLog(@"iPad");
} else {
    DebugLog(@"iPhone or iPod Touch");
}
Run Code Online (Sandbox Code Playgroud)

迅速

if UIDevice.current.model.hasPrefix("iPad") {
    print("iPad")
} else {
    print("iPhone or iPod Touch")
}
Run Code Online (Sandbox Code Playgroud)

此外,在Xcode的"其他示例"中,设备模型以"iPad模拟器"的形式返回,因此上述调整应该对其进行排序.


Kin*_*ard 8

Swift中有很多方法可以做到这一点:

我们检查下面的模型(我们只能在这里进行区分大小写的搜索):

class func isUserUsingAnIpad() -> Bool {
    let deviceModel = UIDevice.currentDevice().model
    let result: Bool = NSString(string: deviceModel).containsString("iPad")
    return result
}
Run Code Online (Sandbox Code Playgroud)

我们检查下面的模型(我们可以在这里做一个区分大小写/不敏感的搜索):

    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
        }
   }
Run Code Online (Sandbox Code Playgroud)

UIDevice.currentDevice().userInterfaceIdiom如果该应用适用于iPad或Universal,则仅返回iPad.如果它是在iPad上运行的iPhone应用程序,那么它不会.所以你应该检查模型.:

    class func isUserUsingAnIpad() -> Bool {
        if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
            return true
        } else {
            return false
        }
   }
Run Code Online (Sandbox Code Playgroud)

如果该类没有继承a UIViewController,则下面的代码段不会编译,否则它可以正常工作.UI_USER_INTERFACE_IDIOM()如果应用程序适用于iPad或Universal,则无论如何都会返回iPad.如果它是在iPad上运行的iPhone应用程序,那么它不会.所以你应该检查模型.:

class func isUserUsingAnIpad() -> Bool {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) {
        return true
    } else {
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我绝对认为我的答案很有用,因为首先所有答案都散布在堆栈溢出上.其次,对于iOS 8及更高版本,有时适用于旧版iOS的功能无法正常工作.所以我测试了这些解决方案,这个答案非常有用.所以我完全不同意你的看法. (4认同)
  • 我不认为有必要将标记为Objective-C的问题的旧答案重写为swift. (2认同)

Ash*_*k R 7

*

在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
    }
Run Code Online (Sandbox Code Playgroud)


Roh*_*dia 5

许多答案都不错,但我在Swift 4中这样使用

  1. 创建常量

    struct App {
        static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 这样使用

    if App.isRunningOnIpad {
        return load(from: .main, identifier: identifier)
    } else {
        return load(from: .ipad, identifier: identifier)
    }
    
    Run Code Online (Sandbox Code Playgroud)

编辑: 按照建议,Cur仅在UIDevice上创建扩展

extension UIDevice {
    static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}
Run Code Online (Sandbox Code Playgroud)

  • 当您可以对UIDevice扩展进行相同操作时,为什么还要对App结构进行打扰呢? (2认同)

iOS*_*iOS 5

Swift 4.2和 Xcode 10中

if UIDevice().userInterfaceIdiom == .phone {
    //This is iPhone
} else if UIDevice().userInterfaceIdiom == .pad { 
    //This is iPad
} else if UIDevice().userInterfaceIdiom == .tv {
    //This is Apple TV
}
Run Code Online (Sandbox Code Playgroud)

如果你想检测特定设备

let screenHeight = UIScreen.main.bounds.size.height
if UIDevice().userInterfaceIdiom == .phone {
    if (screenHeight >= 667) {
        print("iPhone 6 and later")
    } else if (screenHeight == 568) {
        print("SE, 5C, 5S")
    } else if(screenHeight<=480){
        print("4S")
    }
} else if UIDevice().userInterfaceIdiom == .pad { 
    //This is iPad
}
Run Code Online (Sandbox Code Playgroud)