Problem with alert in iOS4.5

When I run the following code, it works fine on iOS 4.4 and up, but on iOS 4.5, the alert does not show up and the following log is output.


2021-05-08 18:21:34.680280+0900 SampleProject12.5[2534:139985] [Presentation] Attempt to present <SwiftUI.PlatformAlertController: 0x7ff27200ce00> on <_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS14NoStyleContext: 0x7ff26f825000> (from <_TtGC7SwiftUIP13$7fff5695965428DestinationHostingControllerVS7AnyView: 0x7ff271c06180>) whose view is not in the window hierarchy.


Code Block
import SwiftUI
struct ContentView: View {
  var body: some View {
    NavigationView {
      List {
        NavigationLink(destination: NextView()) {
          Text("Next View")
        }
      }
    }
  }
}
struct NextView: View {
  @State private var showingAlert = false
   
  var body: some View {
    Button("Show Alert") {
      showingAlert = true
    }
    .alert(isPresented: $showingAlert) {
      Alert(title: Text("Alert!"))
    }
    .onAppear {
      showingAlert = true
    }
  }
}






  • I have confirmed that this issue has been resolved in iOS15. According to Apple, it was fixed in iOS15 beta7.

Add a Comment

Accepted Reply

If you are trying to display an alert on screen transition, then you could try this
as a workaround until it is fixed properly:
Code Block
.onAppear {
DispatchQueue.main.async {
Thread.sleep(forTimeInterval: 0.01)
showingAlert = true
}
}

Replies

seems the Alert is trying to render before the View is fully setup, try this:
Code Block
.onAppear {
// showingAlert = true
}

Hi, thanks for your reply.
I am trying to display an alert on screen transition.
I believe this is an iOS 4.5 issue, as it worked up to iOS 4.4.
I have reported this to the Feedback Assistant.
It effectively works correctly on 14.4 (not 4.4😉) simulator.

The error speaks of splitView. I do not see it in the posted code.

There are some issues with List.
Could you try, just for testing, to replace List by VStack ?

There seems to have been some change with 14.5:
https://forums.swift.org/t/14-5-beta3-navigationlink-unexpected-pop/45279
If you are trying to display an alert on screen transition, then you could try this
as a workaround until it is fixed properly:
Code Block
.onAppear {
DispatchQueue.main.async {
Thread.sleep(forTimeInterval: 0.01)
showingAlert = true
}
}
Thank you very much. I was able to achieve what I wanted to do. > workingdogintokyo

Thanks for pointing that out, it's 14.5. > Claude31