使って創ってApp

ソフトウェアを使ったり作ったりするブログです

iPhoneかiPadかを判定する iOSアプリ開発

iOSアプリ開発で、現在使用しているデバイスが iPhoneiPad なのかを判定します。

開発環境

判定方法

現在使用しているデバイスの情報を取得するには、UIDevice クラスを使います。UIDevice クラスのドキュメントを見てみると、このクラスには userInterfaceIdiom プロパティと UIUserInterfaceIdiom enum が定義されています。説明を読んでみるとそれっぽいことが書かれているので、この二つを使います。

userInterfaceIdiom
For universal applications, you can use this property to tailor the behavior of your application for a specific type of device.
UIUserInterfaceIdiom
The type of interface that should be used on the current device.

UIDevice Class Reference より

判定方法のコード

使用しているデバイスが iPad かを判定するコードです。

if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
 // some code here
}