Search code examples
iosuibuttoniphone-vibrate

How to disable UIButton vibration


Up until today I have been testing my apps in the Simulator. For the first time I tested a project on a real iPhone. I was unpleasantly surprised that every time I press a button the phone vibrates. This wouldn't be a big deal except that my particular app has a built in keyboard that plays an audio sound every time a key (UIButton) is pressed.

enter image description here

The combination of the vibration sound plus the audio sound is very distracting. Is there any way to programmatically disable button vibration? I'm not asking how to disable all vibration for the phone, just the buttons I choose. The only relevant question I could find was just the opposite: Making the iPhone vibrate.

I don't have to leave it up to the user to turn vibration off in settings, do I?

Update 1:

Judging from the comments, maybe this is either a device specific problem or I am somehow triggering a vibration.

This is how I play a sound:

import Foundation
import AudioToolbox

class Player {
    
    private let audioFolder = "raw"
    
    func playSoundFromFile(file: String) {
        var soundURL: NSURL?
        var soundID: SystemSoundID = 0
        let filePath = NSBundle.mainBundle().pathForResource("\(audioFolder)/\(file)", ofType: "mp3")
        soundURL = NSURL(fileURLWithPath: filePath!)
        if let url = soundURL {
            AudioServicesCreateSystemSoundID(url, &soundID)
            AudioServicesPlayAlertSound(soundID)
        }
    }
}

And this is my button tap action:

@IBAction func keyTapped(sender: UIButton) {
    
    let buttonText = sender.titleLabel?.text ?? ""
    
    updateDisplayForIpa(buttonText)
    
    // play sound
    if let fileName = singleSound.fileNameForIpa(buttonText) { // converts IPA sound symbol into a filename 
        player.playSoundFromFile(fileName)
    }
    
}

Since the tap calls an update display method, here that is:

func updateDisplayForIpa(ipa: String) {
    
    if let fileName = singleSound.fileNameForIpa(ipa) {
        
        ipaLabel.text = ipa // UILabel
        ipaDescription.text = "\(fileName)_description".localized // UITextView
        ipaDescription.scrollRangeToVisible(NSRange(location: 0, length: 0))
        example1.setTitle("\(fileName)_example1".localized, forState: UIControlState.Normal) // UIButton
        example2.setTitle("\(fileName)_example2".localized, forState: UIControlState.Normal) // UIButton
        example3.setTitle("\(fileName)_example3".localized, forState: UIControlState.Normal) // UIButton
        
    }
}

I don't see anything here that would trigger a vibration...

Update 2

I created a new project and added a single UIButton to the storyboard. I tested it on the same phone that vibrates in the above app. There was no vibration when I tapped the button, so there must be something about the above code that is triggering a vibration. But what could it be?


Solution

  • AudioServicesPlayAlertSound can cause a vibration:

    iPhone—plays the specified sound. If the user has configured the Settings application for vibration on ring, also invokes vibration.

    I'd try AudioServicesPlaySystemSound instead:

    To play a short sound not used as an alert, use AudioServicesPlaySystemSound.