xcode can't preview new SwiftUI project - ios

I'm running Catalina 10.15.7, and XCode 12.3.
New Project -> Single View App -> provide name (all alpha, no breaks or punctuation).
Project launches. Without making any changes whatever, I click on Resume Preview, and get a message: "Cannot preview in this file: Failed to launch
The code in ContentView.swift is standard, and confirmed in various videos etc.:
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
As I launched XCode, I was asked to upgrade certain unspecified components, which I did.
But no joy. What's the solution?

The answer was obviousish once found, which of course happened right after asking.
I had mistakenly created a project for macos, and I wanted a project for iOS. I'm pretty sure that most of you figured that out...

Related

iOS SwiftUI WidgetKit: My widgets don’t show up in the ‘Add Widgets’ section

I developed an app with 10 widgets but some iPhone, iPad or macOS users report that my widgets or the "App Name" don't appear in the widgets list when they tried to add the widgets of my app.
There's no way I can replicate the problem, but the number of the users that report this issue is growing up every day.
It’s not clear if the problem is caused by the iPhone model, a crash on the widget preview or other factors.
My widgets previews are built reading static data, so this data is the same in every conditions.
I suggest my users to do the following steps:
Start the app and go to check again if "App name" appears among widget List
Close every app and restart the device; then start the app and
go to check again if "App name" appears among widget List
Temporarily change system language and turn on Bold Text and go to check again if "App Name" appears among widget List
Uninstall and reinstall the app, start the app and go to check again if "App Name" appears among widget List
But all users who have tried these steps say that the app still does not appear in the list of widgets.
The following code is the one I use inside the main widgets swift file called TodayWidgetExtension:
import WidgetKit
import SwiftUI
struct PlaceholderView : View {
var body: some View {
Text("loading")
}
}
//MARK: - Main widget bundle configuration
#main
struct WidgetBundle: WidgetBundle {
#WidgetBundleBuilder
var body: some Widget {
//MARK: - 1
Widget1Large() //IntentConfiguration
Widget1Medium() //StaticConfiguration
Widget1Small() //StaticConfiguration
if #available(iOSApplicationExtension 16.0, *) {
Widget1Accessory() //StaticConfiguration
}
//MARK: - 2
Widget2Large() //StaticConfiguration
//MARK: - 3
#if !targetEnvironment(macCatalyst)
Widget3Medium() //StaticConfiguration
#endif
//MARK: - 4
Widget4Large() //StaticConfiguratio
//MARK: - 5
Widget5Medium() //IntentConfiguration
//MARK: - 6
Widget6Large() //IntentConfiguration
Widget6Medium() //IntentConfiguration
}
}
struct todaywidgetextension_Previews: PreviewProvider {
static var previews: some View {
PlaceholderView()
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
The only thing that I've noticed debugging the widgets extension is that, even if I don't add any of them to my iPhone screen, Xcode tells me that widgets occupied 20mb of memory (the limit for widgets is 30mb). I tried removing all the widgets from the above code leaving only one (Widget1Small for example), but Xcode continues to tell me that 20mb of memory are still occupied.
I hope that someone can help me.
Thank you.

Why is Xcode crashing when I try to preview?

Every time I am trying to resume my preview canvas in Xcode I get this annoying error. I tried restarting, moving the project to another location, and changing the preview device. The current project is a fresh one, just started building a view.
Strange thing is that when I run it on the iOS Simulator it works. The app builds successfully. I also noticed that this is only happening when I use source control with my project(GitHub). Not using it is not an option for me.
Let me know if I need to add the whole crash report, because it's very long.
Here is the full crash log
https://developer.apple.com/forums/content/attachment/5f8f5c96-7c1e-4eef-b0d7-ed59894a9c30
You didn't add your code so I can't help specifically but here are some of the reasons it happened to me in the past:
If you use an environment object on the view you're previewing and you don't add it to preview, it causes a crash:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
.environmentObject(ViewModel())
}
}
Sometimes I just add ZStack to it and it magically solves the problem:
struct MyView_Previews: PreviewProvider {
static var previews: some View {
ZStack {
MyView()
}
}
}
Sometimes It helps to clean the project (command + shift + K) and build again
Basically if your project works in simulator it means there's nothing wrong with it, it builds. The only problem is that you can't use the preview. You probably didn't set it right, it's missing something it needs in order to run properly. Try to figure it out. Or just run on simulator like we did before there was such a thing as previews..

“Cannot preview in this file - Connection interrupted: send previewInstances message to agent" error in Xcode 12

