Implement iOS SFSpeechRecognizer platform bridge
epic-speech-to-text-input-foundation-task-007 — Implement the iOS-specific NativeSpeechApiBridge using Flutter MethodChannel to invoke SFSpeechRecognizer via native Swift plugin code. Handle: NSMicrophoneUsageDescription and NSSpeechRecognitionUsageDescription permission requests, AVAudioSession configuration, real-time partial result streaming via EventChannel, recognition task lifecycle (start/stop/cancel), and locale selection for Norwegian (nb-NO).
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Structure the Swift code as a FlutterPlugin in ios/Classes/SpeechPlugin.swift. Use a single MethodChannel ('com.eircodex.speech/methods') for requestPermission, isAvailable, startRecognition, stopRecognition commands, and a separate EventChannel ('com.eircodex.speech/events') for streaming partial and final results as JSON-encoded maps {type: 'partial'|'final'|'error', text: String?, errorCode: String?}. Ensure AVAudioEngine is stopped before calling recognitionTask.cancel() — reversing this order causes an AVAudioSession error on some iOS versions. Use DispatchQueue.main.async for all Flutter channel calls to avoid threading violations.
For nb-NO locale fallback: check SFSpeechRecognizer.supportedLocales() and log a warning if nb-NO is absent. Handle the case where recognition task produces a final result with isFinal=true inside the result handler rather than only in the completion handler — both paths must trigger the final callback.
Testing Requirements
Test on a physical iOS device with TestFlight distribution — SFSpeechRecognizer does not function on iOS Simulator. Write flutter_test integration tests using mock MethodChannel handlers that simulate: (1) permission granted response, (2) permission denied response, (3) a sequence of partial events followed by a final event, (4) a network error mid-recognition, (5) stopRecognition called before any result arrives. Verify that AVAudioSession is deactivated after stop by asserting no further EventChannel events arrive. Manual QA must include Norwegian speech input with nb-NO locale on a real device to confirm accurate recognition.
iOS 15 on-device speech recognition has a 1-minute session limit and requires network fallback for longer sessions. Peer mentor way-forward dictation may routinely exceed this limit, causing silent truncation of transcribed content without user feedback.
Mitigation & Contingency
Mitigation: Implement session-chunking logic in NativeSpeechApiBridge that automatically restarts recognition before the limit is reached, preserving continuity via partial concatenation. Document the iOS 15 vs iOS 16 on-device recognition behaviour difference in code comments.
Contingency: If chunking causes user-visible interruptions, surface a non-blocking informational banner on iOS 15 devices informing users that very long dictation sessions may need to be broken into segments, and use PartialTranscriptionRepository to persist each chunk immediately.
On iOS, speech recognition permission can only be requested once. If the user denies the permission, the app cannot re-request it. A poor first-impression permission flow will permanently disable dictation for those users, impacting the Blindeforbundet blind-user base who rely on dictation most.
Mitigation & Contingency
Mitigation: Design the NativeSpeechApiBridge permission flow to show a clear pre-permission rationale screen before the OS dialog. Implement a graceful degradation path that hides the microphone button and shows a settings deep-link when permission is permanently denied.
Contingency: If users have already denied permission before the rationale screen is added, provide a settings deep-link in DictationScopeGuard's denial message directing users to iOS Settings > Privacy > Speech Recognition to re-enable manually.
The approved field IDs and screen routes configuration in DictationScopeGuard may fall out of sync with the actual report form schema as new fields are added by org administrators, silently blocking dictation on legitimately approved fields.
Mitigation & Contingency
Mitigation: Source the approved field configuration from the same org-field-config-loader used by the report form, rather than a hardcoded list. Add a developer-time assertion that logs a warning when a dictation-eligible field type is rendered but not in the approved routes map.
Contingency: Provide a runtime override mechanism in the scope guard that coordinators or admins can use to temporarily whitelist a field ID while the config is updated, with an automatic expiry.