iOS 检测用户是否在 iPad 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10167221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:52:31  来源:igfitidea点击:

iOS detect if user is on an iPad

iosobjective-cswiftipaddevice

提问by Albert Renshaw

I have an app that runs on the iPhone and iPod Touch, it can run on the Retina iPad and everything but there needs to be one adjustment. I need to detect if the current device is an iPad. What code can I use to detect if the user is using an iPad in my UIViewControllerand then change something accordingly?

我有一个可以在 iPhone 和 iPod Touch 上运行的应用程序,它可以在 Retina iPad 和所有东西上运行,但需要做一个调整。我需要检测当前设备是否是 iPad。我可以使用什么代码来检测用户是否在我的 iPad 中使用UIViewController,然后相应地更改某些内容?

回答by WrightsCS

There are quite a few ways to check if a device is an iPad. This is my favorite way to check whether the device is in fact an iPad:

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

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    return YES; /* Device is iPad */
}

The way I use it

我使用它的方式

#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. */
}   

Other Examples

其他例子

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

#define IPAD     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD ) 
     return YES;

For a Swift solution, see this answer: https://stackoverflow.com/a/27517536/2057171

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

回答by Jeehut

In Swiftyou can use the following equalities to determine the kind of deviceon Universal apps:

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

UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad

Usagewould then be something like:

用法将类似于:

if UIDevice.current.userInterfaceIdiom == .pad {
    // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified
    // Implement your logic here
}

回答by Richard

This is part of UIDevice as of iOS 3.2, e.g.:

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

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad

回答by Chilly

You can also use this

你也可以用这个

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
   // iPad
} else {
   // iPhone / iPod Touch
}

回答by malhal

UI_USER_INTERFACE_IDIOM()only returns iPad if the app is for iPad or Universal. If its an iPhone app running on an iPad then it won't. So you should instead check the model.

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

回答by Andy Davies

I found that some solution didn't work for me in the Simulator within Xcode. Instead, this works:

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

ObjC

对象

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

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

Swift

迅速

if UIDevice.current.model.hasPrefix("iPad") {
    print("iPad")
} else {
    print("iPhone or iPod Touch")
}

Also in the 'Other Examples' in Xcode the device model comes back as 'iPad Simulator' so the above tweak should sort that out.

同样在 Xcode 的“其他示例”中,设备模型作为“iPad 模拟器”返回,因此上面的调整应该可以解决这个问题。

回答by peak

Be Careful: If your app is targeting iPhone device only, iPad running with iphone compatible mode will return false for below statement:

小心:如果您的应用程序仅针对 iPhone 设备,在 iphone 兼容模式下运行的 iPad 将返回 false 以下语句:

#define IPAD     UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

The right way to detect physical iPad device is:

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

#define IS_IPAD_DEVICE      ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])

回答by Ashok R

*

*

In swift 3.0

在快速 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
    }

回答by Rohit Sisodia

Many Answers are good but I use like this in swift 4

许多答案都很好,但我在 swift 4 中是这样使用的

  1. Create Constant

    struct App {
        static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
    }
    
  2. Use like this

    if App.isRunningOnIpad {
        return load(from: .main, identifier: identifier)
    } else {
        return load(from: .ipad, identifier: identifier)
    }
    
  1. 创建常量

    struct App {
        static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
    }
    
  2. 像这样使用

    if App.isRunningOnIpad {
        return load(from: .main, identifier: identifier)
    } else {
        return load(from: .ipad, identifier: identifier)
    }
    

Edit:As Suggested C?ur simply create an extension on UIDevice

编辑:如建议 C?ur 只需在 UIDevice 上创建一个扩展

extension UIDevice {
    static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}

回答by King-Wizard

Many ways to do that in Swift:

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

We check the model below (we can only do a case sensitive search here):

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

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

We check the model below (we can do a case sensitive/insensitive search here):

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

    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().userInterfaceIdiombelow only returns iPad if the app is for iPad or Universal. If it is an iPhone app being ran on an iPad then it won't. So you should instead check the model. :

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

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

This snippet below does not compile if the class does not inherit of an UIViewController, otherwise it works just fine. Regardless UI_USER_INTERFACE_IDIOM()only returns iPad if the app is for iPad or Universal. If it is an iPhone app being ran on an iPad then it won't. So you should instead check the model. :

如果类不继承 an 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
    }
}