I've got a UIKit project and I'm slowly trying to move to SwiftUI, I created a SwiftUI file and I tried to preview it but I'm getting this error:
Cannot preview in this file - Connection interrupted: send previewInstances message to agent
This is my SwiftUI File:
import SwiftUI
struct LineChart: View {
var body: some View {
Text("Hello, World!")
}
}
struct LineChart_Previews: PreviewProvider {
static var previews: some View {
LineChart()
}
}
I only saw one similar question on StackOverFlow but that didn't help either, is there anyway that I could fix this? Appreciate the feedback!
Turning off "Automatically Refresh Canvas" worked for me. (Xcode 13.2.1)
Same error, seems that the automatically refresh canvas freeze the preview. Unchecking fixed the problem, but the whole sense of SwiftUI collapses without the automatic update.

SwiftUI TabView not responding

:-) Here comes a probably silly problem with a bit of a preamble sorry!
Long story short - I've been developing an app pulling my hair out due to a bug with the TabView in SwiftUI. I was overlaying the tabs with a ZLayer to add a funky big button in the middle - so when I was running into problems I thought this would be the issue... but this becomes irrelevant in a moment.
For simplicity I started a New project in XCode v11.6, build target iOS 13.6, Selected Tabbed App, created a very simple boilerplate SwiftUI content view with 2 tabs. (For clarity - I did not modify the default code at all). Runs perfectly on the simulator. Plug in my iPhone XS running iOS 13.6.1 (when I first encountered this problem I was on 13.5 so have updated iOS builds whilst this problem has occurred). Run the simple, non modified app on my phone. Loads up "First View", I tap the "Second" tab, nothing. No response at all. This is the same problem I am having in my more complicated app. Occasionally if I keep rebuilding. It will let me switch to "Second View" but then cannot switch back. The Tab Controller just seems to become non-responsive.
Now back to my more complicated app, on the first view there is a scroll view and there is a button with an attached actionSheet. If I pop open the action sheet, then dismiss it (regardless of just calling the .cancel() button or selecting an action), the Tab Controller works perfectly. There is another option which opens another sheet, again as soon as something has been overlayed, everything works as expected until the next app launch.
Tried resetting my MacBook, tried resetting my iPhone, tried uninstalling the build then rebuilding, cleaning the build folder, tried creating multiple projects with build targets iOS 13, 13.2, 13.5, 13.6. Problem seems to persist, but every time the simulator works perfectly.
So my questions here are: From my searching I can't find anyone with this problem. Is it just me really? Can anyone with XCode 11.6 and an iPhone XS please simply hit "New Project" -> "Tabbed App" and let me know if this works? (I'm almost wondering if there's a bug in XCode 11.6 when you create an app and everyone is either working on projects that were already created pre XCode 11.6 or already using the beta XCode... But really I am just out of ideas)
Also is there any deeper debugging one can do? Something like actually notifying me for every touch it receives and whether the App sandbox is getting the touch or the OS is (for some reason) keeping the tap gesture? I've never experienced something like this so I can't really see where to start debugging as I don't even know if the app is receiving the tap.
Or frankly - does anyone have any other ideas?
Just for clarity - ContentView looks like this: (Again it's straight from what XCode creates for a Tabbed App)
import SwiftUI
struct ContentView: View {
#State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thanks in advance and sorry for the long ramble - from a very sleepy dev at wits' end!

AttributeGraph: cycle detected through attribute using #State in playgrounds

I'm trying to use SwiftUI's #State in playground. Here's my code:
import UIKit
import PlaygroundSupport
import SwiftUI
struct ContentView: View {
#State private var showGreeting = false
var body: some View {
Form {
Button(action: {
self.showGreeting.toggle()
}) {
Text("Toggle Greeting")
}
if showGreeting {
Text("Hello World!")
}
}
}
}
let viewController = UIHostingController(rootView: ContentView())
PlaygroundPage.current.liveView = viewController
I can see the button on the live view. But if I click it, it doesn't show the text. A second click does reveal the text. Further clicks don't seem to make any difference visually.
On each click, I get the following message in log area:
=== AttributeGraph: cycle detected through attribute 38 ===
Need some help understanding what this means in this context and why the state is not behaving naturally.
Note: I'm running macOS Mojave, so I don't have option of SwiftUI previews. I'm making do with live preview of playgrounds.
There's nothing wrong with your SwiftUI, and it runs fine when pasted into a new project.
If you change the Form to a VStack, it works fine in the playground. Using a List gives you the same error. Maybe a bug, I'd report it to Apple.
In my experience, if you're stuck on Mojave for the time being, just set up a project where you can run the simulator. It takes just a couple seconds to compile and run, and you wind up saving time when your SwiftUI playgrounds crash, get weird errors like this, etc.

Resources