是否有在iOS自定义振动的API?

从iOS 5开始,用户可以创build警报和响铃的自定义振动模式。 以下屏幕截图显示了用于创build一个(iOS 6中的联系人应用程序)的UI:

用于在iOS 6中创建自定义振动的UI屏幕截图

我一直在search,包括文档,我找不到公开的自定义振动的创build或播放的公共API。 最接近的是使用AudioToolbox框架来发挥短暂的,不断的振动:

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

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

PS其他人build议在CoreTelephony中使用CoreTelephony ( 示例 )。 我试过了,但由于某种原因无法得到任何振动。

2015年10月更新:

iOS 9中有新的私有API与兼容设备中的Taptic Engine进行交互。 有关更多信息,请参阅iOS 9中的Taptic 。

在联系人应用程序中挖掘小时之后,我已经知道它是如何工作的。

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

在网上search“AudioServicesPlaySystemSoundWithVibration”后,我什么也没find。

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

函数的声明就像

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

“inSystemSoundID”是SystemSoundID。就像“AudioServicesPlaySystemSound”一样,传递“kSystemSoundID_Vibrate”。

“arg”并不重要,通过零,一切都会正常工作。

“vibratePattern”是“NSDictionary”的指针,联系人应用程序通过{Intensity = 1; OffDuration = 1; OnDuration = 10; }用于logging用户input。

但是只有调用这个函数才会使振动永不停歇。 所以我必须find一些function来阻止它。

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

函数的声明就像

 void AudioServicesStopSystemSound(SystemSoundID inSystemSoundID) 

我猜联系应用程序在touchesBegan方法中使用AudioServicesPlaySystemSoundWithVibration,并在touchEnd方法中使用AudioServicesStopSystemSound来达到此效果。

TLVibrationController将pipe理一个振动模式对象来logging你input的过程。

最后它生成一个字典传入AudioServicesPlaySystemSoundWithVibration重播整个过程如下:

 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的自定义振动。 使用AudioServicesPlaySystemSoundWithVibration和AudioServicesStopSystemSound。

有一个名为HapticKeyboard的项目可以做到这一点。 请参阅: http : //code.google.com/p/zataangstuff/source/browse/trunk/HapticKeyboard/Classes/HapticKeyboard.m?r=93

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

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

有两个问题:

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