Making the Iphone Vibrate

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 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

How do you make the iPhone vibrate for arbitrary durations?

Yes, this is something that has caused AppStore rejections in the past, and probably will again...which means it is still possible to do it.

Answering my own question, here's how to do it:

Add framework CoreTelephony in Build Phases.

declare:

extern void * _CTServerConnectionCreate(CFAllocatorRef, int (*)(void *, CFStringRef, CFDictionaryRef, void *), int *);
extern int _CTServerConnectionSetVibratorState(int *, void *, int, int, float, float, float);

static void* connection = nil;
static int x = 0;

initialize:

connection = _CTServerConnectionCreate(kCFAllocatorDefault, &vibratecallback, &x);

start vibration:

_CTServerConnectionSetVibratorState(&x, connection, 3, intensity, 0, 0, 0);

stop vibration:

_CTServerConnectionSetVibratorState(&x, connection, 0, 0, 0, 0, 0);

This code is from HapticKeyboard, a downloadable application that buzzes the phone as you type. It is available for jailbroken phones on Cydia. See also my jailbreaking experience)

Any other good references?

Programmatically make the iPhone vibrate

You can use

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

Note that you need to add the AudioToolbox framework and import the following header file:

#import <AudioToolbox/AudioServices.h>

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)) { }

}

}

}
}

how to run vibrate continuously in iphone?

There are numerous examples that show how to do this with a private CoreTelephony call: _CTServerConnectionSetVibratorState, but it's really not a sensible course of action since your app will get rejected for abusing the vibrate feature like that. Just don't do it.

Vibrate one time Taptic

  1. You can't test vibration on a simulator, Need to use a real device for checking vibrations.

  2. for soft vibration, you can use UISelectionFeedbackGenerator and can also use UIImpactFeedbackGenerator with soft style like this:

// UISelectionFeedbackGenerator

let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()

// UIImpactFeedbackGenerator

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


Related Topics



Leave a reply



Submit