How to make iPhone vibrate using Swift?

I need to make the iPhone vibrate, but I do not know how to do this in Swift. I know that in Objective-C you just write:

import AudioToolbox AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 

But this does not work for me.

+101
ios iphone swift vibration
Oct 19 '14 at 10:00
source share
13 answers

A brief example:

 import UIKit import AudioToolbox class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) } } 

download to your phone and it will vibrate. You can put it in a function or IBAction as you wish.

+205
Oct 19 '14 at 22:51
source share

In iOS 10 on iPhone 7 or 7 Plus, try:

 let generator = UIImpactFeedbackGenerator(style: .heavy) generator.impactOccurred() 
+58
Oct 10 '16 at 11:20
source share

Other types of vibration:

 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) 



More on vibration - http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/

+43
Mar 24 '17 at 3:30
source share

Swift 4.2 Updated

Just paste the code below into your project.

using

 Vibration.success.vibrate() 

Source

 import AVFoundation import UIKit enum Vibration { case error case success case warning case light case medium case heavy case selection case oldSchool func vibrate() { switch self { case .error: let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.error) case .success: let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.success) case .warning: let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.warning) case .light: let generator = UIImpactFeedbackGenerator(style: .light) generator.impactOccurred() case .medium: let generator = UIImpactFeedbackGenerator(style: .medium) generator.impactOccurred() case .heavy: let generator = UIImpactFeedbackGenerator(style: .heavy) generator.impactOccurred() case .selection: let generator = UISelectionFeedbackGenerator() generator.selectionChanged() case .oldSchool: AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) } } } 
+23
Sep 11 '18 at 7:58
source share

For iOS 10. 0+ You can try UIFeedbackGenerator

The simple viewController above, just replace your view controller in your test "single view application"

 import UIKit class ViewController: UIViewController { var i = 0 override func viewDidLoad() { super.viewDidLoad() let btn = UIButton() self.view.addSubview(btn) btn.translatesAutoresizingMaskIntoConstraints = false btn.widthAnchor.constraint(equalToConstant: 160).isActive = true btn.heightAnchor.constraint(equalToConstant: 160).isActive = true btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true btn.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true btn.setTitle("Tap me!", for: .normal) btn.setTitleColor(UIColor.red, for: .normal) btn.addTarget(self, action: #selector(tapped), for: .touchUpInside) } @objc func tapped() { i += 1 print("Running \(i)") switch i { case 1: let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.error) case 2: let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.success) case 3: let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.warning) case 4: let generator = UIImpactFeedbackGenerator(style: .light) generator.impactOccurred() case 5: let generator = UIImpactFeedbackGenerator(style: .medium) generator.impactOccurred() case 6: let generator = UIImpactFeedbackGenerator(style: .heavy) generator.impactOccurred() default: let generator = UISelectionFeedbackGenerator() generator.selectionChanged() i = 0 } } } 
+21
Sep 26 '17 at 8:19 on
source share

We can do it in Xcode7.1

 import UIKit import AudioToolbox class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() AudioServicesPlayAlertSound(kSystemSoundID_Vibrate) } } 
+16
Nov 13 '15 at 12:45
source share

You can vibrate your phone using AudioServices or Haptic Feedback .

 // AudioServices AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) // Haptic Feedback UIImpactFeedbackGenerator(style: .medium).impactOccurred() 

Check out my Haptica open source platform, it supports Haptic Feedback , AudioServices and unique vibration patterns. Powered by Swift 4.2, Xcode 10

+5
Jan 28 '19 at 10:14
source share
 AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) 
+1
Aug 25 '17 at 8:40
source share

UINotificationFeedbackGenerator is available from iOS 10 and works with Haptic v2, we can check this:

  let feedbackSupportLevel = UIDevice.current.value(forKey: "_feedbackSupportLevel") as? Int if #available(iOS 10.0, *), let feedbackSupportLevel = feedbackSupportLevel, feedbackSupportLevel > 1 { do { // 1 let generator = UIImpactFeedbackGenerator(style: .medium) generator.impactOccurred() } do { // or 2 let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.success) } } else { AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) } 
+1
Mar 11 '19 at 8:59
source share
 import AudioToolbox extension UIDevice { static func vibrate() { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) } } 

Now you can just call UIDevice.vibrate() if necessary.

0
Dec 11 '18 at 8:18
source share

Here you will find all codes for each .caf and corresponding category:

https://github.com/TUNER88/iOSSystemSoundsLibrary

For example, if you need a lighter vibration, you can use code 1003.

Good luck and have fun;)

0
Jan 02 '19 at 12:29
source share

Haptica supports both Haptic Feedback , AudioServices and unique vibration patterns.

Powered by Swift 4.2, Xcode 10

0
Jan 20 '19 at 23:12
source share

Swift 4.2

  if #available(iOS 10.0, *) { UIImpactFeedbackGenerator(style: .light).impactOccurred() } 
0
Jul 23 '19 at 10:38
source share



All Articles