在iOS 7上使用AVSpeechSynthesizer但保留iOS 6的兼容性

Kyr*_*das 4 iphone xcode objective-c ios ios7

你好我理解TTS仅在iOS 7中可用,但我过去使用了很多apis,检查该类是否可用并设法保留以前版本的兼容性,但是使用AVSpeechSynthesizer它似乎不起作用,请你帮我使用适用于iOS 7的TTS并通过在iOS 6中禁用它来保持兼容性,非常感谢.

这是我的代码,但它似乎不起作用

    if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)) {
    if([AVSpeechSynthesizer class]) {
        AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text];
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
        utterance.rate = AVSpeechUtteranceDefaultSpeechRate/2;
        utterance.pitchMultiplier = 0.9;
        utterance.preUtteranceDelay = 0;
        utterance.postUtteranceDelay = 0;
        [synth speakUtterance:utterance];
    } else {
        // Feature not available, do something else
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经在我的项目中链接了avfoundation并设置了部署目标iOS 6,它似乎仅在iOS 7设备上运行时工作,如果它的iOS 6崩溃了.

这是我得到的错误消息

dyld: Symbol not found: _AVSpeechUtteranceDefaultSpeechRate
Run Code Online (Sandbox Code Playgroud)

Jos*_*own 6

框架需要弱联系.请执行下列操作:

  1. 单击Project Navigator(左侧)中的项目文件,在编辑器中打开项目
  2. 单击编辑器中的Build Phases选项卡
  3. 展开Link Binary With Libraries部分
  4. AVFoundation.frameworkRequired设置为Optional

将AVFoundation.framework从Required设置为Optional

此外,您可能希望使用更安全(并由Apple推荐)更新版本检查.

  • @TonyMkenu正如我所看到的,Stack Overflow的观点是有人来找他们问题的答案.如果没有问题的实际答案,只有评论,有人可能会错过这个问题的解决方案. (2认同)