How to make my iphone vibrate twice when I click on a button?

sunstan picture sunstan · Apr 24, 2015 · Viewed 12.7k times · Source

I search to make vibrate twice my iphone when I click on a button (like a sms alert vibration)

With AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) I obtain just one normal vibration but I want two shorts :/.

Answer

Adrian picture Adrian · Oct 25, 2016

Update for iOS 10

With iOS 10, there are a few new ways to do this with minimal code.

Method 1 - UIImpactFeedbackGenerator:

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

Method 2 - UINotificationFeedbackGenerator:

let feedbackGenerator = UINotificationFeedbackGenerator()
feedbackGenerator.notificationOccurred(.error)

Method 3 - UISelectionFeedbackGenerator:

let feedbackGenerator = UISelectionFeedbackGenerator()
feedbackGenerator.selectionChanged()