1. AudioToolbox
-
Target > Linked Frameworks and Libraries > Add AudioToolbox
-
import AudioToolbox -
AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil)
It is that simple using AudioServicesPlaySystemSound API.
2. Haptic API
In iOS 10, there is a new API, making use of the new haptic engine in iPhone.
The API is very simple, with 3 concrete classes to UIFeedbackGenerator. Use accordingly to your scenario.
// UI "impact"
UIImpactFeedbackGenerator(style: .light).impactOccurred()
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
// Selection changed
UISelectionFeedbackGenerator().selectionChanged()
// Notifications
UINotificationFeedbackGenerator().notificationOccurred(.success)
UINotificationFeedbackGenerator().notificationOccurred(.error)
UINotificationFeedbackGenerator().notificationOccurred(.warning)
// To be complete, this is the vibration using AudioToolbox
AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil)
Pitfall: Does not work along with other audio session
There is a note:
However, the device does not vibrate if your app’s audio session is configured with the AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord audio session category.
While the note is for AudioServicesPlayAlertSound, it is applicable to AudioServicesPlaySystemSound too.
As long as you have a audio session (via AVAudioPlayer, AVCaptureMovieFileOutput, etc), then the phone will NOT vibrate.