Use AVSpeechSynthesizer on iOS 7 but retain compatibility for iOS 6

我的梦境 提交于 2020-01-02 06:42:01

问题


Hello I Understand TTS is only available in iOS 7 but i used many apis in the past by checking if the class is available and managed to retain compatibility on previous versions but with AVSpeechSynthesizer it doesn't seem to work, can you please help me use TTS for iOS 7 and retain compatibility by disabling it in iOS 6, thank you very much.

Here is my code but it doesn't seem to work

    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
    }
}

i've already linked avfoundation in my project and set deployment target iOS 6 and it seems to work only when runing on iOS 7 devices if its iOS 6 it crashes.

here is the error message i get

dyld: Symbol not found: _AVSpeechUtteranceDefaultSpeechRate

回答1:


The framework needs to be be weak linked. Do the following:

  1. Click your project file in the Project Navigator (on the left) to open your project in the editor
  2. Click the Build Phases tab in the editor
  3. Expand the Link Binary With Libraries section
  4. Set AVFoundation.framework from Required to Optional

Additionally, you might want to update your version check with one that's safer (and recommended by Apple).



来源:https://stackoverflow.com/questions/21102500/use-avspeechsynthesizer-on-ios-7-but-retain-compatibility-for-ios-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!