Hello everyone!
I'm having a problem with background tasks running in the foreground.
When a user enters the app, a background task is triggered. I've written some code to check if the app is in the foreground and to prevent the task from running, but it doesn't always work. Sometimes the task runs in the background as expected, but other times it runs in the foreground, as I mentioned earlier.
Could it be that I'm doing something wrong? Any suggestions would be appreciated.
here is code:
class BackgroundTaskService {
@Environment(\.scenePhase) var scenePhase
static let shared = BackgroundTaskService()
private init() {}
// MARK: - create task
func createCheckTask() {
let identifier = TaskIdentifier.check
BGTaskScheduler.shared.getPendingTaskRequests { requests in
if requests.contains(where: { $0.identifier == identifier.rawValue }) {
return
}
self.createByInterval(identifier: identifier.rawValue, interval: identifier.interval)
}
}
private func createByInterval(identifier: String, interval: TimeInterval) {
let request = BGProcessingTaskRequest(identifier: identifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: interval)
scheduleTask(request: request)
}
// MARK: submit task
private func scheduleTask(request: BGProcessingTaskRequest) {
do {
try BGTaskScheduler.shared.submit(request)
} catch {
// some actions with error
}
}
// MARK: background actions
func checkTask(task: BGProcessingTask) {
let today = Calendar.current.startOfDay(for: Date())
let lastExecutionDate = UserDefaults.standard.object(forKey: "lastCheckExecutionDate") as? Date ?? Date.distantPast
let notRunnedToday = !Calendar.current.isDate(today, inSameDayAs: lastExecutionDate)
guard notRunnedToday else {
task.setTaskCompleted(success: true)
createCheckTask()
return
}
if scenePhase == .background {
TaskActionStore.shared.getAction(for: task.identifier)?()
}
task.setTaskCompleted(success: true)
UserDefaults.standard.set(today, forKey: "lastCheckExecutionDate")
createCheckTask()
}
}
And in AppDelegate:
BGTaskScheduler.shared.register(forTaskWithIdentifier: "check", using: nil) { task in
guard let task = task as? BGProcessingTask else { return }
BackgroundTaskService.shared.checkNodeTask(task: task)
}
BackgroundTaskService.shared.createCheckTask()
SwiftUI
RSS for tagProvide views, controls, and layout structures for declaring your app's user interface using SwiftUI.
Posts under SwiftUI tag
200 Posts
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I am developing a MacOS app with a vertically oriented ScrollView. Inside that View I use a LazyVStack. In the LazyVStack I have a column of images. I want the images to occupy as much of the LazyVStack width as possible so I created a frame for the VStack with a maxWidth: .infinity.
When the user grabs the edge of the window and drags it to reduce the window width, the Views of the window adjust themselves to fit the reduced space. This includes reducing the width of the images. I maintain a fixed aspect ratio for the images, so the height of the images is reduced too.
My problem arises when the width of the images (and height) reduces to the point where the scroll bar is no longer needed because all of the images fit within the height of the ScrollView, and the ScrollView removes the scroll bar. That triggers a redraw of the images, with a slightly bigger size because the width used by the scroll bar is now available. When the the images get bigger they don't all fit within the height of the ScrollView anymore and the scrollbar is restored. That, of course, reduces the space available for the images, so they are reduced in size again - which then triggers the ScrollView to remove the scrollbar again. The whole thing goes into a spasm of flickering images (bigger and smaller) and scrollbar (off and on) until finally I get the dreaded error message:
The window has been marked as needing another Display Window pass, but it has already had more Display Window passes than there are views in the window.
There seems to be no way to prevent this behavior by setting an option on the ScrollView. It would be great if one could indicate that the scroll bar should always remain visible, or at least that the space occupied by the scroll bar remain, even if it is just empty.
The only way I could solve this problem was to go through a somewhat involved calculation to set the image width in such a way that it still responds to window width changes (and some other size changes I allow in some of the other app's Views). This complication would not be necessary if there were better controls on the scroll bar.
Is there a reason for not providing adequate controls for the ScrollView? Its uncontrollable behavior complicates the programming.
Hello,
In my SwiftUI App i'm trying to create a custom UI trait and a matching bridged SwiftUI environment key. I want to override the environment key in a swift view and then have that reflect in the current UITraitCollection.
I'm following the pattern in the linked video but am not seeing the changes reflect in the current trait collection when I update the swift env value.
I can't find anything online that is helping.
Does anyone know what I am missing?
https://developer.apple.com/videos/play/wwdc2023/10057/
// Setup
enum CustomTheme: String, Codable {
case theme1 = “theme1”,
theme2 = “theme2”
}
struct customThemeTrait: UITraitDefinition {
static let defaultValue = brand.theme1
static let affectsColorAppearance = true
static let identifier = "com.appName.customTheme"
}
extension UITraitCollection {
var customTheme: CustomTheme { self[customThemeTrait.self] }
}
extension UIMutableTraits {
var customTheme: CustomTheme {
get { self[customThemeTrait.self] }
set { self[customThemeTrait.self] = newValue }
}
}
private struct customThemeKey: EnvironmentKey {
static let defaultValue: CustomTheme = .theme1
}
extension customThemeKey: UITraitBridgedEnvironmentKey {
static func read(from traitCollection: UITraitCollection) -> CustomTheme {
traitCollection.customTheme
}
static func write(to mutableTraits: inout UIMutableTraits, value: CustomTheme) {
mutableTraits.customTheme = value
}
}
extension EnvironmentValues {
var customTheme: CustomTheme {
get { self[customThemeKey.self] }
set { self[customThemeKey.self] = newValue }
}
}
// Attempted Usage
extension Color {
static func primaryBackground() -> Color {
UITraitCollection.current.customTheme == .theme1 ? Color.red : Color.blue
}
}
struct ContentView: View {
@State private var theme = .theme1
var body: some View {
if (dataHasLoaded && themeIsSet) {
HomeView()
.environment(\.customTheme, theme)
} else {
SelectThemeView( theme: self.theme, setContentThemeHandler)
}
}
func setContentThemeHandler(theme: customTheme) {
self.theme = theme
}
}
struct HomeView() {
@Environment(\.customTheme) private var currentTheme: customTheme
var body: some View {
VStack {
Text("currentTheme: \(currentTheme.rawValue)")
.background(Color.primaryBackground())
Text("currentUITrait: \(UITraitCollection.current.customTheme.rawValue)")
.background(Color.primaryBackground())
}
}
}
OUTCOME:
After selecting theme2 in the theme selector view and navigating to the homeView, the background is still red and the env and trait values print the following:
currentTheme: theme2
currentUITrait: theme1
Can anyone help me identify what I am missing?
I keep running into the following issue when trying to run preview for my application in Xcode.
FailedToLaunchAppError: Failed to launch app.a
/Users/me/Library/Developer/Xcode/DerivedData/a-aloudjuytoewlldqjfxshbjydjeh/Build/Products/Debug/a.app
==================================
| [Remote] JITError
|
| ==================================
|
| | [Remote] XOJITError
| |
| | XOJITError: Could not create code file directory for session: Permission denied`
This is my first post here. Please guide me, if I need to provide more information to answer this post.
I write a simple application, that monitors GPS position (location). I followed Apple documentation for LiveUpdates: https://developer.apple.com/documentation/corelocation/supporting-live-updates-in-swiftui-and-mac-catalyst-apps
My app can monitor location in foreground, background or it can completely stop monitoring location. Background location, if needed, is switched on when application changes scenePhase to .background. But it is in the foreground, that memory leaks occur (according to Instruments/Leaks. Namely Leaks points to the instruction:
let updates = CLLocationUpdate.liveUpdates()
every time I start location and then stop it, by setting updatesStarted to false.
Leaks claims there are 5x leaks there:
Malloc 32 Bytes 1 0x6000002c1d00 32 Bytes libswiftDispatch.dylib OS_dispatch_queue.init(label:qos:attributes:autoreleaseFrequency:target:)
CLDispatchSilo 1 0x60000269e700 96 Bytes CoreLocation 0x184525c64
Malloc 48 Bytes 1 0x600000c8f2d0 48 Bytes Foundation +[NSString stringWithUTF8String:]
NSMutableSet 1 0x6000002c4240 32 Bytes LocationSupport 0x18baa65d4
dispatch_queue_t (serial) 1 0x600002c69c80 128 Bytes libswiftDispatch.dylib OS_dispatch_queue.init(label:qos:attributes:autoreleaseFrequency:target:)
I tried [weak self] in Task, but it doesn't solve the leaks problem and causes other issues, so I dropped it. Anyway, Apple doesn't use it either.
Just in case this is my function, which has been slightly changed comparing to Apple example, to suit my needs:
func startLocationUpdates() {
Task() {
do {
self.updatesStarted = true
let updates = CLLocationUpdate.liveUpdates()
for try await update in updates {
// End location updates by breaking out of the loop.
if !self.updatesStarted {
self.location = nil
self.mapLocation = nil
self.track.removeAll()
break
}
if let loc = update.location {
let locationCoordinate = loc.coordinate
let location2D = CLLocationCoordinate2D(latitude: locationCoordinate.latitude, longitude: locationCoordinate.longitude)
self.location = location2D
if self.isAnchor {
if #available(iOS 18.0, *) {
if !update.stationary {
self.track.append(location2D)
}
} else {
// Fallback on earlier versions
if !update.isStationary {
self.track.append(location2D)
}
}
}
}
}
} catch {
//
}
return
}
}
Can anyone help me locating these leaks?
When I try to implement the new Background Task options in the same way as they show in the WWDC video (on watchOS) likes this:
let config = URLSessionConfiguration.background(withIdentifier: "SESSION_ID")
config.sessionSendsLaunchEvents = true
let session = URLSession(configuration: config)
let response = await withTaskCancellationHandler {
try? await session.data(for: request)
} onCancel: {
let task = session.downloadTask(with: request))
task.resume()
}
I'm receiving the following error:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'
Did I forget something?
I have a memory leak, when using AVAudioPlayer. I managed to narrow down the issue into a very simple app, which code I paste in at the end.
The memory leak start immediately when I start playing sound, but only in the emylator. On the real iPhone there is no memory leak.
The memory leak on the Simulator looks like this:
import SwiftUI
import AVFoundation
struct ContentView_Audio: View {
var sound: AVAudioPlayer?
init() {
guard let path = Bundle.main.path(forResource: "cd201", ofType: "mp3") else { return }
let url = URL(fileURLWithPath: path)
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers])
} catch {
return
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
return
}
do {
sound = try AVAudioPlayer(contentsOf: url)
} catch {
return
}
}
var body: some View {
HStack {
Button {
playSound()
} label: {
ZStack {
Circle()
.fill(.mint.opacity(0.3))
.frame(width: 44, height: 44)
.shadow(radius: 8)
Image(systemName: "play.fill")
.resizable()
.frame(width: 20, height: 20)
}
}
.padding()
Button {
stopSound()
} label: {
ZStack {
Circle()
.fill(.mint.opacity(0.3))
.frame(width: 44, height: 44)
.shadow(radius: 8)
Image(systemName: "stop.fill")
.resizable()
.frame(width: 20, height: 20)
}
}
.padding()
}
}
private func playSound() {
guard sound != nil else { return }
sound?.volume = 1
// sound?.numberOfLoops = -1
sound?.play()
}
func stopSound() {
sound?.stop()
}
}
Issue Description
Whenever the first item in the List is a DisclosureGroup, all subsequent disclosure groups work fine. However, if the first item is not a disclosure group, the disclosure groups in subsequent items do not render correctly.
This issue does not occur in macOS 15, where everything works as expected.
Has anyone else encountered this behavior, or does anyone have a workaround for macOS 13 & 14?
I’m not using OutlineGroup because I need to bind to an isExpanded property for each row in the list.
Reproduction Steps
I’ve created a small test project to illustrate the issue:
Press “Insert item at top” to add a non-disclosure item at the start of the list.
Then, press “Append item with sub-item” to add a disclosure group further down.
The disclosure group does not display correctly. The label of the disclosure group renders fine, but the content of the disclosure group does not display at all.
Press "Insert item at top with sub-item" and the list displays as expected.
Build Environment
macOS 15.3.2 (24D81)
Xcode Version 16.2 (16C5032a)
Issue Observed
macOS 13 & 14 (bug occurs)
macOS 15 (works correctly)
Sample Code
import SwiftUI
class ListItem: ObservableObject, Hashable, Identifiable {
var id = UUID()
@Published var name: String
@Published var subItems: [ListItem]?
@Published var isExpanded: Bool = true
init(
name: String,
subjobs: [ListItem]? = nil
) {
self.name = name
self.subItems = subjobs
}
static func == (lhs: ListItem, rhs: ListItem) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
struct ContentView: View {
@State private var listItems: [ListItem] = []
@State private var selectedJob: ListItem?
@State private var redraw: Int = 0
var body: some View {
VStack {
List(selection: $selectedJob) {
ForEach(self.listItems, id: \.id) { job in
self.itemRowView(for: job)
}
}
.id(redraw)
Button("Insert item at top") {
self.listItems.insert(
ListItem(
name: "List item \(listItems.count)"
),
at: 0
)
}
Button("Insert item at top with sub-item") {
self.listItems.insert(
ListItem(
name: "List item \(listItems.count)",
subjobs: [ListItem(name: "Sub-item")]
),
at: 0
)
}
Button("Append item") {
self.listItems.append(
ListItem(
name: "List item \(listItems.count)"
)
)
}
Button("Append item with sub-item") {
self.listItems.append(
ListItem(
name: "List item \(listItems.count)",
subjobs: [ListItem(name: "Sub-item")]
)
)
}
Button("Clear") {
self.listItems.removeAll()
}
Button("Redraw") {
self.redraw += 1
}
}
}
@ViewBuilder
private func itemRowView(for job: ListItem) -> some View {
if job.subItems == nil {
self.itemLabelView(for: job)
} else {
AnyView(
erasing: ListItemDisclosureGroup(job: job) {
self.itemLabelView(for: job)
} jobRowView: { child in
self.itemRowView(for: child)
}
)
}
}
@ViewBuilder private func itemLabelView(for job: ListItem) -> some View {
Text(job.name)
}
struct ListItemDisclosureGroup<LabelView: View, RowView: View>: View {
@ObservedObject var job: ListItem
@ViewBuilder let labelView: () -> LabelView
@ViewBuilder let jobRowView: (ListItem) -> RowView
var body: some View {
DisclosureGroup(isExpanded: $job.isExpanded) {
if let children = job.subItems {
ForEach(children, id: \.id) { child in
self.jobRowView(child)
}
}
} label: {
self.labelView()
}
}
}
}
When i have TextField inside ScrollView and tap on it the keyboard is shown as expected. But it seems that the TextField is moved up just enough to show the input area but i want to be moved enough so that is visible in its whole. Otherwise it looks cropped. I couldn't find a way to change this behaviour.
struct ContentView: View {
@State var text:String = ""
var body: some View {
ScrollView {
VStack(spacing: 10) {
ForEach(1...12, id: \.self) {
Text("\($0)…")
.frame(height:50)
}
TextField("Label..", text: self.$text)
.padding(10)
.background(.white)
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(.blue, lineWidth: 1)
)
}
.padding()
.background(.red)
}
}
}
Dear Developers and DTS team,
This is writing to seek your expert guidance on a persistent memory leak issue I've discovered while implementing video playback in a SwiftUI application.
Environment Details:
iOS 17+, Swift (SwiftUI, AVKit), Xcode 16.2
Target Devices:
iPhone 15 Pro (iOS 18.3.2)
iPhone 16 Plus (iOS 18.3.2)
Detailed Issue Description:
I am experiencing consistent memory leaks when using UIViewControllerRepresentable with AVPlayerViewController for FullscreenVideoPlayer and native VideoPlayer during video playback termination.
Code Context:
I have implemented the following approaches:
Added static func dismantleUIViewController(: coordinator:)
Included deinit in Coordinator
Utilized both UIViewControllerRepresentable and native VideoPlayer
/// A custom AVPlayer integrated with AVPlayerViewController for fullscreen video playback.
///
/// - Parameters:
/// - videoURL: The URL of the video to be played.
struct FullscreenVideoPlayer: UIViewControllerRepresentable {
// @Binding something for controlling fullscreen
let videoURL: URL?
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.delegate = context.coordinator
print("AVPlayerViewController created: \(String(describing: controller))")
return controller
}
/// Updates the `AVPlayerViewController` with the provided video URL and playback state.
///
/// - Parameters:
/// - uiViewController: The `AVPlayerViewController` instance to update.
/// - context: The SwiftUI context for updates.
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
guard let videoURL else {
print("Invalid videoURL")
return
}
// Initialize AVPlayer if it's not already set
if uiViewController.player == nil || uiViewController.player?.currentItem == nil {
uiViewController.player = AVPlayer(url: videoURL)
print("AVPlayer updated: \(String(describing: uiViewController.player))")
}
// Handle playback state
}
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
static func dismantleUIViewController(_ uiViewController: AVPlayerViewController, coordinator: Coordinator) {
uiViewController.player?.pause()
uiViewController.player?.replaceCurrentItem(with: nil)
uiViewController.player = nil
print("dismantleUIViewController called for \(String(describing: uiViewController))")
}
}
extension FullscreenVideoPlayer {
class Coordinator: NSObject, AVPlayerViewControllerDelegate {
var parent: FullscreenVideoPlayer
init(parent: FullscreenVideoPlayer) {
self.parent = parent
}
deinit {
print("Coordinator deinitialized")
}
}
}
struct ContentView: View {
private let videoURL: URL? = URL(string: "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4")
var body: some View {
NavigationStack {
Text("My Userful View")
List {
Section("VideoPlayer") {
NavigationLink("FullscreenVideoPlayer") {
FullscreenVideoPlayer(videoURL: videoURL)
.frame(height: 500)
}
NavigationLink("Native VideoPlayer") {
VideoPlayer(player: .init(url: videoURL!))
.frame(height: 500)
}
}
}
}
}
}
Reproducibility Steps:
Run application on target devices
Scenario A - FullscreenVideoPlayer:
Tap FullscreenVideoPlayer
Play video to completion
Repeat process 5 times
Scenario B - VideoPlayer:
Navigate back to main screen
Tap Video Player
Play video to completion
Repeat process 5 times
Observed Memory Leak Characteristics:
Per Iteration (Debug Memory Graph):
4 instances of NSMutableDictionary (Storage) leaked
4 instances of __NSDictionaryM leaked
4 × 112-byte malloc blocks leaked
Cumulative Effects:
Debug console prints: "dismantleUIViewController called for <AVPlayerViewController: 0x{String}> Coordinator deinitialized" when navigate back to main screen
After multiple iterations, leak instances double
Specific Questions:
What underlying mechanisms are causing these memory leaks in UIViewControllerRepresentable and VideoPlayer?
What are the recommended strategies to comprehensively prevent and resolve these memory management issues?
I have a Date field that holds the scheduled start date for an activity.. However, activities can be unscheduled (i.e., waiting to be scheduled at some other time). I want to use Date.distantFuture to indicate that the activity is unscheduled. Therefore I am looking to implement logic in my UI that looks something like
@State private var showCalendar: Bool = false
if date == .distantFuture {
Button("unscheduled") {
showCalendar.toggle()
}.buttonStyle(.bordered)
} else {
DatePicker(section: $date)
}
.popover(isPresented: $showCalendar) {
<use DatePicker Calendar view>
}
But this approach requires that I access the DataPicker's Calendar view and I don't know how to do that (and I don't ever what my users to see "Dec 31, 4000"). Any ideas?
(BTW, I have a UIKit Calendar control I could use, but I'd prefer to use the standard control if possible.)
import SwiftUI
import OsLog
let logger = Logger(subsystem: "Test", category: "Test")
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
.task {
logger.info("Hallo")
}
}
}
#Preview {
ContentView()
}
27 | .padding()
28 | .task {
29 | logger.info(__designTimeString("#6734_2", fallback: "Hallo"))
| `- error: argument must be a string interpolation
30 | }
31 | }
Should OsLog be compatible with __designTimeString?
I was doing an app which had several "camera" buttons each one dedicated to taking/storing/reviewing/deleting an image associated with a variable URL but what should have been a simple no brainer turned out to be a programming nightmare.
To cut a long story short there is a bug in the sheet handling wherebye even tho you have separate instance for each button the camera/picker cylcles sequentially thru the stack of instances for any action finally always placing the image in the first URL. Working with myself debugging, all major AIs (Grok, Claude, Gemini and Perplexity) after 4 x 12hr+ days we finally managed to crack a solution. What follows is Groks interpretation (note it misses the earlier problem of instance cycling!!) ...
You can follow the discussion here: https://x.com/i/grok/share/KHeaUPladURmbFq5qy9W506er
but be warned its long a detailed but if you are having problems then read ...
**Bug Report: Race Conditions with UIImagePickerController in SwiftUI Sheet
**
Environment:
SwiftUI, iOS 17.7.5
Device: iPad Pro (12.9-inch, 2nd generation)
Xcode Version: [Insert your Xcode version]
Date: March 30, 2025
**Issue 1: Multiple Instances of UIImagePickerController Spawned After Dismissal
**
Description:
When using a UIImagePickerController wrapped in a UIViewControllerRepresentable and presented via a SwiftUI .sheet, selecting "Use Photo" resulted in multiple unintended instances of the picker being initialized and presented. The console logs showed repeated "Camera initialized" and "Camera sheet appeared" messages (e.g., multiple <UIImagePickerController: 0x...> instances) after the initial dismissal, despite the sheet being dismissed programmatically.
Reproduction Steps:
Create a SwiftUI view with a button that sets a @State variable showCamera to true.
Present a UIImagePickerController via .sheet(isPresented: $showCamera).
Update a @Binding variable (e.g., photoLocation: URL?) in imagePickerController(_:didFinishPickingMediaWithInfo:) after saving the image.
Dismiss the picker with picker.dismiss(animated: true) and presentationMode.wrappedValue.dismiss().
Observe that updating the @Binding variable triggers a view re-render, causing the .sheet to re-present multiple times before finally staying dismissed.
Root Cause:
A race condition occurred between the view update (triggered by changing photoLocation) and the dismissal of the picker. During the re-render, showCamera remained true momentarily, causing the .sheet modifier to re-evaluate and spawn new picker instances before the onDismiss closure could reset showCamera to false.
The fix involved delaying the @Binding update (photoLocation) until after the picker and sheet were fully dismissed, ensuring showCamera was reset to false before the view re-rendered:
Introduced an onPhotoPicked: (URL) -> Void closure to decouple the photoLocation update from the dismissal timing.
Modified the coordinator to call onPhotoPicked and reset showCamera before initiating dismissal:swift
Issue 2: Single Unintended Picker Reopen After Initial Fix
Description:
After addressing the multiple-instance issue, a single unintended reopen of the picker persisted. The logs showed one additional "Camera initialized" and "Camera sheet appeared" after "Use Photo," before the final dismissal.
Reproduction Steps:
Reproduction Steps:
Use the initial fix with onPhotoPicked and delayed photoLocation update.
Take a photo and select "Use Photo."
Observe one extra picker instance appearing briefly before dismissal completes.
Root Cause:
The @Binding update (photoLocation) was still occurring too early in the dismissal sequence. Although delayed until after picker.dismiss, the view re-render happened while showCamera was still true during the dismissal animation, causing the .sheet to re-present once before onDismiss reset showCamera.
Resolution:
The fix ensured showCamera was set to false before the picker dismissal animation began, preventing the .sheet from re-evaluating during the transition:
Moved the dismissCamera() call (which sets showCamera to false) into the onPhotoPicked callback, executed before picker.dismiss:
CameraView(
photoLocation: $photoLocation,
storeDirectory: storeDirectory,
onPhotoPicked: { url in
print("Photo picked callback for \(id), setting photoLocation: \(url)")
self.photoLocation = url
self.cameraState.dismissCamera() // Sets showCamera to false first
}
)
Kept the dismissal sequence in the coordinator:
DispatchQueue.main.async {
self.parent.onPhotoPicked(fileURL)
picker.dismiss(animated: true) {
self.parent.presentationMode.wrappedValue.dismiss()
}
}
This synchronized the state change with the dismissal, ensuring showCamera was false before the view re-rendered, eliminating the single reopen.
Request:
Could the SwiftUI team clarify if this behavior is expected, or consider improving the .sheet modifier to better handle state transitions during UIKit controller dismissal? A more robust bridge between SwiftUI’s declarative state and UIKit’s imperative lifecycle could prevent such race conditions.
Background
I have a SwiftUI app that uses OSLog and the new Logger framework. In my SwiftUI views, I would like to use something like Self._logChanges() to help debug issues.
After some trial and error, I can see the messages appear in the System console log for the app I am debugging using the com.apple.SwiftUI subsystem.
Problem
I'd like to see those same messages directly in Xcode's console window so I can filter them as needed. How do I do that?
Thanks! -Patrick
As in the title: openImmersiveSpace works as expected. The ImmersiveSpace in the ID opens normally but the function never resolves a Result. Here is a workaround I used to make user it was on the UI thread and scenePhase was active:
`
@MainActor func openSpaceWithStateCheck() async {
if scenePhase == .active {
Task {
switch await openImmersiveSpace(id: "RoomCaptureInteraction") {
case .opened:
isCapturingImagery = true
break
case .error:
print("!! An error occurred when trying to open the immersive space captureRoomImagery")
case .userCancelled:
print("!! The user declined opening immersive space captureRoomImagery")
@unknown default:
print("!! unknown default result of opening space")
break
}
}
} else {
print("Scene not active, deferring immersive space opening")
}
}
I'm on visionOS 2.4 and SDK 2.2.
I have tried uninstalling the app and rebuilding. Tried simply opening an empty ImmersiveSpace.
The consistency of the ImmersiveSpace opening at least means I can work around it. Even dismissImmersiveSpace works normally and closes the immersive space. But a workaround seems hamfisted.
Problem
Setting ".environment(.layoutDirection, .rightToLeft)" to a view programmatically won't make buttons in menu to show right to left.
However, setting ".environment(.locale, .init(identifier: "he-IL"))" to a view programmatically makes buttons in menu to show Hebrew strings correctly.
Development environment: Xcode 16.x, macOS 15.3.1
Target iOS: iOS 17 - iOS 18
The expected result is that the button in the menu should be displayed as an icon then a text from left to right.
Code to demonstrate the problem:
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Buttons in menu don't respect the environment value of .layoutDirection")
.font(.subheadline)
.padding(.bottom, 48)
/// This button respects both "he-IL" of ".locale" and ".rightToLeft" of ".layoutDirection".
Button {
print("Button tapped")
} label: {
HStack {
Text("Send")
Image(systemName: "paperplane")
}
}
Menu {
/// This button respects "he-IL" of ".locale" but doesn't respect ".rightToLeft" of ".layoutDirection".
Button {
print("Button tapped")
} label: {
HStack {
Text("Send")
Image(systemName: "paperplane")
}
}
} label: {
Text("Menu")
}
}
.padding()
.environment(\.locale, .init(identifier: "he-IL"))
.environment(\.layoutDirection, .rightToLeft)
}
}
I was determined to fully rely on SwiftUI's navigation system giving the fact I'm starting a new project - strongly reconsidering this after a very basic requirement.
Namely: navigate between my two screens while wanting to hide the back button label.
One of them has a large navigation title
The other one has no navigation title
The problem is that back button icon simply jumps from top, a little bit down (where the title is supposed to be) and then back to top while navigating - resulting in a rough bouncy animation.
struct ParentView: View {
var body: some View {
NavigationStack {
WelcomeView()
.navigationDestination(for: WelcomeRoute.self) { route in
destinationView(for: route)
}
}
}
...
}
struct WelcomeView: View {
var body: some View {
ScrollView {
VStack {
...
NavigationLink(value: WelcomeRoute.Routes.next) {
Text("Next screen")
}
}
}
.navigationTitle("WELCOME")
.navigationBarTitleDisplayMode(.large)
}
}
struct NextWelcomeView: View {
var body: some View {
ScrollView {
VStack {
....
Text("Hello")
}
}
.toolbarRole(.editor)
}
}
Since tvOS 18, my Picker view with inline style is not showing the checkmark on the selected item.
enum Flavor: String, Identifiable {
case chocolate, vanilla, strawberry
var id: Self { self }
}
struct ContentView: View {
@State private var selectedFlavor: Flavor = .chocolate
var body: some View {
NavigationView {
Form {
Picker("Flavor", selection: $selectedFlavor) {
Text("Chocolate").tag(Flavor.chocolate)
Text("Vanilla").tag(Flavor.vanilla)
Text("Strawberry").tag(Flavor.strawberry)
}.pickerStyle(.inline)
}
}
}
}
Am I missing something? When I run this on tvOS 17.x, it works fine.
I'm trying to make a timetable app for my Apple Watch, and it has all actually been going pretty smoothly until this random error started showing up when I try to build my application. Here's a code snippet:
// all on top level:
class globalStorage {
static let shared = globalStorage() // line 27
@State @AppStorage(runningKey) var termRunningGB = false
@State @AppStorage(ghostWeekKey) var ghostWeekGB = false
@State @AppStorage(startDateKey) var startDateGB = Date.now
var currentCourse: Course = getCurrentClass(date: .now)
}
let storage = globalStorage.shared // &lt;&lt; ERRORING HERE (line
// ...
@main
struct myApp: App { /* ... */ }
Can anybody tell me what is happening? (And, of course, how to fix it?)
Furthermore, upon removing the offending line (let storage = globalStorage.shared) (and replacing all callers of said variable with 'globalStorage.shared' to bypass the variable) the error has decided to settle on line 27, where i define the 'shared' thing in the class.
[ I just went back to try more solutions, I have resolved it but forgot to give my solution here. Now I've forgotten how I fixed it. I do know that I moved currentCourse out of the class, that most likely was it I think.]
All of this code is on GitHub: https://github.com/the-trumpeter/Timetaber-for-iWatch
I would like to understand how to programmatically set the position of a cursor in a SwiftUI TextField.
In UIKit this can be done using the selectedTextRange property, but I couldn't find a similar way to achieve this with pure SwiftUI.
I want to figure out something like setCursorPosition (index:) - maybe by tracking the position in a @State or any other way.
I understand that I can do this using UIViewRepresentable but I am looking for a pure SwiftUI solution and wanted to know if there is any.