Search code examples
iosswift3dtouch

swift check if 3D Touch is possible


In my app I will show settings, which only should be visible, if the device can 3D Touch. at the moment I check, if the device has ios9.

if #available(iOS 9.0, *)

problem is, for example the iPhone 6 have iOS 9 but no 3D Touch. how can I check the possibility of 3D Touch with swift?

i found this post, but no swift solution: Check if 3D touch is supported and enabled on the iOS9 device

One more Question. now i want to check if iOS 9 AND 3D Touch is available. i try this:

if (#available(iOS 9.0, *)) AND (is3DTouchAvailable() == true) {

But i always get this error:

Expected '{' after 'if' condition


Solution

  • Try this swift code, self is referring to your UIViewController

    func is3DTouchAvailable() -> Bool
    {
        return self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available
    }