3

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61

1 Answers1

6

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.

Cheng-Yu Hsu
  • 1,029
  • 7
  • 11