SpeechFramework 是 iOS 10 提供的語音識別框架,可讓 App 直接接入系統語音能力。

需在 Info.plist 加入 Privacy - Speech Recognition Usage Description,並呼叫:
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
// 處理授權結果
}];
SFSpeechRecognizer *recognizer = [[SFSpeechRecognizer alloc] init];
SFSpeechRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:audioURL];
[recognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
NSLog(@"%@", result.bestTranscription.formattedString);
}];
SFSpeechRecognitionResult 會提供多組轉寫結果,bestTranscription 是可信度較高的一組;SFTranscriptionSegment 則可描述單個音訊片段的時間與可信度。
若使用模擬器測試,可能會遇到限制,建議以真機驗證。