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()
Kevin Lieser
  • 951
  • 1
  • 9
  • 25
  • Did you find any solution for this? – shelll Sep 20 '17 at 12:44
  • 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. – ullstrm Sep 28 '17 at 08:09

5 Answers5

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.

ullstrm
  • 9,812
  • 7
  • 52
  • 83
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)
Michael
  • 9,639
  • 3
  • 64
  • 69
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()
Canucklesandwich
  • 693
  • 6
  • 15
  • I am sure I've tried prepare(), too. I will give it another try and report. Thanks! – Kevin Lieser Dec 16 '16 at 21:55
  • 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. – Kevin Lieser Dec 19 '16 at 08:02
  • 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? – Canucklesandwich Dec 20 '16 at 14:04
  • 1
    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`? – Kevin Lieser Dec 20 '16 at 14:12
  • 2
    @KevinLieser I have problem with haptic feedback and AVFoundation aswell! – ullstrm Sep 28 '17 at 08:13
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

Neil Galiaskarov
  • 5,015
  • 2
  • 27
  • 46
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
    }
}
KTang
  • 340
  • 1
  • 17