Default RealityKit application isn't working - ios

When running the generated Augmented Reality app with RealityKit from Xcode, nothing appears on the screen and I'm getting weird logs on the console:
2020-09-16 16:54:29.246883+0200 TestAR[494:29654] Metal GPU Frame Capture Enabled
2020-09-16 16:54:29.247203+0200 TestAR[494:29654] Metal API Validation Enabled
2020-09-16 16:54:29.792709+0200 TestAR[494:29654] Compiler failed to build request
2020-09-16 16:54:29.792957+0200 TestAR[494:29654] [Graphics] makeRenderPipelineState failed [output of type ushort is not compatible with a MTLPixelFormatR16Float color attachement.].
2020-09-16 16:54:29.792989+0200 TestAR[494:29654] [Graphics] makeRenderPipelineState failed.
I have tried to run the app on my two devices and the result is the same. Also tried to use Xcode 12, same result. What's even more disturbing is that I got the app to work like 2 times on the 20+ tries at random without changing anything, just killing the app and restarting or closing xcode and restaring.
Here is the code, but this is just the basic generated code:
struct ContentView : View {
var body: some View {
return ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
At this point I don't really understand what I'm doing wrong, here are the additionnal informations:
macOS Catalina v10.15.5
iPhone X 13.5.1
iPhone X 13.7
iPhone 6S Plus 13.5.1
Xcode 11.7
Xcode 12

At first you have to make sure that you didn't change a content in AppDelegate.swift file. A window's root view controller must come from contentView:
let contentView = ContentView()
window.rootViewController = UIHostingController(rootView: contentView)
window.makeKeyAndVisible()
Solution I
Sometimes in Xcode you may observe a bug – your app can't be launched or run due to some errors found in intermediate compiled data folder. All you need to do is to empty a DerivedData directory.
For that in Finder just press a shortcut Cmd-Shift-G and specify the following path to get to desired directory:
~/Library/Developer/Xcode/DerivedData/
After the deletion of a content of DerivedData directory your compiled app must be working as expected.
Additional info about DerivedData:
DerivedData is a folder located in ~/Library/Developer/Xcode/DerivedData by default. It’s the location where Xcode stores all kinds of intermediate build results, generated indexes, etc. DerivedData location can be configured in Xcode preferences (Locations tab).
Solution II
If the above two steps didn't help, you should reinstall Xcode.

Related

PDFView causes app to freeze on iOS 16, only when autoscales = true

I am using a PDFView to display images in my app (built using SwifUI), simply for quick and easy pinch-to-zoom functionality. This worked perfectly in iOS 15, but since updating to iOS 16, the app freezes when attempting to load the image viewer (PhotoDetailView below). The issue persists across both the simulator and a physical device.
Here is the code I'm using:
import SwiftUI
import PDFKit
struct PhotoDetailView: UIViewRepresentable {
let image: UIImage
func makeUIView(context: Context) -> PDFView {
let view = PDFView()
view.document = PDFDocument()
guard let page = PDFPage(image: image) else { return view }
view.document?.insert(page, at: 0)
view.autoScales = true
return view
}
func updateUIView(_ uiView: PDFView, context: Context) {
// empty
}
}
When I run the code on iOS 16.0 in the simulator, I get 2 errors in the console:
[Assert] -[UIScrollView _clampedZoomScale:allowRubberbanding:]: Must be called with non-zero scale
[Unknown process name] CGAffineTransformInvert: singular matrix.
I have been able to isolate the issue to view.autoscales = true. If I print view.scaleFactor, I can see that it is 1.0 before the autoscale and 0.0 afterward (which is what appears to be prompting the errors). These errors also show up in the console when using iOS 15 in the simulator, but the images load as expected.
When view.autoscales = true is commented out, the image loads, albeit at a size that is much larger than the device screen.
Does anyone have any idea what may be causing this? I'd really like to avoid having to build a custom viewer, since I'm just trying to let users quickly pinch to zoom on images.
I managed to resolve this issue. I'm posting my solution here, in case anyone else runs into the same type of problem.
The issue only occurred when using a NavigationLink to navigate to PhotoDetailView. This led me to look more closely at my navigation stack.
After some digging, I found that the problem was related to my use of .navigationViewStyle(.stack) on the NavigationView. I needed this modifier to get things to display correctly on iOS 15, but the same modifier was breaking my image viewer with iOS 16.
My solution was to create a custom container to replace NavigationView, which conditionally uses NavigationStack for iOS 16, or NavigationView + .navigationViewStyle(.stack) for iOS 15. That worked like a charm and my image viewer is back in action.
I found inspiration for my custom container here: https://developer.apple.com/forums/thread/710377

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..

swift ios macCatalyst app does not load some views on startup on macOS Monterey

since the release of the new macOS Moterey operating system (12) my MacCatalyst application does not load some views at startup until I resize the window or change focus.
If I look at XCode's Debug View Hierarchy these views are not present.
If, on the other hand, I use the classic debug (while they are not shown) it turns out that they have the correct frame (origin and size), superviews and window.
To show them I have to resize the window (manually, using the mouse) or remove the focus from the app (for example by clicking on another window and then clicking the app window again).
Only after doing one of these two things are the views loaded.
Has anyone experienced the same behavior?
The application is developed in Swift only with UIKit and various Storyboards (without SwiftUI).
Thanks a lot to everyone.
I understand what happens. 
If it can be useful to anyone ... 
I use custom margins for my UIViewControllers. To calculate a margin, I use the minimum width of a scene as a variable.
I take this value in this way: 
var minSceneWidth:CGFloat = 400
scenes.forEach { windowScene in
if let w = windowScene.sizeRestrictions?.minimumSize.width {
if minSceneWidth > w {
minSceneWidth = w
}
}
}
Since the latest version of macOS the value "minimumSize.width" seems to be also "0" and this is not good for the calculation of my margins. 
Thank you all.

SwiftUI Crash LayoutComputer.EngineDelegate.sizeThatFits?

I can't identify the source of this crash, and when I "Open in Project" Xcode doesn't point to any particular area of my code. What could be causing this? It seems to be when a certain button is tapped.
Solution
After I ran into this same sort of problem, the solution for me, was to clean my build folder, then perform a full build of my application for my target device, as opposed to not cleaning my build folder, and just performing an incremental build of my app.
Steps I followed to trigger this bug, and to resolve my issue
I had an Xcode Project, containing:
A Swift Package I'd added as a "Local Swift Package" which contained:
A module that defined a public method (let's call it modifyListRow) in an extension of the View protocol.
My SwiftUI Application for iOS which contained:
My #main App which rendered a SwiftUI View that contained:
List { ForEach { MyRow.modifyListRow() } }
I then did a full build & run of my app - which depended on that Local Swift Package, and after the app launched on my physical iPhone X device, I saw that everything worked properly.
I then modified the body of the modifyListRow method, in such a way that it would affect how the sizes of the List-rows produced by the ForEach View should be calculated by the SwiftUI Framework.
I then did an incremental build and run of my app.
This time, after my app launched on my iPhone, I just saw a blank screen in it because SwiftUI had failed to render any of my app's views, and I saw in Xcode that my app had crashed while attempting to calculate a sizeThatFits for a view - presumably for one of the List-rows produced by my ForEach View.
I then cleaned my build folder, and did a full build & run of my project, and after the app launched on my iPhone, I saw that this time everything worked properly.
Hypothesis of cause of bug for my app
Presumably, Xcode had cached the compiled code for my SwiftUI View that contained the List after I did the first full build, but didn't invalidate the compiled code for that View, despite the fact I'd modified the body of the modifyListRow method in such a way that the List-rows would have needed to have their sizes calculated differently by the SwiftUI Framework. This made it so that my SwiftUI View would not be re-compiled during the incremental build I'd performed, despite the fact the modifyListRow method was re-compiled. Consequently, the compiled code of my SwiftUI View and the modifyListRow method became "out-of-sync" in the build artefact, and hence, the SwiftUI framework failed to calculate the sizes of the List-rows. But cleaning my build folder, and performing a full-build, made it so that my SwiftUI View would be re-compiled, along with the modifyListRow method, and hence, the compiled code for my SwiftUI View and the modifyListRow method resumed being "in-sync" in the build artefact just as they originally had been. This made it so that the SwiftUI Framework would be able to successfully calculate the sizes of the List-rows.
But please remember, this is just my hypothesis as to the cause of the bug for my app - it may be incorrect.

Cannot print to console in Xcode

I am new to Xcode, macOS development etc.. So maybe its just because I am new - but I could not make a simple printout to console work with all effort.
I created a minimum nonworking example of my problem:
import SwiftUI
struct Test: View {
#State var message = "Test"
var body: some View {
Button(action: {
print("test worked")
message = "test worked"
}) {
Text(message)
}
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
When executing this the Button text changes, but nothing appears in console.
I tried by following this tutorial, but it did not work with it either: https://www.hackingwithswift.com/read/18/2/basic-swift-debugging-using-print
I tried by enabling debug preview according to this thread: How to print() to Xcode console in SwiftUI?
And I tried by enabling this setting:
Nothing helps.. I am using Xcode Version 12.1 (12A7403) btw.
You cannot print to the console from a SwiftUI preview.
The only possibility for outputting debug info in a preview is to display your logs in a Text (or any other UI element) that's displayed in your Preview.
However, if you need proper debugging, run the full app, don't use previews. Previews are great for initial wireframes, but once you get to the stage where you need debugging, switch to using the view in your app and running that on the Simulator (or a real device) rather than using the preview.
I have faced the same problem nothing shows in console.This helps me. Make sure you mark right side button to see the debug console.
It's possible to debug SwiftUI previews and print in the console without launching the app on a device or simulator. From the canvas, make sure to click on "Debug preview". More info can be found on Apple website.
Xcode 12
Long press on the Live Preview button, then click on Debug Preview.
Xcode 11
Right-click (or Control-click) on the Live Preview button in the bottom right corner of the preview.
I'm also having the same problem. I think it's a bug. I am pretty sure it was working on some previous xcode versions.
Anyway as a workaround you can just set a breakpoint on the line where you 'd like to log something and manually set a debugger command action like you can see in the picture.
Don't forget to tick the checkbox if you want to just print the log without actually stopping.

Resources