Here is how to programmatically identify if user is on iPhone or iPad. There is also way to find out if user is on either device and if it is in portrait mode, landscape mode.

We will explore our ways to identify device of user whether it is iPad or iPhone, We will do all this in Swift Programming Language with latest Xcode build.

 
There four ways that iOS device will get identified .phone, .pad, .tv & .unspecified.

 
So code will look like this in swift

 

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 iOS device orientation of iPad & iPhone.

 
There are seven types of iOS device orientations and those are as follows Portrait, PortraitUpsideDown, LandscapeLeft, LandscapeRight, FaceUp, FaceDown, Unknown.

 

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 seasoned developer you will immediately solve how to identify if iPhone or iPad is in landscape or portrait mode etc.

 
So that can be done by combining above two codes & make 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

Leave a Reply

Your email address will not be published. Required fields are marked *