Search code examples
iosswiftiphonehaptic-feedback

No haptic feedback when testing on a actual iPhone


I followed Apple's guide regarding implementing haptic features to my app. I tested on my iPhone but when I trigger the haptic action (tap a button on a tableView controller), nothing happens. Thie is my implementation:

if supportHaptics {//line 95
            do {
                let player = try hapticsEngine.makePlayer(with: hapticPattern)
                hapticsEngine.start(completionHandler:nil)
                try player.start(atTime: 0)//line 99
                hapticsEngine.stop(completionHandler: nil)
            } catch {
                print(error)
            }
}

I am pretty sure my hapticsEngine and hapticPattern are valid and there is no error, because if I set a break at line 95 and walk through the do block line by line, my phone DOES vibrate after line 99. It's just that after I removed the breakpoint and run the app, my iPhone doesn't send any haptic feedback.

What did I miss? Anything would help.


Solution

  • Ok I sorted out. Do not use CHHapticsEngine to accomplish the tap feedback. Instead, using the two lines below can easily trigger a tap (From UIKit).

    let feedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
    feedbackGenerator.impactOccurred()