How to make iPhone vibrate using Swift?
Short example:
import UIKit
import AudioToolbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
}
load onto your phone and it will vibrate. You can put it in a function or IBAction as you wish.
Code Update:
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }
As apple code docs written:
This function will be deprecated in a future release. Use
AudioServicesPlaySystemSoundWithCompletion instead.
NOTE: If vibrate doesn't work. check vibrate is enable in sounds and haptics settings
Swift : Make a very short vibration
If you have the right kind of iPhone and you're willing to confine yourself to iOS 13 and later, you can use Core Haptics to create any kind of vibration you like.
SwiftUI: How to make phone vibrate?
This should help a little I think. Try it out! Lmk if it works!
Source Code
import SwiftUI
import AudioToolbox
struct ContentView: View {
var body: some View {
VStack{
Button("Press"){
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { }
}
}
}
}
Loop vibration in Swift
Not sure why this isn't working for you--the following sample works for me on my test device (iPhone X, iOS 11.2).
The ViewController sample below includes outlets for playing single sounds, playing single vibrations, looping sounds and looping vibrations. The "Chirp" sound is a 1s wav file.
Note that since this is a sample, the code below doesn't dispose of the system sounds, nor invalidate timers if the ViewController is hidden. Make sure to manage your system resources appropriately if you adapt this.
//
// ViewController.swift
//
import UIKit
import AudioToolbox
class ViewController: UIViewController {
static let soundId: SystemSoundID? = {
guard let soundURL = Bundle.main.url(forResource: "Chirp", withExtension: "wav") else {
return nil
}
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundId)
return soundId
}()
static let timerInterval: TimeInterval = 2
var soundTimer: Timer?
var vibrationTimer: Timer?
var playCount = 0 {
didSet {
playCountLabel.text = String(playCount)
}
}
func invalidateTimers() {
if let vibrationTimer = vibrationTimer {
vibrationTimer.invalidate()
}
if let soundTimer = soundTimer {
soundTimer.invalidate()
}
}
@IBOutlet weak var playCountLabel: UILabel!
@IBAction func playSound(_ sender: Any) {
if let soundId = ViewController.soundId {
AudioServicesPlaySystemSound(soundId)
playCount += 1
}
}
@IBAction func loopSound(_ sender: Any) {
invalidateTimers()
soundTimer = Timer.scheduledTimer(timeInterval: ViewController.timerInterval, target: self, selector: #selector(playSound(_:)), userInfo: nil, repeats: true)
}
@IBAction func vibrate(_ sender: Any) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
playCount += 1
}
@IBAction func loopVibrate(_ sender: Any) {
invalidateTimers()
soundTimer = Timer.scheduledTimer(timeInterval: ViewController.timerInterval, target: self, selector: #selector(vibrate(_:)), userInfo: nil, repeats: true)
}
@IBAction func cancelAll(_ sender: Any) {
invalidateTimers()
}
}
Making the iPhone vibrate
From "iPhone Tutorial: Better way to check capabilities of iOS devices":
There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate
:
1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Both of the functions vibrate the iPhone. But, when you use the first
function on devices that don’t support vibration, it plays a beep
sound. The second function, on the other hand, does nothing on
unsupported devices. So if you are going to vibrate the device
continuously, as an alert, common sense says, use function 2.
First, add the AudioToolbox framework AudioToolbox.framework
to your target in Build Phases.
Then, import this header file:
#import <AudioToolbox/AudioServices.h>
How to make my iphone vibrate twice when I click on a button?
#import <AudioToolbox/AudioServices.h>
AudioServicesPlayAlertSound(UInt32(kSystemSoundID_Vibrate))
This is the swift function...See this article for detailed description.
Related Topics
Swiftui Holding Reference to Deleted Core Data Object Causing Crash
How to Know That If the Only Visible Area of a .Png Is Touched in Xcode
Itsappusesnonexemptencryption Export Compliance While Internal Testing
Paging Uiscrollview in Increments Smaller Than Frame Size
How to Hide Tab Bar with Animation in iOS
Swift Merge Audio and Video Files into One Video
Missing Push Notification Entitlement Warning
Broken Uisearchbar Animation Embedded in Navigationitem
Google Maps iOS Sdk, Getting Current Location of User
Auto Adjust Custom Uitableviewcell and Label in It to the Text
Format Uitextfield Text Without Having Cursor Move to the End
Decode Base-64 Encoded Png in an Nsstring
Dashed Line Border Around Uiview
Xcode 8/Swift 3: "Expression of Type Uiviewcontroller? Is Unused" Warning
Make Custom Button on Tab Bar Rounded
Uploading Archive Error: "Missing iOS Distribution Signing Identity for ..."
How to Set Imageview in Circle Like Imagecontacts in Swift Correctly