iOS SwiftUI - get the height of top safe area - ios

I tried this:
let window = UIApplication.shared.windows.first
print("safeareatop: \(window?.safeAreaInsets.top)")
and get:
'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
so I tried:
UIWindowScene.windows.first
but I get another error, the error messages from xcode are absolutely useless compared to android studios btw
the "solutions" from here:
Why it is throw an error as "'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" in SwiftUI?
and here:
'windows' was deprecated in iOS 15.0
are still giving me this message.
So how to do this?

With access to window provided in reference (https://stackoverflow.com/a/60359809/12299030) works fine here.
Tested with Xcode 13.4 / iOS 15.5 / iPhone 12

Use GeometryReader
struct ContentView: View {
var body: some View {
GeometryReader { proxy in
Text("Something")
.onAppear {
print(proxy.safeAreaInsets)
}
}
}
}

Related

SwiftUI & requestReview() – review popup not displaying

I am building an app with SwiftUI and would like to use requestReview() to display a review request popup. However, the popup is not displaying. From what I understand from the documentation, when the app is in development mode, the popup should display every time.
I've tested the app in the Xcode simulator and on a real device, I'm using Xcode 14.1. and the app builds without issue. (I'm also a SwiftUI newbie, I think I must be missing something obvious).
import SwiftUI
import StoreKit
struct DetailView: View {
#Environment(\.requestReview) var requestReview
#AppStorage("AppUsedCount") private var appUsedCount: Int = 0
var body: some View {
VStack {...}.onAppear{
let updatedAppUsedCount: Int = appUsedCount + 1
UserDefaults.standard.set(updatedAppUsedCount, forKey: "AppUsedCount")
if (appUsedCount > 5){
requestReview()
}
}
}
}
I also tested without the IF statement, but still no popup. appUsedCount updates as expected.
Thanks in advance!

iOS widget always displaying placeholder on simulator or device

I'm made an very basic iOS Widget Extension with (on iOS 16):
struct TotoView: View
var body: some View {
VStack {
Text("Toto")
.font(.headline)
}
}
}
struct TotoWidget: Widget {
var body: some WidgetConfiguration {
IntentConfiguration(kind: "myKind", intent: ViewTodayIntent.self, provider: TimelineProvider()) { entry in
TotoView(entry: entry)
}
.configurationDisplayName("Today work")
.description("Show today work sessions")
.supportedFamilies([.systemSmall, .systemMedium])
}
}
On the SwiftUI Canvas preview, I can see the "Toto" text.
BUT when I select the Widget Target and run in the simulator, I only have the placeholder in place of text. Any idea of why? Note that not only the text is replaced by placegholder but also an Image(systemName:)
I think the issue seems to be related by IntentConfiguration (as everything works fine with StaticConfiguration)
I'm on xCode 14.
In my Case it helped to change the Simulator. The Bug is still in my case on an iPhone 11 Simulator and on a real iPhone 11.
With other Simulators it works well.
Maybe its an issue with an iPhone 11.

Pop back from NavigationLink in TabView crashes tvOS app

I'm trying to make a simple tvOS app. I was testing out some basic UI code and had the following:
struct ContentView: View {
var body: some View {
TabView {
NavigationView {
NavigationLink(destination: Text("Subview")) {
Text("Subview")
}
}
.tabItem {
Text("Main")
}
NavigationView {
Text("Settings")
}
.tabItem {
Text("Settings")
}
}
}
}
This launches the app correctly, and I can click the NavigationLink to open the Subview. But, when I press the back/menu button the app closes and the console prints out the following error:
[Common] Error response from snapshot request action of type 1 gave Error Domain=BSActionErrorDomain Code=1 "(null)"
I'm not quite sure where to go from here. From what I have seen, I believe the above code should work (it works fine on my iOS devices). Are there any modifiers I need to add to get this to work or do I need to change the structure of my View?
I am testing this code on my Apple TV 4K (2nd gen) on tvOS 15.0.

navigationBarTitle / displayMode is crashing on iOS 13.2 and 13.3

When loading this view, the app crashes on iOS 13.2 and 13.3 with an EXC_BAD_INSTRUCTION error:
struct HomeView: View {
var body: some View {
NavigationView {
ScrollView {
// Some content
}.navigationBarTitle(
Text("home.title"),
displayMode: .inline
)
}
}
}
Removing the navigationBarTitle part entirely, or just the displayMode will prevent the app from crashing. This looks similar to this question, but just upgrading xcode doesn't fix the problem.
Another interesting thing to note is that if I remove this line in my ContentView's init() it does not crash:
UINavigationBar.appearance().isTranslucent = false
It is fixed in later versions of iOS 13 and iOS 14, but I have users on older versions of iOS complaining. Do you have any idea of what the problem could be ?

SwiftUI. App crashes on iPad with iOS 13.4-13.7 when long-pressed

It's my first question on stackoverflow. Please correct me if I wrong with formatting topic.
I get crash of application when try use long press (?haptic touch?) on real iPad. Also it's reproducible on Simulators iPad with Force Touch click on Magic Trackpad.
Which devices have this bug?
Every iPad since iOS 13.4 until the last iOS 13.
Every Simulator iPad since iOS 13.4 until the last iOS 13.
I never saw this bug on iPhone.
My set up for testing: MacOS Catalina 10.15.6, xCode 11.7,
Simulator iPad mini 2019 5-gen iOS 13.7,
iPad 2018 6-gen iOS 13.7.
Upd 26.10.2020. Also, I try to use xCode 12.0.1 on MacOS Catalina 10.15.7,
iPad 2018 6-gen iOS 13.7. I retested, looks like the bug still available on iOS 13.4-13.7, but is gone for iOS 14. Good decision for iOS 13 I didn't find.
Steps to reproduce the bug.
I use SwiftUI view (TestCrashIPAD) with List, ForEach and dynamic sections (source code below).
Some static sections are predefined, some dynamic sections appear after fetching from server.
IMPORTANT the bug reproducible only when first appear view! Don't go to another screen or minimize the app!
Just open this view (TestCrashIPAD).
Wait until dynamic sections appers.
As soon as the sections appear, try long-press on the cell with text "r1" or "r2" or ... (screenshot)
Get crash (screenshot)
CRASH
I get crash on AppDelegate with:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
I investigated a little bit, and found: this bug reproducible since iOS 13.4. Also with iOS 13.4 there is a View Modifier onDrag (link). May be it's somehow connected, because in call stack I see:
UIDragInteractionLongPressDriver
UIGestureRecognizer
May be something wrong with dynamic sections array, because I see:
SwiftUI.Sections.rowIDs(forSectionAt: Swift.Int)
SwiftUI.ListCoreDataSource.rowIndex(at: Foundation.IndexPath)
Question
How to fix this issue? How to work around without crash in this case? Please, say any idea.
Source Code
Please have a look the source code:
import SwiftUI
struct TestCrashIPAD: View {
#State var sections = [TestSection]()
var body: some View {
List {
// predefined sections
Section(header: Text("Section predefined")) {
Text("1")
Text("2")
}
Section(header: Text("Section predefined")) {
ForEach(1..<7) {
Text("\($0)")
}
}
// dynamic content
ForEach(sections) { section in
Section(header: Text(section.title)) {
ForEach(section.rows) { row in
HStack {
Text(row.str)
Spacer()
}
}
}
}
}
.listStyle(GroupedListStyle())
.navigationBarTitle("iPad crash with long press")
.onAppear(perform: onAppearAction)
}
func onAppearAction() {
DispatchQueue.global().async {
// fetch some data from the server
sleep(3)
// show data in UI
DispatchQueue.main.async {
self.sections = [TestSection](TestSection.array)
}
}
}
}
struct TestSection: Identifiable {
let id = UUID()
let title: String
let rows: [TestRow]
static var array: [TestSection] {
var result = [TestSection]()
for idx in 0..<50 {
result.append(TestSection(title: "s\(idx)", rows: TestRow.array))
}
return result
}
}
struct TestRow: Identifiable {
let id = UUID()
let str: String
static var array: [TestRow] {
var result = [TestRow]()
for idx in 0..<Int.random(in: 3..<7) {
result.append(TestRow(str: "r\(idx)"))
}
return result
}
}
Looks like the answer is to add .onLongPressGesture { } to your lists that are crashing
(i got this from comments, but this resolved my issues, so seems like an answer. posting as an answer bc not everyone reads comments

Resources