Xcode Previews

RSS for tag

Create an app UI and configure almost everything your users see using Xcode Previews.

Posts under Xcode Previews tag

113 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Why are asynchronous tasks not executed in Xcode Canvas, and why do they only work in the simulator?
Hi everyone, I'm new to Xcode and iOS development. I've encountered an issue where asynchronous tasks seem to not execute in Xcode Canvas, but they work fine in the simulator. Can anyone explain why this might be happening or what I could do to fix it? Thank you for your help! struct PartnerProfileView: View { @State private var showSheet: Bool = true let partnerName: String var body: some View { ZStack(alignment: .bottom) { Color("EFEFF4_0F2534") ScrollView(showsIndicators: false) { headerSection() infoSection() .padding(.bottom, 5) sectionTitle("Other Skills") skillsGrid() sectionTitle("···") dynamicsSection() } .frame(maxWidth: .infinity, maxHeight: .infinity) CustomTabBar() } .navigationBarHidden(true) .ignoresSafeArea(edges: .all) .onAppear { print("PartnerProfileView received") Task { await BottomCustomPopup().present() } } .onChange(of: showSheet) { oldState, newValue in print("oldState \(oldState)") print("newValue \(newValue)") } } } //@MainActor struct BottomCustomPopup: BottomPopup { var body: some View { HStack(spacing: 0) { Text("Hello World") Spacer() Button(action: { Task { await dismissLastPopup() }}) { Text("Dismiss") } } .padding(.vertical, 20) .padding(.leading, 24) .padding(.trailing, 16) } }
1
0
335
Jan ’25
Preview and bundle for modular app target
I have a dummy project that is set up in the following way: one Xcode project two frameworks, UI and Feature, Feature embeds UI-framework three app targets, two that are SwiftUI lifecycle and one UIKit lifecycle all app targets import the Feature framework all app targets have same setup for asset catalogue The point of this setup is to be able to switch between app target scheme and see preview changes accordingly. That works 100% with anything coming from the asset catalogue and even localization catalogues. The dummy project exists as a proof of concept for the main project (8 years old, all Swift) I am working on. However, the same setup does not work in the main project. I am suspecting it has to do with how the project is initialised and bundles. I print out the bundles to see which bundles are registered. Dummy project will print out the bundle based on app target, the main bundle being based on the selected scheme. The main project will have the previews.com.apple.PreviewAgent.iOS as main bundle.
2
0
349
Jan ’25
Cannot preview in this file, failed to launch
I am not able to use SwiftUI Preview Here is the report I get while trying to see the issue: Here is the report of the issue: And here is the call and code preview: import SwiftUI struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } #Preview { ContentView() } Please update.
2
0
711
Jan ’25
ASWebAuthenticationSession not working in Preview
class ViewModel : NSObject, ObservableObject, ASWebAuthenticationPresentationContextProviding { private var authSession: ASWebAuthenticationSession? func signInWithOpenID(provider: OAuthProvider) { let url = getOIDCAuthenticationURL(provider: provider) authSession?.cancel() authSession = nil authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "com.ninjanutri") { callbackURL, error in if let error = error { print("Error: \(error.localizedDescription)") return } guard let callbackURL = callbackURL else { return } guard let idToken = callbackURL.valueOf("id_token") else { return } self.signInWithIdToken(provider: provider, idToken: idToken) } authSession?.prefersEphemeralWebBrowserSession = false authSession?.presentationContextProvider = self authSession?.start() } public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { return ASPresentationAnchor() } } struct ContentView: View { @StateObject private var viewModel = ViewModel() var body: some View { Button { viewModel.signInWithOpenID(provider: .github) } label: { Text("Test") } } } when the prefersEphemeralWebBrowserSession is false, the alert and webview is totally working fine in Simulator and Real device, but not XCode Preview. Is this behaviour expected or it's a bug?
1
0
417
Dec ’24
[SwiftUI Preview] App crashes when previewing on physical device but runs normally
Hello everyone, I've encountered an issue with SwiftUI Preview and would appreciate your help. Problem Description In a mixed Objective-C and Swift project, I'm experiencing the following situation with SwiftUI Preview: Crashes when previewing on a physical device The same code runs normally when launched directly through Xcode Project uses CocoaPods for dependency management with static frameworks Project Environment: Project Type: Mixed (Objective-C + Swift) Dependency Management: CocoaPods Dependency Type: Static frameworks Device: Physical device preview Steps to Reproduce: Write Preview code in SwiftUI file Select physical device as Preview device Click Preview button Application crashes
3
0
530
Dec ’24
Preview Crashes with glitched visuals
When working on my Xcode project and using Previews from time to time I find preview crashes and ens up with the image below. The UI is completely unresponsive and only way to bring it back I find is close and relaunch Xcode. Anyone else experience this - is there a known fix or a better way to "kick" preview to restart that doesn't involve full relaunch of XCode
2
0
309
Dec ’24
XCPreviewAgent Crashed / Preview Not Working
My preview crashed because it could not access my font resources. I am working on a Framework, and it seems that for frameworks, the resources are not copied into the app package of the preview. Investigation: I verified this by examining the contents of the preview: /Users/YOUR_NAME/Library/Developer/Xcode/UserData/Previews/Simulator Devices/DEVICE_UUID/data/Containers/Bundle/Application/APPLICATION_UUID/APPLICATION_NAME.app Make sure to replace "YOUR_NAME," "DEVICE_UUID," "APPLICATION_UUID," and "APPLICATION_NAME" with the actual names or UUIDs relevant to your specific situation. Now right-click on the app and select Show Package Contents. For previews that work correctly, the name of the associated app is used, and you will see all the resources in the package. However, my framework was not using the framework name. Instead, it was named XCPreviewAgent.app and did not contain any resources from my framework. Fix: As of November 2024 (Xcode 16.0 and Xcode 16.1), the fix is to use Legacy Preview Execution. In the menu, select Editor > Canvas > Legacy Preview Execution. Please leave a comment if this is fixed in a newer Xcode version.
1
0
719
Nov ’24
CrashReportError: Fatal Error in PersistentModel.swift
I got a preview crash info as this: *** crashed due to fatalError in PersistentModel.swift at line 559. This KeyPath does not appear to relate RecordInfo to anything - \RecordInfo.<computed 0x00000001921dfb4e (Array<SomeInfo>)> If I change preview device to iPhoneSE, It is no crash. I think the reason is I have some dirty data in iPhone16 Pro preview device. If I'm right and how to delete it? Model of RecordInfo like this: @Model class RecordInfo { @Relationship(deleteRule: .cascade) var someInfo:[SomeInfo] } @Model class SomeInfo { var date: Date var info: String }
1
0
488
Nov ’24
Hi, I have an issue with app preview
I can't seem to access preview for any of my views in any of my swift projects at all (using Mac to code and using iPhone 16 pro as preview and simulation, simulation can work), even after restarting Mac and Xcode itself. The preview section has a popup saying "Cannot preview in this file, unexpected error occured" (for every file), please tell me what is wrong and help me solve it to see the preview, thank you! details
1
0
530
Nov ’24
Xcode simulator & preview not loading
Hello, I recently decided to start learning how to code for iOS. I don't have much coding experience but I still wanted to explore it for fun at least. I downloaded Xcode on my Macbook, and opened a new iOS file after downloading iOS 18.1 so I could run the simulator/get a preview of my code. Even though I only had the basic "Hello World!" that is auto-generated in my code, the preview would never show and sat at a loading screen for multiple hours, saying "Preparing (Automatic) iPhone Simulator" at the top. There is probably a simple solution that I'm missing. I would appreciate any tips! Thanks.
2
0
841
Dec ’24
VisionOS Simulator Stuck on Black Screen with "Hello World" Code in Xcode
Hello everyone, I’m currently developing my first VisionOS app in Xcode, starting with the default "Hello World" code provided when creating a new VisionOS Mixed Reality App. However, I’m facing some issues with performance and previewing that I can’t seem to resolve. When I load the preview, it takes an extremely long time, and sometimes it doesn’t load at all. Even when I try to run the app in the VisionOS Simulator, the simulator shows an endless black screen and never displays the intended view. I’ve made no changes to the code, so it’s purely the base setup. Here are my system details: Xcode version: 16.1, VisionOs 2.0; macOS version: 15.0.1; Hardware: MacBook Air 2020 M1 I’ve tried restarting Xcode and my machine, but the issue persists. Has anyone else faced similar problems or have any suggestions for fixing this? Or is my hardware simply too weak? Any help would be greatly appreciated! Thank you in advance!
1
0
376
Nov ’24
How to mock CBPeripheral data when develop with Core Bluetooth
I'm developing an app which supports for connect with an BLE device. There are some views for showing data from bluetooth. The code below for an example: @EnvironmentObject var bleManager: BLEManager // a class implemented CBCentralManagerDelegate var body: some View { // bleConnected is a CBPeripheral if let bleConnected = bleManager.bleConnected { if let services = bleConnected.services { ForEach(services, id:\.self) { service in // UI for print uuid, descriptions ... showServiceDescriptionView(services) if let characteristics = service.characteristics { ForEach(characteristics, id:\.self) { characteristic in // UI for print uuid, data for each characteristic showCharacteristicsDetailView(characteristic) } } } } } } } But it seems that Xcode Preview do not support connect to a real bluetooth device. So the preview window in Xcode will always be a empty view. I only can see the data on the real device. But it's important for me to design UI on the preview. So are there any method to mock a fake CBPeripheral in the Previewer? Or are there any other way to achieve this?
1
0
385
Nov ’24
Core Data boilerplate project causes Fatal Error
I'm using Xcode 16 and SwiftUI targeting iOS 18. I'm new to Core Data, and when I create a new project and select to use Core Data as storage, I get boilerplate code for it. The problem is that when I try to see the preview without any change to the code, I get a Fatal Error: CrashReportError: Fatal Error in Persistence.swift Test crashed due to fatalError in Persistence.swift at line 52. Unresolved error Error Domain=NSSQLiteErrorDomain Code=8 "(null)" UserInfo={NSFilePath=/Users/monni/Library/Developer/Xcode/UserData/Previews/Simulator Devices/D0D98B5B-7E6F-4DC3-B16A-34D6D2958558/data/Containers/Data/Application/A98879A6-46F5-4E29-B2D7-AD294F1EFFD0/Library/Application Support/Test.sqlite, NSSQLiteErrorDomain=8}, ["NSSQLiteErrorDomain": 8, "NSFilePath": /Users/monni/Library/Developer/Xcode/UserData/Previews/Simulator Devices/D0D98B5B-7E6F-4DC3-B16A-34D6D2958558/data/Containers/Data/Application/A98879A6-46F5-4E29-B2D7-AD294F1EFFD0/Library/Application Support/Test.sqlite] When I try to open the SQLite database there are no entities in it. I have also tried xcrun simctl --set previews delete all, but with no luck.
2
1
437
Oct ’24
SwiftUI Previews creating views out of thin air?
My iOS (iPhone/iPad/Mac Catalyst) app has a relatively complex authentication flow, primarily required due to it being an API client. Several of my views have .task modifiers that begin executing code when the views are created. After moving to iOS 18 and Xcode 16, when running in the Simulator, I began noticing my workflow was breaking as views were being created out of sequence... but not by me 🤯 Here is an example stack when I place a breakpoint inside of this view's .task. From my logging (and this stack) I can verify I am not creating this view. This view has no preview associated with it, and there are no previews up the view hierarchy from it either. This happens in the Simulator only The only clue I have to go on is this line of text which is present at the top of what looks like ASM when I click on the (5) line in the stack above. SwiftUI`(1) await resume partial function for dispatch thunk of static SwiftUI.PreviewModifier.makeSharedContext() async throws -> τ_0_0.Context: If this (or any hierarchical view) used Previews, I'd remove them to see if it had an effect, but they don't! I'm at a loss on what to do here...
8
0
777
Oct ’24