iPhone6S iOS14.3下UINotificationFeedbackGenerator等触觉API无响应,是否有人遇到同款问题?
问题原因及解决方案
这是旧款iPhone搭配特定iOS版本的常见兼容性问题,可按以下顺序排查修复:
1. 校验系统配置
- 首先确认iPhone 6S设备的「设置 - 声音与触感」中系统触感反馈开关处于开启状态:该开关仅控制
UINotificationFeedbackGenerator/UIImpactFeedbackGenerator系列接口的触发逻辑,AudioServicesPlayAlertSound调用的全局震动不受该开关限制,这也是两类接口表现不一致的最常见原因 - 确认设备未开启低功耗模式:低功耗模式下系统会默认停用非关键的触觉反馈能力
2. 优化代码调用逻辑
当前代码在触发震动时才临时初始化UINotificationFeedbackGenerator实例,不符合官方推荐的调用规范:Generator初始化后需要短暂时间准备硬件,旧设备性能不足很容易导致首次/偶发调用丢失,可按如下方式修改代码:
import SwiftUI import AVFoundation struct ContentView: View { // 提前初始化Generator实例 private let notificationGenerator = UINotificationFeedbackGenerator() func simpleSuccess() { // 调用前可再次调用prepare()减少延迟 notificationGenerator.prepare() notificationGenerator.notificationOccurred(.error) } func otherVibration() { print("#") AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) } var body: some View { VStack { Text("UINotificationFeedbackGenerator") .onTapGesture(perform: simpleSuccess) .padding(.bottom, 60) Text("AudioServicesPlayAlertSound") .onTapGesture(perform: otherVibration) } .onAppear { // 页面加载时提前准备Generator notificationGenerator.prepare() } } }
3. 系统Bug兼容方案
如果上述排查都做完仍然无法生效,则属于iOS 14.x在搭载第一代Taptic Engine的旧设备(iPhone 6S/6SP/7/7P)上的已知系统Bug,目前无系统层面的修复方案,可添加设备/系统版本判断逻辑:在对应设备和系统版本下,直接降级使用AudioServicesPlayAlertSound接口实现震动效果即可。
内容的提问来源于stack exchange,提问作者Luc-Olivier
相关产品推荐
相关产品推荐

