ios - 将 iOS 7 代码添加到 iOS 6 项目

标签 ios objective-c

我知道 TTS 仅在 iOS 7 中可用在投反对票之前,请仔细阅读。

我的一位客户希望将文本转语音功能添加到基于 iOS 6 的现有项目中。我知道在 iOS 7 中我们内置了 TTS,所以我想使用它。

当项目基于 iOS 7 时,以下代码可以正常工作。

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"];
[synthesizer speakUtterance:utterance];

但我无法将此代码添加到基于 iOS 6 的现有项目中。

我正在使用这个 thread方式。我错过了什么吗?或者这个内置的 TTS 是不同的东西?

最佳答案

由于您尝试运行无法在 iOS 6 上运行的代码,因此您需要进行运行时检查以确保您可以运行您尝试运行的代码。一个快速的方法是检查类是否可用:

if([AVSpeechSynthesizer class]) {
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"];
    [synthesizer speakUtterance:utterance];
} else {
    // Feature not available, do something else
}

无论代码是否编译,您都需要执行此操作。

如果您的代码甚至没有正确编译,那么您可以检查以下几点:

  • 您的基础 SDK 是否设置为 iOS 6.0?
  • AVSpeechSynthesizer 是 AVFoundation 的一部分 - 您是否将您的项目链接到它?

关于ios - 将 iOS 7 代码添加到 iOS 6 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20519586/

相关文章:

Objective-C:以相反顺序对数组进行 block 枚举?

objective-c - Bool 不可转换为 ObjCBool

ios - AVAudioSession 重新激活问题

ios - 更快地删除 subview

iphone - 如何在 Xcode 的框架中进行调试?

iphone - 选择 UITableView 行时添加 subview ?

ios - SpriteKit - 生成随机节点

iOS:无法编译连接:Cell 中的 UIImageView

ios - 自定义 NSError 代码

ios - 搜索时禁用 segue 上的导航栏