UISelectionFeedbackGenerator() not working on iPhone 7
Asked Answered
K

5

7

I would like to use some haptic in my app. I am using the UISelectionFeedbackGenerator but it will not work. I am testing on real iPhone 7 with iOS 10. These two lines should do the magic – but nothing happens:

let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
Katinakatine answered 12/12, 2016 at 9:57 Comment(2)
Did you find any solution for this?Proceeding
I'm in the same boat here. In one of my viewcontrollers, haptic feedback refuses to work. In another, it works even without prepare. I cannot for the life of me understand what's going on.Fogel
F
8

It seems as if AVFoundation (or probably just AVCaptureSession) is somehow messing with the haptic feedback! Without AVFoundation, my haptic feedback works. As soon as I put it in there, it stops working.

Fogel answered 28/9, 2017 at 8:15 Comment(0)
C
5

Here are some alternatives that work on older devices:

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)
Captious answered 24/3, 2017 at 3:28 Comment(0)
S
1

Before you call selectionChanged(), you need to call generator.prepare(). This wakes up the taptic engine. It will stay active for a matter of seconds though, so make sure you prepare() it again if you're going to call selectionChanged() down the road.

let generator = UISelectionFeedbackGenerator()
generator.prepare()
generator.selectionChanged()
Schaal answered 16/12, 2016 at 21:19 Comment(5)
I am sure I've tried prepare(), too. I will give it another try and report. Thanks!Katinakatine
I do not get that to work. Since i am builing an App with target for iOS 9 I'm wrapping these lines in if #available(iOS 10.0, *) { ... }. But also for iOS 10 without the condition it will not work on my iPhone 7. That was also excatly what I was trying before I posted on stackoverflow.Katinakatine
Odd. Do you have an accessibility setting on the phone level that disables taptics? Have you tried using a date picker or moving a table view cell in an Apple stock app to ensure you're feeling their taptics?Schaal
It is more than strange. After I capture an image within my app it works (capturing an image and the haptic button have nothing to do with each other – haptic is not bind on the capture button). When I rebuild the app the haptic feedback do not work until I capture an image again. Are there limitations on haptic feedback when using AVCaptureSession?Katinakatine
@KevinLieser I have problem with haptic feedback and AVFoundation aswell!Fogel
S
1

For those who is struggling with AVFoundation, there is a method starting from iOS13

func setAllowHapticsAndSystemSoundsDuringRecording(_ inValue: Bool) throws

You can enable system sounds and haptics play while recording from audio input.

Reference

Sounder answered 2/2, 2021 at 17:28 Comment(0)
P
0

For who using AVFoundation and doesn't work with haptic, I found a simple way that makes haptic works with AVFoundation simultaneously:

func yourAVRecording() {
    // Haptic
    let generator = UISelectionFeedbackGenerator()
    generator.prepare()
    generator.selectionChanged()

    // Record after played Haptic (just give few time to let haptic done and then magic happen)
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
        // Do your AVFoundation stuffs whatever here
    }
}
Poppas answered 19/7, 2018 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.