IOS 中是否有用于自定义振动的 API?

从 iOS5开始,用户可以为警报和戒指创建自定义振动模式。下面的屏幕截图显示了创建一个联系人的用户界面(iOS6中的联系人应用程序) :

Screenshot of UI for creating custom vibrations in iOS 6

我一直在搜索,包括文档,我找不到任何公共 API,暴露的创建或自定义振动播放。最接近的方法是使用 AudioToolbox框架来发挥短暂的、持续的振动:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

有人知道是否有自定义振动 API?它不一定是公共 API。我很好奇联系人应用程序使用什么。有人知道吗?

有人建议在 CoreTelephony(例子)中使用 _CTServerConnectionCreate,我试过了,但是由于某些原因没有振动。

2015年10月更新:

Ios9中有新的私有 API,可以在兼容设备上与 Taptic Engine 互动。有关更多信息,请参见 IOS9中的 Taptic

51731 次浏览

有一个叫做 HapticKeyboard 的项目可以做到这一点

具体来说,看看最后一组函数

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

有两个问题:

  1. 我怀疑你的应用程序能不能进入应用程序商店。苹果会认为这是用户的电池消耗,他们对用户体验非常严格
  2. 这可能需要越狱。自从我上次玩这个以来,已经有好几个新的 iOS 版本了,所以很难说

经过几个小时的挖掘联系应用程序,我已经弄清楚它是如何工作的。

ABNewPersonViewControl 调用 ToneLibrary 框架中的某个类来执行此操作。

调用堆栈如下所示:

0   CoreFoundation                  0x3359a1d4 CFGetTypeID + 0
1   CoreFoundation                  0x33596396 __CFPropertyListIsValidAux + 46
2   CoreFoundation                  0x33517090 CFPropertyListCreateData + 124
3   AudioToolbox                    0x38ac255a AudioServicesPlaySystemSoundWithVibration + 158
5   ToneLibrary                     0x35a7811a -[TLVibrationRecorderView vibrationComponentDidStartForVibrationRecorderTouchSurface:] + 38
6   ToneLibrary                     0x35a772b2 -[TLVibrationRecorderTouchSurface touchesBegan:withEvent:] + 342
7   UIKit                           0x3610f526 -[UIWindow _sendTouchesForEvent:] + 314
8   UIKit                           0x360fc804 -[UIApplication sendEvent:] + 376

在网上搜索“ AudioServicesPlaySystemSoundWith振动”后,我什么也没找到。

所以我决定自己研究一下,它是 AudioToolbox 框架中的一个私有函数。

函数的声明类似于

void AudioServicesPlaySystemSoundWithVibration(SystemSoundID inSystemSoundID,id arg,NSDictionary* vibratePattern)

“ inSystemSoundID”是 SystemSoundID。就像“ AudioServicesPlaySystemSound”一样,传递“ kSystemSoundID _ Vibrate”。

“ arg”不重要,传递0给它,一切都会正常工作。

“振动模式”是一个指针的“ NSDictionary”,联系应用程序传入 { 强度 = 1; 持续时间 = 1; 持续时间 = 10; }用于记录用户输入。

但只有调用这个函数才能使振动永不停止。所以我必须找到一些功能来阻止它。

答案是“ AudioServicesStopSystemSound”,它也是 AudioToolbox 框架中的一个私有函数。

函数的声明类似于

void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID)

我猜联系应用程序在 touch esBegan 方法中使用 AudioServicesPlaySystemSoundWith 振动,在 touch End 方法中使用 AudioServicesStopSystemSound 来达到这个效果。

TLVibrationController 将管理一个振动模式对象来记录您输入的过程。

最后,它生成一个字典,传递到 AudioServicesPlaySystemSoundWith 振动来重播整个过程,如下所示:

NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* arr = [NSMutableArray array ];


[arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms
[arr addObject:[NSNumber numberWithInt:2000]];


[arr addObject:[NSNumber numberWithBool:NO]];  //stop for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];


[arr addObject:[NSNumber numberWithBool:YES]];  //vibrate for 1000ms
[arr addObject:[NSNumber numberWithInt:1000]];


[arr addObject:[NSNumber numberWithBool:NO]];    //stop for 500ms
[arr addObject:[NSNumber numberWithInt:500]];


[dict setObject:arr forKey:@"VibePattern"];
[dict setObject:[NSNumber numberWithInt:1] forKey:@"Intensity"];




AudioServicesPlaySystemSoundWithVibration(4095,nil,dict);

因此,如果你想在 iOS 中自定义振动,请使用 AudioServicesPlaySystemSoundWith 振动和 AudioServicesStopSystemSound。