Search code examples
iosswiftcrash

UI_USER_INTERFACE_IDIOM() crashes app distributed on devices ONLY


I created a sample single page swift language based iOS app that CRASHES on this func call in viewDidLoad() -

func regularFont() -> UIFont {
    var fontSize : CGFloat = (UI_USER_INTERFACE_IDIOM() == .Pad) ? 15 : 12
    return UIFont.systemFontOfSize(fontSize)
}

But once I replace this UI_USER_INTERFACE_IDIOM() with Apple recommended UIDevice() method, it WORKS fine.

func regularFont() -> UIFont {
    var fontSize : CGFloat = (UIDevice().userInterfaceIdiom == .Pad) ? 15 : 12
    return UIFont.systemFontOfSize(fontSize)
}

The crash occurs on all devices I tested - iPhone 5s, iPhone 6 and iPad Air (all on iOS 8.x) and it happens ONLY on devices, NOT simulator.

PS: The app is installed on all devices through our OTA/web link.

To my surprise, we have another objective c language based app on Apple's App Store that uses UI_USER_INTERFACE_IDIOM() heavily, regularly updated... but never crashed due to this.

Any thoughts?


Solution

  • I got it (partially!). Actually "release" implementation of UI_USER_INTERFACE_IDIOM() in swift project crashes the app.

    Once I edit the scheme to "release" (Xcode > Product > Scheme > Edit Scheme > Run > Build Configuration change to "Release") and then run on simulator/device, the app crashes everywhere.. all devices/simulators/developer/distribution profiles.

    However, still I have no clue why our app store app (objective c language based) does NOT crash.

    My only guess is that it's a glitch in UI_USER_INTERFACE_IDIOM() API implementation with some language specific coding (swift vs objective c) by Apple.

    Anyways, I would replace all UI_USER_INTERFACE_IDIOM() with UIDevice(). userInterfaceIdiom. I hope this helps someone!