Identify whether a user is on an iPhone or iPad and whether the device is in portrait or landscape mode.

We will explore ways to identify the user’s iPad or iPhone device. With the latest Xcode build, we will do all this in Swift Programming Language.

iOS devices will be identified in four ways: .phone, .pad, .tv & .unspecified.

So the code will look like this in swift

Code for iOS 13, iPadOS 13, macOS 10.15 Catalyst and above:

if UIDevice.current.userInterfaceIdiom == .phone {

} else if UIDevice.current.userInterfaceIdiom == .pad {

} else if UIDevice.current.userInterfaceIdiom == .carPlay {

} else if UIDevice.current.userInterfaceIdiom == .tv {

} else if UIDevice.current.userInterfaceIdiom == .unspecified {

}

Code for iOS 12 and below:

if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
    print("It's iPad")
    // Your code here

} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.phone) {
    print("It's iPhone")
    // Your code here

} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.tv) {
    print("It's Apple TV")
    // Your code here

} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.unspecified) {
    print("It's Unknown Device")
    // Your code here

}

Now, we will explore how to identify the iOS device orientation of iPad & iPhone.

There are seven types of iOS device orientations:

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
    print("It's UIDeviceOrientation.landscapeLeft")
    // Landscape
    // Your code here

} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
    print("It's UIDeviceOrientation.landscapeRight")
    // Landscape
    // Your code here

} else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
    print("It's UIDeviceOrientation.portrait")
    // Portrait
    // Your code here

} else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
    print("It's UIDeviceOrientation.portraitUpsideDown")
    // Portrait
    // Your code here

} else if UIDevice.current.orientation == UIDeviceOrientation.faceUp {
    print("It's UIDeviceOrientation.faceUp")
    // faceUp
    // Your code here

} else if UIDevice.current.orientation == UIDeviceOrientation.faceDown {
    print("It's UIDeviceOrientation.faceDown")
    // faceDown
    // Your code here

} else if UIDevice.current.orientation == UIDeviceOrientation.unknown {
    print("It's UIDeviceOrientation.unknown")
    // unknown
    // Your code here

}

So, if you are a seasoned developer, you will immediately solve the problem of identifying if an iPhone or iPad is in landscape or portrait mode, etc.

So that can be done by combining the above two codes & making it work with each other.

if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
    print("It's iPad")
    if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
        print("It's iPad UIDeviceOrientation.landscapeLeft")
        // Landscape
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
        print("It's iPad UIDeviceOrientation.landscapeRight")
        // Landscape
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
        print("It's iPad UIDeviceOrientation.portrait")
        // Portrait
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
        print("It's iPad UIDeviceOrientation.portraitUpsideDown")
        // Portrait
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.faceUp {
        print("It's iPad UIDeviceOrientation.faceUp")
        // faceUp
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.faceDown {
        print("It's iPad UIDeviceOrientation.faceDown")
        // faceDown
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.unknown {
        print("It's iPad UIDeviceOrientation.unknown")
        // unknown
        // Your code here

    }
} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.phone) {
    print("It's iPhone")
    if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
        print("It's iPhone UIDeviceOrientation.landscapeLeft")
        // Landscape
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
        print("It's iPhone UIDeviceOrientation.landscapeRight")
        // Landscape
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
        print("It's iPhone UIDeviceOrientation.portrait")
        // Portrait
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
        print("It's iPhone UIDeviceOrientation.portraitUpsideDown")
        // Portrait
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.faceUp {
        print("It's iPhone UIDeviceOrientation.faceUp")
        // faceUp
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.faceDown {
        print("It's iPhone UIDeviceOrientation.faceDown")
        // faceDown
        // Your code here

    } else if UIDevice.current.orientation == UIDeviceOrientation.unknown {
        print("It's iPhone UIDeviceOrientation.unknown")
        // unknown
        // Your code here

    }
} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.tv) {

} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.unspecified) {
    print("It's Unspecified Device")
    if self.view.frame.size.width < self.view.frame.size.height {
        print("It's Portrait Unspecified Device")
        // Portrait
        // Your code here

    } else {
        print("It's Landscape Unspecified Device")
        // Landscape
        // Your code here

    }
}

Hope it helps,

Thanks & Regards
Mandar Apte

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Leave a Reply