Search code examples
iosswiftforce-touch

3d Touch Not Available on iPhone 6s+


I am trying to add 3d touch into my application. I am trying to enable the user to use force touch on a UITableViewCell, like shown in this project. I am running this code:

if traitCollection.forceTouchCapability == UIForceTouchCapability.Available {
    registerForPreviewingWithDelegate(self, sourceView: view)
} else {
    print("unavailable")
}

I have tried running it on an iPhone 6s+, but I receive "unavailable" in my logs. Why can I not use 3d touch on a 3d touch-supported device? Thanks for your help.


Solution

  • Just found some reference about 3D Touch APIs: according to the 3D Touch APIs documentation, Apple suggested that developers to read forceTouchCapability in traitCollectionDidChange: delegate method.

    For example, you may check the value in your view controller using:

    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        switch traitCollection.forceTouchCapability {
        case .Available:
            print("Available")
        case .Unavailable:
            print("Unavailable")
        case .Unknown:
            print("Unknown")
        }
    }
    

    Hope it helps.