🗂

iPhone/iPad判別とiPadの向き判定

2021/06/12に公開

-iPhoneとiPadを判別
-iPadの場合はPortrait/Landscapeどちらか判定する。

if UIDevice.current.userInterfaceIdiom == .phone {
    // for iPhone
}
if UIDevice.current.userInterfaceIdiom == .pad {
    // for iPad
    let deviceOrientation: Int? = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.rawValue
    switch deviceOrientation {
        case 1, 2:
            // 1:Portrait, 2:Portrait Upside Down
            debugPrint("interfaceOrientation: Portrait")
        case 3, 4:
            // 3:Landscape Left, 4:Landscape Right
            debugPrint("interfaceOrientation: Landscape")
        default:
            debugPrint("unknown")
    }
}

Discussion