SwiftUI Canvas is blank or has crashed? - swiftui

My SwiftUI Canvas has disappeared. Has it crashed? I don't see any output in the console.
I have macOS 10.15 Catalina (the requirement for SwiftUI) and Xcode 11+.

Solution 1
If your SwiftUI view were named "TestView," the following would be necessary to display the Canvas:
#if DEBUG
struct TestView_Previews : PreviewProvider {
static var previews: some View {
TestView()
}
}
#endif
Simply copy the above code and change the instances of "TestView" to whatever your file is called. Click "Resume" and the canvas should load up.
Solution 2
If the above did not work, try to clear the derived data from the machine by first closing Xcode, then navigating in Finder to
~/Library/Developer/Xcode/DerivedData
Drag all of the files to the trash and restart Xcode. Don't worry, none of your code will be deleted, this is just the data that Xcode creates to help it to compile your project faster.

Related

Conditionally add commands to SwiftUI WindowGroup

With macOS 12 Apple fixed the omission of „Continuity Camera“ in SwiftUI by introducing the command group ImportFromDevicesCommands which can simply be added to a window.
But when you try to use it in an app supporting macOS 11 and 12, you are missing the conditionality support as it is common when combining SwiftUI views.
I have tried the following:
import SwiftUI
#main
struct ThrowAwayApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
if #available(macOS 12, *) {
ImportFromDevicesCommands()
}
}
}
}
But the compiler brings up the following error message:
Closure containing control flow statement cannot be used with result builder 'CommandsBuilder'
I understand the CommandsBuilder does not support buildIf(), buildEither(first:) and buildEither(second:) and is therefore missing the if-block support as for SwiftUI views.
Is there a way to conditionally add commands to the WindowGroup?
Can I do some trickery with #available? (I'm really missing the #unavailable...)
I've come up with a solution which allows me to check whether code is compiled for macOS 11 or macOS 12 by defining derived User-Defined build configuration in Xcodes project settings:
TARGET_MAJOR = $(SUPPORTED_PLATFORMS:upper)$(MACOSX_DEPLOYMENT_TARGET:base)
And then referring to this variable in the Other Swift Flags
OTHER_SWIFT_FLAGS = -DTARGET_$(TARGET_MAJOR)
Afterwards I can conditionally compile code for macOS 12 by using:
WindowGroup {
ContentView()
}
.commands {
#if TARGET_MACOSX12
ImportFromDevicesCommands()
#elseif TARGET_MACOSX11
#warning("ImportFromDevicesCommands not implemented")
#endif
}

PreviewDevice not showing up in preview

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice(PreviewDevice(rawValue: "iPhone 11"))
}
}
However, the device is not shown in the preview. I ran "xcrun simctl list devicetypes" in terminal which does show the simulator. Is this normal behavior or am I missing something. The goal is that I want to show how the app looks with the iPhone resolution. I am using xCode 13.
I have meet same issue at Xcode Version 14.1 (14B47b).
In my case ,this is because an unavailable simulator model has been selected like "iPhone 11",Xcode automatically chooses a available preview device based on your currently selected run destination.
Like this:
(selected iPhone SE2 but automatically chooses iPhone14 Pro)
Press control and option, click previewDevice, and then select an available model in the inspector can fix it:
(iPhone 14 selected, preview shows as expected)
If there is no model you want in these options, you can add it in the menu Window -> Devices and Simulators first.
Hope this helps you.

SwiftUI iOS 14 beta TextField 100% CPU

Using iOS 14 and Xcode 12.0 beta 6 if I try and use a simple TextField anywhere
import SwiftUI
struct ContentView: View {
#State private var name: String = "Tim"
var body: some View {
VStack {
TextField("Enter your name", text: $name)
Text("Hello, \(name)!")
}
}
}
the keyboard opens but then the CPU goes to 99%/100% and app is frozen.
Is this a known issue? How do I fix it?
This bug exists since the 14.0 betas and has not been fixed so far :/ I tried to search for workarounds or solutions but there seems to be none at the moment.
Once the user activates an input field, the CPU goes to 95%-100% and stays there until you actually quit the app.
I found some reason, If you use some .onAppear listener, When device keyboard is opened, application is being crazy if you set or change any #EnvironmentObject variable using .onAppear listener anywhere on your app. But it is not for all .onAppear... it was really weird. I searched piece by piece those when I have noticed.

SwiftUI warning: `Attempting -[UIContextMenuInteraction dismissMenu], when not in an active state`

Using Xcode 12 beta (12A6159) on macOS Catalina 10.15.5 (19F101) when I press "Back" button in NavigationView, navigating back from a pushed View, I see this warning in the console.
[UIContextMenuInteraction] Attempting -[UIContextMenuInteraction
dismissMenu], when not in an active state. This is a client error most
often caused by calling dismiss more than once during a given
lifecycle. (<_UIVariableGestureContextMenuInteraction:
0x6000037689a0>)
I did not get this warning on Xcode 11.5.
The code is dead simple:
var body: some View {
NavigationView {
NavigationLink(destination: gameScreen) {
Text("Start game")
}
}
}
After having navigated to gameScreen and then pressing the "Back" button in the NavigationBar I see the logged warning in the console.
I have not upgraded to any of the new SwiftUI stuff like App or SceneBuilder etc...

What is the equivalent of WKInterfaceController.didAppear() in SwiftUI on watchOS?

I'm building an Apple Watch app, and there is code I want to run every time the app is brought to the foreground.
Previously, if I wanted to do this in a watchOS with a WKInterfaceController, I would put this code in didAppear().
In SwiftUI, there is onAppear(), but when I call that on watchOS it only seems to be called the first time the app loads up, so it behaves like WKInterfaceController.willActivate() instead. The app has just a single view.
If onAppear() is the equivalent of WKInterfaceController.willActivate(), is there a different SwiftUI function that is the equivalent of WKInterfaceController.didAppear()?
Here's my current example code:
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello World").font(.footnote)
.onAppear {
print("onAppear called")
}
}
}
In the meantime, I am going to experiment with triggering what I need to do within the ExtensionDelegate, but I'm just trying to learn my way around SwiftUI on WatchOS, so knowing the answer to this would be helpful in the future.