Home > front end >  No haptic feedback when testing on a actual iPhone
No haptic feedback when testing on a actual iPhone

Time:11-05

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.

CodePudding user response:

You use start with a completion handler parameter, that means the haptic engine will work asynchronously ie work in sometime in the future and then stop it. In debug step by step it works because the haptic engine has enough time to execute. If you want synchronous (that is start immediately)you must call start() (= no parameter which is different than with parameter = nil) From Apple doc :

Call the haptic engine’s start(completionHandler:) for an asynchronous start, or start() to start the engine synchronously (immediately).

CodePudding user response:

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()
  • Related