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?

  • coredata - fetch one attribute into an array
  • Delete an object in core data
  • In iOS, how can you programmatically fill out a pdf form field?
  • iPad modal view controller acting in portrait even though it's landscape
  • What is the equivalent of Android's “Spannable” in Objective-C
  • Loading NSData into a UIWebView
  • 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

    2 Solutions Collect From Internet About “swift check if 3D Touch is possible”

    Try this swift code, self is referring to your UIViewController

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

    Yes, if self.traitCollection.forceTouchCapability == UIForceTouchCapability.Available will work, just make sure to wrap it up in a if #available(iOS 9, *)

    So an example for Peek and Pop:

    override func viewDidLoad()
        {
            super.viewDidLoad()
    
            if #available(iOS 9, *) {
                if (traitCollection.forceTouchCapability == .Available) {
                    if let collectionView = self.collectionView {
                        registerForPreviewingWithDelegate(self, sourceView: collectionView)
                    }
                }
            }
        }
    

    Moreover if you have an UITouch object (you can have it from method like touchesBegan, touchesMoved, …) if your device is 3D Touch capable the touch.maximumPossibleForce property will be higher than 0. Don’t forget to wrap it up in a if #available(iOS 9, *) neither 😉

    Feel free checkout this Peek and Pop tutorial for more details, or this sample of 3D Touch code