Post

Replies

Boosts

Views

Activity

Reply to Translation API & Download Language Sheet
@DTS Engineer Thank you for the response. The attached code recreates the issue. We discovered the root appears to be a variable state-change while the Download Sheet is presented causes the sheet to auto-dismiss with errors. However, this error is only present on the first execution. Subsequent attempts do not auto-dismiss the sheet. Please let us know if this description and sample code are clear enough. Thanks! Example Code: // // TranslateSheet.swift // // import SwiftUI import Translation struct TranslateSheet: View { @State private var configuration: TranslationSession.Configuration? private let languageAvailability = LanguageAvailability() @State var text: String = "Sample Text to be Translated" @State var permitTranslation: Bool = true var body: some View { VStack (alignment: .center) { Text(text) .fontWeight(.black) .padding() Button("TAP to Translate English to Spanish & Show Download Sheet") { performTranslation(from: "en_US", to: "es_ES") DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { permitTranslation = false } } .padding() Button("Permitted: \(permitTranslation)") { permitTranslation.toggle() } Group { Text("Make sure Spanish Language is not already downlaoded.") Text("First Launch: Permission is True. Tap Translate English to Spanish.") Text("Dispatch Queue simulates change of state from true to false while sheet is presented.") Text("When sheet dismisses, observe hundreds of lines of repeating errors in Xcode console.\n") Text("Also, setting the state back to true by tapping 'Permitted: false', then attempting translation again does not cause same auto-dismissal error!") } .font(.title3) Spacer() } .font(.title) .translationTask(configuration) { session in Task { @MainActor in do { print("preparing...") // Show Download Lanugage sheet from Translation API try await session.prepareTranslation() print("executing translation") let response = try await session.translate(text) text = response.targetText } catch { print("Translate failed: \(error)") } } } } func performTranslation(from sourceLanguageCode: String, to targetLanguageCode: String) { print("excecuting performTranslation() - 830: sourceLanguageCode: \(sourceLanguageCode), targetLanguageCode: \(targetLanguageCode)") // check if we need to create a translation configuration if configuration == nil { configuration = TranslationSession.Configuration.init( source: Locale.Language(identifier: sourceLanguageCode), target: Locale.Language(identifier: targetLanguageCode) ) } else { // or just update the target code then invalidate the config to re-trigger the refresh of .translationTask() configuration?.source = Locale.Language(identifier: sourceLanguageCode) configuration?.target = Locale.Language(identifier: targetLanguageCode) configuration?.invalidate() } }}
Topic: App & System Services SubTopic: General Tags:
May ’25
Reply to Translation API & Download Language Sheet
Resolution Incase anyone stumbles on this, here's our solution to "can't use if-statements in .translationTask(configuration)" Originally I built a ViewModifier to switch between conditions (download only, or perform translation); but that didn't work for our use case. View Modifiers redraw the view causing TextEditors to lose their focus, which would not work for us. I landed on making a second TranslationSession.Configuration one for checking if language is downloaded, one for performing the translation. We check which to execute when the user taps the translate button: Two Configurations device owner is translating to @State var configurationForDownloadCheck: TranslationSession.Configuration? @State var configuration: TranslationSession.Configuration? Check which to run .onTapGesture { Task { // check if languages are downloaded and set control var let languagesFullyDownloaded = await translationLanguageIsDownloaded(from: nativeLanguageCode!, to: guestLanguageCode!) performFullTranslate = languagesFullyDownloaded // decide if we need can translate or need to download first. if performFullTranslate { // send for translation work performTranslation(from: nativeLanguageCode!, to: guestLanguageCode!) } else { // send to check of Downloaded or not downloadLanguages(from: nativeLanguageCode!, to: guestLanguageCode!) } } } View Modifier (works, but not for our needs) @available(iOS 18.0, *) struct TaskTranslationSwitchModifier: ViewModifier { @EnvironmentObject var textObject: TextObject @Binding var configuration: TranslationSession.Configuration? let performFullTranslate: Bool @Binding var lastTranslation: String // Callback for notifying about text changes var onTextChanged: ((String) -> Void)? func body(content: Content) -> some View { if performFullTranslate { content .translationTask(configuration) { session in print("greg: performing full translate") Task { @MainActor in do { // prepare try await session.prepareTranslation() // send inputText to Translation API let response = try await session.translate(textObject.inputText) } catch { print("Translate failed: \(error)") } } } } else { content .translationTask(configuration) { session in print("greg: performing DL sheet ONLY!") Task { @MainActor in do { // just prepare the DL sheet try await session.prepareTranslation() } catch { print("Translate failed: \(error)") } } } } } }
Topic: App & System Services SubTopic: General Tags:
May ’25
Reply to Translation API & Download Language Sheet
Progress Update: An if-statement inside .translationTask(configuration){} seems to be causing the issue:. The goal is using only 1 UI element, check if the language is downloaded. Only present the Download Sheet if needed; otherwise, perform the translation. Presently looking for work-arounds. Doesn't work (sheet auto-dismisses): .translationTask(configuration) { session in Task { @MainActor in do { if performFullTranslate { // prepare try await session.prepareTranslation() // send inputText to Translation API let response = try await session.translate(textObject.inputText) // swap the inputText for the translation textObject.inputText = response.targetText } else { // just prepare the DL sheet try await session.prepareTranslation() } } catch { print("Translate failed: \(error)") } } } Works (presents sheet, but can't perform translation): .translationTask(configuration) { session in Task { @MainActor in do { // just prepare the DL sheet try await session.prepareTranslation() } catch { print("Translate failed: \(error)") } } }
Topic: App & System Services SubTopic: General Tags:
May ’25
Reply to iOS17 UITextView inputView becomFirstResponder does not work
Using TextField in an .alert() and this bug seems to be preventing the OK button from executing: .alert("New Category", isPresented: $showNewCategoryNameAlert) { TextField("Enter Name", text: $userNewCategoryName) Button("OK", action: { print("Clicked OK") addCategory(newName: userNewCategoryName) userNewCategoryName = "" }).disabled(userNewCategoryName.isEmpty) Button("Cancel", role: .cancel) { } Enter Name Tap OK - dismisses Print statement and addCategory don't run BUT! Launch the .alert() again and click OK, everything executes. This issue is not present in iOS 18. Open to ideas for a work around!
Topic: UI Frameworks SubTopic: General Tags:
Apr ’25
Reply to SwiftUI: Conditionally switching between .fullScreenCover() and .sheet() not working
For anyone else stumbling on this post like I did... I verified OP @NicolasBrunnerSRF post appears to be an issue in ipadOS 17.x; but is resolved in ipadOS 18.x. The code recognizes the horizontal size has changed; but SwiftUI doesn't reflect the change in the UI element from sheet to fullScreenCover. I haven't found a work around :-\ @NicolasBrunnerSRF thanks for sharing your .modal ViewModifier.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’25