Hello!
My app supports iOS and visionOS in a single target. But when I preview the app on iOS device/simulator, an error occurred:
The RealityKitContent.rkassets is located in my RealityKitContent package that is linked on visionOS.
It seems that Xcode Preview is ignoring the link settings and attempt to build RealityKitContent.rkassets on iOS.
Reproduce Steps:
Clone my demo project at https://github.com/gongzhang/rkassets-preview-issue-demo
Build the app for iOS (success)
Preview ContentView.swift (failed due to RealityKitContent.rkassets issue)
Xcode Previews
RSS for tagCreate an app UI and configure almost everything your users see using Xcode Previews.
Posts under Xcode Previews tag
153 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
I've made a small reproducing example app to demonstrate this issue. Just create an iOS App project and replace the App and ContentView files with the following code.
The app works as expected when running it in a simulator or on a device but the SwiftUI Preview will get stuck showing only the loading state, even though the print statement in the View's body has printed viewModel.state: showingContent(SwiftUITest.ViewModel.Content(text: "Hello world!", reloadButtonTitle: "Reload"))to the console.
When stuck showing the loading state (an animated ProgressView), If I change .padding(.all, 12) to e.g. .padding(.all, 2) or vice versa, then that will make the Preview render the expected content.
Also, if I tap the Reload-button, it will not show the ProgressView for 2 seconds as expected (and as the app will do when run in a simulator or on a device), instead it will just show a white screen, and the same workaround (changing the padding amount) can be used to make the it render the expected content.
Can other people reproduce this behavior, is it a known bug, or am I doing something wrong?
TestApp.swift
import SwiftUI
@main
struct SwiftUITestApp: App {
var body: some Scene {
WindowGroup {
ContentView(viewModel: ViewModel(
contentLoader: {
try! await Task.sleep(nanoseconds: 2_000_000_000)
return .init(text: "Hello world!", reloadButtonTitle: "Reload")
}
))
}
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@StateObject var viewModel: ViewModel
var body: some View {
let _ = print("viewModel.state:", viewModel.state)
Group {
switch viewModel.state {
case .notStarted, .loading:
ProgressView()
case .showingContent(let content):
VStack {
Text(content.text)
.padding(.all, 12)
Button(content.reloadButtonTitle) {
viewModel.handle(event: .reloadButtonWasTapped)
}
}
}
}
.onAppear {
viewModel.handle(event: .viewDidAppear)
}
}
}
// MARK: - ViewModel
@MainActor
final class ViewModel: ObservableObject {
@Published var state: State = .notStarted
let contentLoader: () async -> Content
init(contentLoader: @escaping () async -> Content) {
self.contentLoader = contentLoader
}
func handle(event: Event) {
switch state {
case .notStarted:
if event == .viewDidAppear { loadContent() }
case .loading:
break
case .showingContent:
if event == .reloadButtonWasTapped { loadContent() }
}
}
func loadContent() {
guard state != .loading else { return }
state = .loading
Task {
print("starts loading", Date.now)
let content = await contentLoader()
print("finished loading", Date.now)
state = .showingContent(content)
}
}
enum State: Equatable {
case notStarted
case loading
case showingContent(Content)
}
struct Content: Equatable {
let text: String
let reloadButtonTitle: String
}
enum Event: Equatable {
case viewDidAppear
case reloadButtonWasTapped
}
}
// MARK: - Preview
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(viewModel: ViewModel(
contentLoader: {
try! await Task.sleep(nanoseconds: 2_000_000_000)
return .init(text: "Hello world!", reloadButtonTitle: "Reload")
}
))
}
}
Here's the simple behavior of the app (recorded from simulator):
Each time the view appears, it loads it's content for two seconds while showing a ProgressView, then it shows the content, which includes a Reload button, and if you tap that button it will reload the content for 2 seconds again.
I would expect the Preview to behave in the same way.
Repro steps: Create new project
Create WidgetKit extension (verify that previews work)
Create dummy framework (verify that previews still work)
Link dummy framework to the widget extension
At that point, preview stops working with the following error:
"RemoteHumanReadableError
ProcessError: Failed to launch widget extension: The operation couldn’t be completed. (CHSErrorDomain error 1050.)"
What can I do to make this work?
I have a SwiftUI view that is being passed a binding from a parent view. I would like to preview the subview, but can't. I tried .constant and a @Model object directly. Both ways crash the preview.
Any idea on how I can get this to work properly?
@Model
class FamilyMember: Identifiable {
@Attribute(.unique) var id: String { name }
var name = ""
init(name: String = "") {
self.name = name
}
}
struct MemberView: View {
@Bindable var member: FamilyMember
var body: some View {
Text(member.name)
}
}
#Preview {
MemberView(member: FamilyMember(name: "Family member"))
}
#Preview {
MemberView(member: .constant(FamilyMember(name: "Family member")))
}
The the enhancements to Xcode Previews in Xcode 14 are very helpful.
I just had a situation where I wanted to take a screen capture of a preview of a view. Is there a built-in way to do that from Xcode, similar to taking a screen capture of the simulator screen?
I realize I can just use a system-level screen capture, but then I have to adjust the size of what I capture to the area of the view in the preview.
It would be great to just be able to have a button or shortcut and have the correct area captured.
Recently have been working on this project just fine... had an app throw a warning for some malware called InstallCore. Removed the malware and now I am getting this on every preview, regardless of the device preview type.
I've tried clearing derived data, cache, etc. No dice.
Could not install the preview host "AppName.app" on iPhone 14 Pro Max: Failed to get parent identifier for BUNDLEIDENTIFIER.
Greetings...
I am trying to use @Namespace for my matchedGeometryEffect use case.
prior to Xcode 15 beta the following code worked just fine:
struct ChapterItem_Previews: PreviewProvider {
@Namespace static var namespace
static var previews: some View {
ChapterItem(namespace: namespace, show: .constant(true))
}
}
However trying to do the same within the new Xcode 15 beta #Preview Macro
#Preview {
@Namespace var namespace
ChapterItem(namespace: namespace, show: .constant(true))
}
produces the following error message:
Ambiguous use of 'Preview(_:traits:body:)'
May I kindly get assistance on the proper way I can get this to work in Xcode 15 beta?
Please be as detail as you can since I'm still new to swiftUI as well Thank You.
I have a Mac app in Xcode using SwiftUI.
Recently I noticed the Xcode's previews had become very sluggish, and after some sleuthing I discovered that was because when the previews were running, it would spawn a new instance of the app in the background for every keystroke, and never quits any of these instances.
Here's a video where you can see the problem: https://static.squares.tv/Xcode%20Spawning%20720p.mov
It's a document-based app where I'm doing a lot of Metal rendering, using different audio and video frameworks and extensions with custom networking so it's possible something is blocking the app's ability to quit easily.
How do I troubleshoot this problem to figure out exactly what is preventing Xcode from operating cleanly here?
XCode Previews do not work when defined in a Swift Package in XCode 15 beta 6. The following error will be returned when trying to show a SwiftUI Preview:
FailedToInstallAppError: Failed to install ”XCPreviewAgent.app”
Could not install the preview host ”XCPreviewAgent.app” on iPhone 14 Pro
...
HumanReadableNSError: Simulator device returned an error for the requested operation.
Ensure your bundle contains a valid CFBundleShortVersionString.
As far as I know, a Swift Package doesn't need a CFBundleShortVersionString declared? Am I missing something?
Replicate Error
To replicate the error, simply create a new SPM package from XCode, and create a Preview:
struct Preview: PreviewProvider {
static var previews: some View {
Group {
Text("Hello")
}
}
}
Does anyone know of any workaround this issue?
A similar request was posted last year and the response was to request a generated report.
This is unacceptable; we cannot preview our app reliably because we have to use a hybrid build because you insist on releasing framework updates piecemeal and NEVER back-porting them. So no, "generating a report" for you to fix whatever the delay is, will not work for us.
PLEASE..
Give us a way to increase this timeout value in the project. You put us into this mess, give us a way to get out of it without having to wait on Apple engineers to "analyze" it.
Any help with preview crash on an App that is building just fine and runs on simulator fine?
Are there logs that I can access somehow?
The previews were working just fine, but then stopped and I have reverted all changes since and the previews are still broken.
If I have the code below and do the following with preview on:
Choose one of the TextEditors and start typing: works
Go to another one. Start typing, the text shows only in the first one you chose.
Try to delete the text while on the second (or third, or fourth) and you can't.
Try to delete the text in the first one. It works. >.<
If you check the preview console, you will see that the first variable is used forever.
Is anyone having the same problem? So annoying having to run everything every single time.
I'm running the Xcode on Sonoma 14.0.
import SwiftUI
struct TextStruct {
var text1: String = "text1"
var text2: String = "text2"
}
struct TextSummaryEditorView: View {
@State private var textForSummary: String = "textForSummary"
@State private var boomshakalaka: String = "boomshakalaka"
@State private var textstruct: TextStruct = TextStruct(text1: "boo1", text2: "boo2")
var body: some View {
VStack {
VStack {
Text(textForSummary)
TextEditor(text: $textForSummary)
.textFieldStyle(PlainTextFieldStyle())
.font(.system(size: Config.defaultFontSize))
.padding()
.background(Color("background").opacity(0.9))
.lineSpacing(10)
.zIndex(1)
.multilineTextAlignment(.leading)
Text(boomshakalaka)
TextEditor(text: $boomshakalaka)
.textFieldStyle(PlainTextFieldStyle())
.font(.system(size: Config.defaultFontSize))
.padding()
.background(Color("background").opacity(0.9))
.zIndex(0.9)
Text(textstruct.text1)
TextEditor(text: $textstruct.text1)
.textFieldStyle(PlainTextFieldStyle())
.font(.system(size: Config.defaultFontSize))
.padding()
.background(Color("background").opacity(0.9))
.lineSpacing(10)
.zIndex(1)
.multilineTextAlignment(.leading)
Text(textstruct.text2)
TextEditor(text: $textstruct.text2)
.textFieldStyle(PlainTextFieldStyle())
.font(.system(size: Config.defaultFontSize))
.padding()
.background(Color("background").opacity(0.9))
.zIndex(0.9)
}
.cornerRadius(Config.radioL)
.onChange(of: boomshakalaka) {
print(boomshakalaka)
}
Text("Button")
}
}
}
struct TextSummaryEditorView_Previews: PreviewProvider {
static var previews: some View {
VStack {
TextSummaryEditorView()
}
.padding()
.background(Color("background").opacity(0.4))
}
}```
Xcode: 14.3
Cocopods: 1.11.3
SwiftUI file Previews error for Xcode 14.3 , and my project is written in OC and Swift
The following is the detailed error information, what may be the cause?
Thanks a lot!
= PREVIEW UPDATE ERROR:
HumanReadableSwiftError
ProviderError: ProviderError: noPreviewInfos(runDestination: Simulator - iOS 16.4 | iphonesimulator | arm64 | iPhone 14 Pro Max | no proxy, thunkConfiguration: <IDEBuildOperationConfiguration: 0x6000cc5510a0; buildParameters=<IDEBuildParameters:0x600070e4a580:action=build:command=<IDEConcretePrimitiveSchemeCommand:0x60000629abb0:Run>:config=Debug:dest=iPhone 14 Pro Max:arch=arm64:workspaceArena=<IDEWorkspaceArenaSnapshot: 0x60008415fe00>:
overridingProperties=(
{ // Preview overrides (immutable)
DSTROOT = 【𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝗦𝘁𝗿𝗶𝗻𝗴:/Users/wj/Library/Developer/Xcode/DerivedData/MyApp-gljivierkfnnqydoswlgudwodyeg/Build/Intermediates.noindex/Previews/MyApp/ReleaseProducts】
ENABLE_PREVIEWS = 【YES】
OBJROOT = 【𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝗦𝘁𝗿𝗶𝗻𝗴:/Users/wj/Library/Developer/Xcode/DerivedData/MyApp-gljivierkfnnqydoswlgudwodyeg/Build/Intermediates.noindex/Previews/MyApp/Intermediates.noindex】
SYMROOT = 【𝗟𝗶𝘁𝗲𝗿𝗮𝗹𝗦𝘁𝗿𝗶𝗻𝗴:/Users/wj/Library/Developer/Xcode/DerivedData/MyApp-gljivierkfnnqydoswlgudwodyeg/Build/Intermediates.noindex/Previews/MyApp/Products】
},
):
collectBuildTimeStatistics=NO
collectTimelineMetrics=NO>:executionEnvironment=<IDEExecutionEnvironment: 0x2cf4c0550>:singleFileToBuild=(null):buildLog=(null):parallelizeBuildables=YES:dontActuallyRunCommands=NO:buildImplicitDependencies=YES:skipDependencies=NO:restorePersistedBuildResults=YES:enableIndexBuildArena=NO:buildables=(
"<Xcode3TargetProduct:0x60010c4472a0:MyApp.app blueprint:<Xcode3Target:0x2ee0b7860:MyApp>>"
):subsetOfBuildables=(null):buildDescriptionID=(null):scheme=<IDEScheme:0x461aeff20:'MyApp'>:schemeTask=BuildAndCommand:clientInfo=(null)>, blueprintName: TaskActivity, sourceFilePath: /Users/wj/Desktop/international/MyApp/DependencesLib/MyAppLib/TaskActivity/TaskActivity/View/InviteFriend/InviteFriendView.swift, thunkVariantSuffix: __XCPREVIEW_THUNKSUFFIX__)