Alex Merlin
Alex Merlin

Reputation: 451

How to check if device is an iPad or iPhone not working

I am currently updating an app and I need to know whether the app is being used in an iPad or not.

I checked online and I found the code below. I used the iPad simulators in Xcode and ran both if statements. but whenever I run the code, nothing happens (the print message does not print) does this code work with simulators or am I doing something wrong?

When I check if it is UIUserInterfaceIdiom.phone the print statement executes, but I am using an iPad in the simulator.

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad){
        print("This is an iPad")
        redoButton.layer.position.y -= 500



    }

if UIDevice.current.userInterfaceIdiom == .pad{
        print("iPad True")
    }

Thank You

Upvotes: 11

Views: 10544

Answers (3)

Venu Gopal Tewari
Venu Gopal Tewari

Reputation: 5876

You can use:

if UIDevice.current.model == "iPad" {
        
}

Upvotes: 4

Awais Fayyaz
Awais Fayyaz

Reputation: 2415

If you want to frequently check iphone/ipad. use the following class

class Env {
  static var isIpad : Bool { return UIDevice.current.userInterfaceIdiom == .pad }
}

Then you can use like this

if Env.isIpad { // Ipad } 
else { iphone } 

Upvotes: 6

Wendra
Wendra

Reputation: 517

if UIDevice.current.userInterfaceIdiom == .pad {
    print("iPad")
}else{
    print("not iPad")
}

But you need to make your app a universal app.

Upvotes: 30

Related Questions