Xcode Previews

RSS for tag

Create 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

Live Preview Not Working for UIKit Unless I raise Deployment Target to iOS 17
In Xcode 15 Live Previews are available for UIKit view controllers and views. However I noticed they do not work if the deployment target is < 17.0. If I try to make a live preview for a view controller like so: #Preview { let someVC = ViewController() return someVC } It doesn't load. The error is described below: == PREVIEW UPDATE ERROR: CompileDylibError: Failed to build ASwiftViewController.swift Compiling failed: module 'SwiftUI' has no member named 'VStack' If I raise the deployment target to iOS 17 it starts working.
3
2
2.1k
Oct ’23
Please Help!Xcode15 Archive has error:Command PhaseScriptExecution failed with a nonzero exit code
Hello, everything worked fine when I packaged my APP with the old version of Xcode, but I started reporting this error when I archived the package with Xcode15. I hope to get your help, thank you. error: Build target App of project App with configuration Release PhaseScriptExecution [CP]\ Embed\ Pods\ Frameworks /Users/wang/Library/Developer/Xcode/DerivedData/App-dcarpmhimruyzxfuqacwgyqbpwsy/Build/Intermediates.noindex/ArchiveIntermediates/App/IntermediateBuildFilesPath/App.build/Release-iphoneos/App.build/Script-9592DBEFFC6D2A0C8D5DEB22.sh (in target 'App' from project 'App') cd /Users/wang/Documents/new-bn-app/bn-app/ios/App /bin/sh -c /Users/wang/Library/Developer/Xcode/DerivedData/App-dcarpmhimruyzxfuqacwgyqbpwsy/Build/Intermediates.noindex/ArchiveIntermediates/App/IntermediateBuildFilesPath/App.build/Release-iphoneos/App.build/Script-9592DBEFFC6D2A0C8D5DEB22.sh mkdir -p /Users/wang/Library/Developer/Xcode/DerivedData/App-dcarpmhimruyzxfuqacwgyqbpwsy/Build/Intermediates.noindex/ArchiveIntermediates/App/BuildProductsPath/Release-iphoneos/App.app/Frameworks Symlinked... rsync --delete -av --filter P .*.?????? --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" --filter "- Headers" --filter "- PrivateHeaders" --filter "- Modules" "../../../IntermediateBuildFilesPath/UninstalledProducts/iphoneos/Capacitor.framework" "/Users/wang/Library/Developer/Xcode/DerivedData/App-dcarpmhimruyzxfuqacwgyqbpwsy/Build/Intermediates.noindex/ArchiveIntermediates/App/InstallationBuildProductsLocation/Applications/App.app/Frameworks" building file list ... rsync: link_stat "/Users/wang/Documents/new-bn-app/bn-app/ios/App/../../../IntermediateBuildFilesPath/UninstalledProducts/iphoneos/Capacitor.framework" failed: No such file or directory (2) done sent 29 bytes received 20 bytes 98.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files could not be transferred (code 23) at /AppleInternal/Library/BuildRoots/d9889869-120b-11ee-b796-7a03568b17ac/Library/Caches/com.apple.xbs/Sources/rsync/rsync/main.c(996) [sender=2.6.9] Command PhaseScriptExecution failed with a nonzero exit code
3
0
1.3k
Oct ’23
Xcode 15 Previews not working for watchOS apps
I'm using Xcode 15 15A240d Similarly to https://developer.apple.com/forums/thread/731732 when I try to run the Xcode previews for watchOS views in a watchOS target I get compilation errors from UIKit code. This is only valid for Xcode Previews. Building the code normally / Run it on simulator or device works. I don't understand why Xcode attempts to build iOS code. my scheme does include the host app for the watch I made a duplicate of that scheme by unchecking the host app and that makes previews work again. I'm not sure this is the intended behavior. I double checked and this was not the case with Xcode 14.3.1 I could run the initial target without issues on Xcode previews. Do you experience the same issue? is this a problem with my scheme settings or an Xcode bug?
0
0
519
Oct ’23
Xcode preview selectable mode real click/tap
When I set the preview mode to selectable the app is restarted at the home screen. However, I want really to look at a secondary screen reached by clicking a navigation link icon on the main screen. In this mode clicking the icon selects it but doesn't activate it. This seems like a catch 22. How could I get to the secondary screen in selectable mode? Is there a key/click combination that does a real click in selectable mode so I can get to it?
2
0
366
Oct ’23
#Preview unable to see other generated Swift Macro code
I have recently created a very simple Swift Macro that I can attach to a view that will generate the boilerplate we have in the project for passing in the view model instance. @TPView&lt;CompanyProfileViewModel&gt; struct CompanyProfileView: View { ... // expanded code private (set) var viewModel: CompanyProfileViewModel init(viewModel: CompanyProfileViewModel) { self.viewModel = viewModel } } However the issue I'm running into is that whilst this init method is accessible in most of the project, when I try to use it in a #Preview block I get errors: #Preview("Default") { CompanyProfileView(viewModel: .mock()) .previewLayout(.fixed(width: 380, height: 2000)) } Based on the top error it seems like the #Preview block isn't able to see the generated init method. I could go back to just using the standard preview code that doesn't using #Preview, but that seems like I'm taking a step forward and a step back with making the code less verbose. Any suggestions on how to get this working? This seems like a bug to me.
2
2
341
Oct ’23
Mac Catalyst Modally Presented View Controllers Not Working in Xcode Live Previews
I have a UIViewController subclass I'm using in Mac Catalyst. This view controller is only ever presented as a sheet. When I try to make a live preview for it the preview is displayed a gigantic size (not the sheet's actual size at runtime). I made a separate thread about this: https://developer.apple.com/forums/thread/738641 In order to be able to preview the view controller for Mac Catalyst at the desired size I figured I'd present it on another view controller. #Preview { let wrapperVC = WrapperViewController() return wrapperVC } //In WrapperViewController override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if (firstViewDidAppear) { firstViewDidAppear = false view.backgroundColor = UIColor.yellow let realVC = ActualVCIWantToPreview() realVC.modalPresentationStyle = .formSheet present(realVC, animated: false); } } But that's not working. However if I change the device from Mac to iPad it does work so it appears modal presentations aren't working for Live Previews on Mac Catalyst (unless I'm doing something wrong but Xcode is reporting no errors it just isn't showing my presented view controller).
1
0
486
Sep ’23
Mac Catalyst with Live Previews: Any Way to Change the Size the View Controller Preview is Rendered at?
I have a Mac Catalyst app. When using "Live Previews" in Xcode 15 for a view controller of mine the live preview renders at a gigantic size in the Preview. This view controller is only ever presented as a "sheet" on Mac at a fixed size so ideally I'd like to be able to specify the "window size" for the preview environment. Is there a way to do this? Thanks in advance.
0
0
341
Sep ’23
UIKit Live Preview for Objective-C View Controller?
Now that live previews are available in UIKit (source: https://developer.apple.com/wwdc23/10252) I was wondering how to get a UIViewController from Objective-C. The Swift macro looks like this: #Preview { var controller = SomeViewController() return controller; } Is there a way to get a live preview for UIViewControllers/UIViews written in Objective-C (other than wrapping it as a child view controller in an empty swift view controller)?
4
0
901
Sep ’23
Fatal Error in ModelData at line 43
Failed to decode landmarksData.json from bundle because it appears to be invalid JSON. Full disclosure - Im extremely new to this and am playing around with the swift tutorials to see what it can do and see if I could build an app. Any advice is appreciated. I added a few locations to the SwiftUI Tutorial for Lanmarks and this is what happened. I have checked that JSON file and all commas are in the correct place everything is correct but im getting this error. Not sure what im doing incorrectly. Can anyone give me assistance on how to fix this. I can only preview favorite button, circle image and map. Everything else appears as Fatal Error in ModelData.
4
0
2.6k
Sep ’23
Trouble with new XCode 15 Preview Macro for Widgets
I'm trying to get rid of the auto-padding added recently by iOS 17, and I found out that you can use the WidgetConfigurations properly with the new Preview Macro, but I'm having trouble getting it to work, and some help would be much appreciated. Here's my code: struct ChartEntry: TimelineEntry { let date: Date let configuration: ConfigurationIntent ... static func getSampleEntry() -> ChartEntry { let sample = ChartEntry(date: Date(), configuration: WidgetUtils.getSampleConfiguration()) return sample } } struct PreviewTimelineProvider: TimelineProvider { typealias Entry = ChartEntry func placeholder(in context: Context) -> ChartEntry { ChartEntry.getSampleEntry() } func getSnapshot(in context: Context, completion: @escaping (ChartEntry) -> Void) { completion(ChartEntry.getSampleEntry()) } func getTimeline(in context: Context, completion: @escaping (Timeline<ChartEntry>) -> Void) { var entries: [ChartEntry] = [] let currentDate = Date() for hourOffset in 0 ..< 5 { let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! let entry = ChartEntry(date: entryDate, configuration: WidgetUtils.getSampleConfiguration()) entries.append(entry) } let timeline = Timeline(entries: entries, policy: .atEnd) completion(timeline) } } struct DailyHoursOfSunshine: Identifiable { let id = UUID() var date: Date var hoursOfSunshine: Double init(date: Date, hoursOfSunshine: Double) { self.date = date self.hoursOfSunshine = hoursOfSunshine } } var data = [ DailyHoursOfSunshine(date: Date(), hoursOfSunshine: 0), // Sunday DailyHoursOfSunshine(date: Date().addingTimeInterval(86400), hoursOfSunshine: 60), // Monday DailyHoursOfSunshine(date: Date().addingTimeInterval(2 * 86400), hoursOfSunshine: 50), // Tuesday DailyHoursOfSunshine(date: Date().addingTimeInterval(3 * 86400), hoursOfSunshine: 72), // Wednesday DailyHoursOfSunshine(date: Date().addingTimeInterval(4 * 86400), hoursOfSunshine: 88), // Thursday DailyHoursOfSunshine(date: Date().addingTimeInterval(5 * 86400), hoursOfSunshine: 70), // Friday DailyHoursOfSunshine(date: Date().addingTimeInterval(6 * 86400), hoursOfSunshine: 120) // Saturday ] struct ChartSmallView: View { var entry: ChartEntry var body: some View { VStack { Spacer() VStack(alignment: .leading, spacing: 0) { Chart(data) { AreaMark( x: .value("Week", $0.date), y: .value("Hours of Sunshine", $0.hoursOfSunshine) ) .alignsMarkStylesWithPlotArea() } .foregroundStyle( .linearGradient ( colors: [.blue, .clear], startPoint: .top, endPoint: .bottom ) ) .chartXAxis(.hidden) .chartYAxis(.hidden) .chartLegend(.hidden) .padding([.horizontal], 3) } Spacer() } .frame(maxWidth: .infinity) } } #Preview("Small", as: .systemSmall) { ChartWidget() } timelineProvider: { PreviewTimelineProvider() } NOTE: There's nothing much on my ChartWidget struct, just the configuration to disable the margins and declare the preferred size and calling the ChartSmallView struct, I just didn't copy it here because it is in another file and I'm a little busy to go out excluding sensible information. I would actually prefer to call just ChartSmallView on my preview, but then I wouldn't have access to the configuration that disables the awful contextualized margins. The code compiles just fine, but when the preview tries to build it, the following error happens: == PREVIEW UPDATE ERROR: CompileDylibError: Failed to build ChartSmallView.swift Compiling failed: cannot convert value of type 'PreviewTimelineProvider' to closure result type 'any View' @__swiftmacro_63MyWidgetExtension_PreviewReplacement_ChartSmallView_133_77BD2C8074131F1FDF53BC08606762A7Ll0F0fMf_.swift ------------------------------ @available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, xrOS 1.0, *) struct $s63MyWidgetExtension_PreviewReplacement_ChartSmallView_133_77BD2C8074131F1FDF53BC08606762A7Ll0F0fMf_15PreviewRegistryfMu_: DeveloperToolsSupport.PreviewRegistry { static let fileID: String = "MyWidgetExtension_PreviewReplacement_ChartSmallView_1/ChartSmallView.1.preview-thunk.swift" static let line: Int = 125 static let column: Int = 1 static func makePreview() throws -> DeveloperToolsSupport.Preview { DeveloperToolsSupport.Preview("Small", as: .systemSmall) { ChartWidget() } timelineProvider: { PreviewTimelineProvider() } } } ------------------------------ /.../MyWidgetExtension.build/Objects-normal/x86_64/ChartSmallView.1.preview-thunk.swift:137:13: error: cannot convert value of type 'PreviewTimelineProvider' to closure result type 'any View' PreviewTimelineProvider() ^~~~~~~~~~~~~~~~~~~~~~~~~ as! any View I have also attempted to use the Preview Macro in the following way: #Preview("Small", as: .systemSmall) { ChartWidget() } timeline: { ChartEntry.getSampleEntry() } But achieved the same result. If anyone has any idea of what I'm doing wrong, I would appreciate the help. I couldn't find much material online to help me here
2
0
1.5k
Sep ’23
Xcode 15 (15A240d): Cannot preview in this file
I downloaded Xcode 15, and tried to create a new macOS project. But after it opens up, I cannot see the preview, neither am I able to run the application. I get the following error in the debugger console when I try to run the application: dyld[4339]: Symbol not found: _$s21DeveloperToolsSupport7PreviewV7SwiftUIE_6traits4bodyACSSSg_AA0D5TraitVyAC10ViewTraitsOGdAD0J0_pyScMYcctcfC Referenced from: &lt;B71F07A5-FC93-303C-8B33-1607EA5185FC&gt; /Users/monawwar/Library/Developer/Xcode/DerivedData/TestApp-dndfpagiijwwzvfkoioloexccazm/Build/Products/Debug/TestApp.app/Contents/MacOS/TestApp Expected in: &lt;5CBA1C3F-43C5-3529-8130-C71BAB1413D5&gt; /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
1
1
515
Sep ’23
Xcode 15.0 (15A240d) - Preview Provider Orientation
Hello! 👋 In previous versions of Xcode I was used to make something like this: struct MyView_Previews: PreviewProvider { static var previews: some View { MyView() .previewInterfaceOrientation(.landscapeLeft) } } So I don't have to change the Device Settings in the Canvas. Now, with Xcode 15, I couldn't manage to do the same. #Preview { MyView() .previewInterfaceOrientation(.landscapeLeft) }
1
0
632
Sep ’23
User location undefined in SwiftUI Preview
In the wwdc2023-10043 session, the instructor adds a MapUserLocationButton towards the end, which seems to work fine in the SwiftUI Preview for him. However, when I follow this tutorial and add the very same button, I get a never-ending spinning wheel upon clicking it in SwiftUI Preview. I guess that the SwiftUI Preview cannot get a user location. I searched in various places whether I could set the user location, as possible in the simulator, but to no avail. In the Debug menu, there is a Simulate Location submenu, but it totally deactivated (greyed out). Does anyone know how to allow SwiftUI Preview to get an actual user location, i.e., either the one from my Mac or one that I could provide to the SwiftUI Preview simulator?
2
0
1k
Sep ’23
Landmarks Tutorial. Preview doesn't work
Hi everyone, I'm trying to follow the Landmarks project tutorial and got stuck in the second part, in the second section (Create the Row View), step 6 where it says "Modify the text view to use the landmark property’s name." This must be done in the LandmarkRow.swift file. https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation The tutorial shows that after doing this, the preview will display the name of the landmark (Turtle Rock) instead of the usual "Hello world" greeting. But my replacement is not happening. Moreover, from this point on, the preview stops working. There are no error messages in the code, except for the message that the preview could not be performed. I checked and rewrote the code several times, replaced the data source files, but nothing helped. At the same time, the preview works well in other view files. I can't figure out what's wrong with my code? Any ideas as to what the reason will be is appreciated. Below is the code of two files, the LandmarkRow.swift, where view does not work, the second is ModelData.swift and it is related to the previous one. LandmarkRow.swift import SwiftUI struct LandmarkRow: View {     var landmark: Landmark     var body: some View {         HStack {             landmark.image                 .resizable()                 .frame(width: 50, height: 50)             Text(landmark.name)             Spacer()         }     } } struct LandmarkRow_Previews: PreviewProvider {     static var previews: some View {         LandmarkRow(landmark: landmarks[0])     } } ModelData.swift import Foundation var landmarks: [Landmark] = load("landmarkData.json") func loadT: Decodable(_ filename: String) - T {     let data: Data     guard let file = Bundle.main.url(forResource: filename, withExtension: nil)     else {         fatalError("Couldn't find \(filename) in main bundle")     }     do {         data = try Data(contentsOf: file)     } catch {         fatalError("Couldn't load \(filename) from main bundle:\n\(error)")     }     do {         let decoder = JSONDecoder()         return try decoder.decode(T.self, from: data)     } catch {         fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")     } }
8
1
2.9k
Sep ’23
Xcode Previews doesn't work when used in a package that imports other packages.
I keep getting a Xcode Previews error in Xcode 12 from a package that imports other packages. As soon as I remove the dependency I'm able to see the SwiftUI preview. I'm only able to use previews in packages that have no dependencies The error I get is: "LoadingError: failed to load library at path...Library not loaded " then it point to the dependency that it could not load in the current package. How can I access Xcode Previews from a package that depends on other packages?
32
20
18k
Sep ’23