LoginSignup
32
28

More than 5 years have passed since last update.

SwiftでiPhone、iPadなどのデバイス判定をする方法

Last updated at Posted at 2016-12-06

iPhone、iPadなどのデバイス判定をしたいときはこんな感じでやれば判定できます。

Swift
if UIDevice.current.userInterfaceIdiom == .phone {
   // 使用デバイスがiPhoneの場合

} else if UIDevice.current.userInterfaceIdiom == .pad {
   // 使用デバイスがiPadの場合

}

ちなみに、Switch文で書くとこのような分岐になります。
iPhone、iPad以外の分岐もあわせて書いてみます。

Swift
switch UIDevice.current.userInterfaceIdiom {
case .phone:
    // iPhone、iPod touchの場合

case .pad:
    // iPad端末の場合

case .tv:
    // Apple TVの場合

case .carPlay:
    // CarPlayの場合

case .unspecified:
    // 上記以外の端末の場合
}

iOSシミュレーターを使用しても、この分岐を判定してくれるので便利ですね。

CarPlay用のアプリを作ることはなかなかないかとは思いますが、iPhone、iPad以外のデバイスで作成するときにはデバイス判定が役立つときがあるかもしれませんね。

参考:UIUserInterfaceIdiom - UIKit | Apple Developer Documentation

2018/07/30 追記・修正:全体的に書き換え

32
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
28