ios 9 iphone 6S Play Haptic feedback or vibrate

Khant Thu Linn picture Khant Thu Linn · Sep 29, 2015 · Viewed 9k times · Source

After user does force touch, I want to vibrate phone just like default behaviour.

Is it haptic? If so, how shall I do?

Answer

Mikita Manko picture Mikita Manko · Feb 5, 2017

Example in Swift (for iPhone 6S)

import AudioToolbox

AudioServicesPlaySystemSound(1519) // Actuate `Peek` feedback (weak boom)
AudioServicesPlaySystemSound(1520) // Actuate `Pop` feedback (strong boom)
AudioServicesPlaySystemSound(1521) // Actuate `Nope` feedback (series of three weak booms)

Just in case - here're examples for iPhone 7/7+.

As for the force touch - you need to detect if it's available first:

func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool {
    return traitCollection.forceTouchCapability == UIForceTouchCapability.available
}

and then in touch events, it will be available as touch.force

func touchMoved(touch: UITouch, toPoint pos: CGPoint) {
    let location = touch.location(in: self)
    let node = self.atPoint(location)

    //...
    if is3dTouchEnabled {
        bubble.setPressure(pressurePercent: touch.force / touch.maximumPossibleForce)
    } else {
        // ...
    }
}

Here's my blog with a more detailed example with code samples:
http://www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/