I've been experiencing random crashes in my app and today, I could finally narrow it down enough to start building an example project that consistently crashes. The error is this:
"com.apple.SwiftUI.AsyncRenderer (19): EXC_BREAKPOINT (code=1, subcode=0x102f103f8)"
It's a very strange bug because the SwiftUI view hierarchy has to be in a very specific shape to cause the crash, but if it is, then it crashes 100% of the time:
It's a button inside a List and a simple "@State var number: Int?" that is being animated when the view appears. You can simply paste the following code into a project and run it (device, simulator or even in a preview):
// Must be in Swift 6 Language Mode!
// iOS 18 Beta 4
struct ContentView: View {
@State var number: Int?
var body: some View {
List { // Must be in a List
// It also crashes if button is here inside the List
}
.overlay {
button
}
.task { // Has to be in a task modifier (or .onAppear + Task {}, so it has to be async)
number = 0
}
}
// Has to be in a separate view property
private var button: some View {
Button {} label: { // Has to be this initializer and not Button(number == nil ? "" : "") {}
if number == nil { // There has to be a conditional with "number" in here
Color.clear
}
}
.animation(.default, value: number) // Animation must be inside the List
.alignmentGuide(VerticalAlignment.center) { dimension in
dimension[VerticalAlignment.center]
}
}
}
The crash only happens under the following conditions, as far as I can tell:
This only happens in the Swift 6 language mode (which I have enabled in the example project)
It's related to an .alignmentGuide modifier attached to a button that also has an .animation modifier
The button has to have a conditional view inside its label that checks for the same variable that is being animated
The button must be inside a separate property that is put either inside a List or inside an overlay of a List
The crash is then triggered when the animated value is asynchronously changed (in a .task modifier for example)
The crash happens at least with Beta 3 and 4 of Xcode 16.0
I have also opened a feedback: FB14510236 but I thought I'd post it here as well because this crash was driving me crazy and maybe others might find it helpful for their debugging. Took me hours to find out what's been causing it 😅
Edit:
Interestingly, when I run this project on an iOS 17 device, it does not crash, but rather print a runtime warning:
"warning: data race detected: @MainActor function at AlignmentGuideCrash/ContentView.swift:27 was not called on the main thread"
Line 27 is:
.alignmentGuide(VerticalAlignment.center) { dimension in
SwiftUI
RSS for tagProvide views, controls, and layout structures for declaring your app's user interface using SwiftUI.
Posts under SwiftUI tag
188 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
With the new @Observable macro, it looks like every time the struct of a view is reinitialized, any observable class marked as @State in the struct also gets reinitialized. Moreover, the result of the reinitialization immediately gets discarded.
This is in contrast to @StateObject and ObservableObject, where the class would only be initialized at the first creation of the view. The initialization method of the class would never be called again between view updates.
Is this a bug or an expected behavior? This redundant reinitialization causes performance issues when the init method of the observable class does anything slightly heavyweight.
Feedback ID: FB13697724
This problem will occur in Xcode 16 beta and iOS 18 beta, are there any changes to the function of highPriorityGesture in iOS 18?
Example code is:
import SwiftUI
struct ContentView: View {
@State private var dragable: Bool = false
@State private var dragGestureOffset: CGFloat = 0
var body: some View {
ZStack {
ScrollViewReader { scrollViewProxy in
ScrollView(showsIndicators: false) {
cardList
.padding(.horizontal, 25)
}
}
}
.highPriorityGesture(dragOffset)
}
var cardList: some View {
LazyVStack(spacing: 16) {
Text("\(dragGestureOffset)")
.frame(maxWidth: .infinity)
.padding(.vertical,8)
.background {
RoundedRectangle(cornerRadius: 8)
.fill(.gray)
}
//Display 100 numerical values in a loop
ForEach(0..<100) { index in
Text("\(index)")
.frame(maxWidth: .infinity)
.padding(.vertical,8)
.background {
RoundedRectangle(cornerRadius: 8)
.fill(.gray)
}
}
}
}
var dragOffset: some Gesture {
DragGesture()
.onChanged { value in
if abs(value.translation.width) > 25 {
dragable = true
dragGestureOffset = value.translation.width
}
}
.onEnded { value in
if abs(value.translation.width) > 25 {
dragable = false
dragGestureOffset = 0
}
}
}
}
onContinueUserActivity(CSSearchableItemActionType, perform) works as expected on iOS when we search and select an item from Spotlight, but nothing happens when we do the same on a SwiftUI macOS app.
var body: some Scene {
WindowGroup {
MyView()
.onContinueUserActivity(CSSearchableItemActionType, perform: handleSpotlight)
}
}
func handleSpotlight(_ userActivity: NSUserActivity) {
// Is not called...
}
How can we respond to a user clicking a Spotlight result from our apps on macOS?
I was adding WidgetExtension target for my old project.
The widget target is running fine in iOS 17, In my case, widget need to support iOS 14 and above, so I updated my widget code to old style, To removing WidgetConfigurationIntent and AppIntentTimelineProvider.
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry()
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> Void) {
completion(SimpleEntry())
}
func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> Void) {
completion(Timeline(entries: [SimpleEntry()], policy: .never))
}
typealias Entry = SimpleEntry
}
struct SimpleEntry: TimelineEntry {
let date: Date = Date()
}
struct NCWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Text("Time:")
Text(entry.date, style: .time)
}
}
}
struct NCWidget: Widget {
let kind: String = "NCWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
NCWidgetEntryView(entry: entry)
}
.configurationDisplayName("DisplayName")
.description("description")
}
}
In my case, the code was working fine in iOS 17 simulator, But if I try to run in iOS 15, it returns the below error
code-SendProcessControlEvent:toPid: encountered an error: Error Domain=com.apple.dt.deviceprocesscontrolservice Code=8 "Failed to show Widget 'com.name-pprd.NCWidgetExtension' error: Error Domain=FBSOpenApplicationServiceErrorDomain Code=5 "The request to open "com.apple.springboard" failed." UserInfo={NSLocalizedDescription=The request to open "com.apple.springboard" failed., NSLocalizedFailureReason=Unexpected error type., NSUnderlyingError=0x600003570b40 {Error Domain=BSServiceConnectionErrorDomain Code=3 "XPC error received on message reply handler" UserInfo={BSErrorCodeDescription=OperationFailed, NSLocalizedFailureReason=XPC error received on message reply handler}}, BSErrorCodeDescription=InvalidResponse}." UserInfo={NSLocalizedDescription=Failed to show Widget 'com.name-pprd.NCWidgetExtension' error: Error Domain=FBSOpenApplicationServiceErrorDomain Code=5 "The request to open "com.apple.springboard" failed." UserInfo={NSLocalizedDescription=The request to open "com.apple.springboard" failed., NSLocalizedFailureReason=Unexpected error type., NSUnderlyingError=0x600003570b40 {Error Domain=BSServiceConnectionErrorDomain Code=3 "XPC error received on message reply handler" UserInfo={BSErrorCodeDescription=OperationFailed, NSLocalizedFailureReason=XPC error received on message reply handler}}, BSErrorCodeDescription=InvalidResponse}., NSUnderlyingError=0x600003570bd0 {Error Domain=FBSOpenApplicationServiceErrorDomain Code=5 "The request to open "com.apple.springboard" failed." UserInfo={NSLocalizedDescription=The request to open "com.apple.springboard" failed., NSLocalizedFailureReason=Unexpected error type., NSUnderlyingError=0x600003570b40 {Error Domain=BSServiceConnectionErrorDomain Code=3 "XPC error received on message reply handler" UserInfo={BSErrorCodeDescription=OperationFailed, NSLocalizedFailureReason=XPC error received on message reply handler}}, BSErrorCodeDescription=InvalidResponse}}}
Domain: DTXMessage
Code: 1
User Info: {
DVTErrorCreationDateKey = "2024-07-20 17:30:58 +0000";
}
--
System Information
macOS Version 14.5 (Build 23F79)
Xcode 15.0.1 (22266) (Build 15A507)
Timestamp: 2024-07-20T23:00:58+05:30
In widget target Minimum target is 14.0
In App target Minimum target is 13.0
I added gesture support to my app that supports iOS 16 and 17 and have never had issues with it.
However, when I compiled my app with Xcode 16 I immediately noticed a problem with the app when I ran it in the simulator. I couldn't scroll up or down. I figured out it’s because of my gesture support.
My gesture support is pretty simple.
let myDragGesture = DragGesture()
.onChanged { gesture in
self.offset = gesture.translation
}
.onEnded { _ in
if self.offset.width > threshold {
...some logic
} else if self.offset.width < -threshold {
...some other logic
}
logitUI.debug("drag gesture width was \(self.offset.width)")
self.offset = .zero
}
If I pass nil to .gesture instead of myDragGesture then scrolling starts working again.
Here’s some example output when I’m trying to scroll down. These messages do NOT appear when I run my app on an iOS 16/17 simulator with Xcode 15.
drag gesture width was 5.333328
drag gesture width was -15.333344
drag gesture width was -3.000000
drag gesture width was -24.333328
drag gesture width was -30.666656
I opened FB14205678 about this.
Hello, community and Apple engineers. I need your help.
Our app has the following issue: NavigationStack pushes a view twice if the NavigationStack is inside TabView and NavigationStack uses a navigation path of custom Hashable elements.
Our app works with issues in Xcode 18 Beta 13 + iOS 18.0. The same issue happened on previous beta versions of Xcode 18.
The issue isn’t represented in iOS 17.x and everything worked well before iOS 18.0 beta releases.
I was able to represent the same issue in a clear project with two simple views. I will paste the code below.
Several notes:
We use a centralised routing system in our app where all possible routes for navigation path are implemented in a View extension called withAppRouter().
We have a enum RouterDestination that contains all possible routes and is resolved in withAppRouter() extension.
We use Router class that contains @Published var path: [RouterDestination] = [] and this @Published property is bound to NavigationStack. In the real app, we need to have an access to this path property for programmatic navigation purposes.
Our app uses @ObservableObject / @StateObject approach.
import SwiftUI
struct ContentView: View {
@StateObject private var router = Router()
var body: some View {
TabView {
NavigationStack(path: $router.path) {
NavigationLink(value: RouterDestination.next, label: {
Label("Next", systemImage: "plus.circle.fill")
})
.withAppRouter()
}
}
}
}
enum RouterDestination: Hashable {
case next
}
struct SecondView: View {
var body: some View {
Text("Screen 2")
}
}
class Router: ObservableObject {
@Published var path: [RouterDestination] = []
}
extension View {
func withAppRouter() -> some View {
navigationDestination(for: RouterDestination.self) { destination in
switch destination {
case .next:
return SecondView()
}
}
}
}
Below you can see the GIF with the issue:
What I tried to do:
Use iOS 17+ @Observable approach. It didn’t help.
Using @State var path: [RouterDestination] = [] directly inside View seems to help. But it is not what we want as we need this property to be @Published and located inside Router class where we can get an access to it, and use for programmatic navigation if needed.
I ask Apple engineers to help with that, please, and if it is a bug of iOS 18 beta, then please fix it in the next versions of iOS 18.0
Hi folks,
I've used a NavigationSplitView within one of the tabs of my app since iOS 16, but with the new styling in iOS 18 the toolbar region looks odd. In other tabs using e.g. simple stacks, the toolbar buttons are horizontally in line with the new tab picker, but with NavigationSplitView, the toolbar leaves a lot of empty space at the top (see below). Is there anything I can do to adjust this, or alternatively, continue to use the old style?
Thanks!
I'm trying to add a ControlWidget to my WidgetBundle like this:
struct MyWidgets: WidgetBundle {
var body: some Widget {
if #available(iOSApplicationExtension 18.0, *) {
LauncherControl()
}
MyLiveActivityWidget()
HomeScreenWidget()
LockScreenWidget()
}
This works exactly as expected on iOS 18. However on iOS 17 my app seems to have no widgets at all.
The workaround described here (https://www.avanderlee.com/swiftui/variable-widgetbundle-configuration/) does not work either since WidgetBundleBuilder.buildBlock does not accept ControlWidget as an argument.
What is the correct way to include a Control widget conditionally on iOS 18?
Hello,
With iOS 18, when NavigationStack is in new TabView, with path parameter containing current navigation state is set, the navigation destination view is pushed twice.
See below with example that pushes twice on iOS 18 but is correct on iOS 17
@MainActor
class NavigationModel: ObservableObject {
static let shared = NavigationModel()
@Published var selectedTab: String
@Published var homePath: [Route]
@Published var testPath: [Route]
}
struct ContentView: View {
@StateObject private var navigationModel: NavigationModel = NavigationModel.shared
var body: some View {
TabView(selection: $navigationModel.selectedTab){
HomeView()
.tabItem {
Label("Home", systemImage: "house")
}
.tag("home")
TestView()
.tabItem {
Label("Test", systemImage: "circle")
}
.tag("test")
}
}
}
struct HomeView: View {
@StateObject private var navigationModel: NavigationModel = NavigationModel.shared
var body: some View {
NavigationStack(path: $navigationModel.homePath){
VStack{
Text("home")
NavigationLink(value: Route.test1("test1")){
Text("Go to test1")
}
}
.navigationDestination(for: Route.self){ route in
NavigationModelBuilder.findFinalDestination(route:route)
}
}
}
}
I don't what causes the issue because it works well on iOS 16 and iOS 17. I think the path is somehow reset but I don't why (maybe by the TabView ?)
Note that the bug only occurs with TabView.
Don't really know if it is a TabView bug or if it is on my side.
I filed a feedback with sample project FB14312064
Hi, I'm working on visionOS and find I can't get onDisappear event just on the first window after app launch. It comes like that:
WindowGroup(id:"WindowA"){
MyView()
.onDisappear(){
print("WindowA disappear")
}
}
WindowGroup(id:"WindowB"){
MyView()
.onDisappear(){
print("WindowB disappear")
}
}
WindowGroup(id:"WindowC"){
MyView()
.onDisappear(){
print("WindowC disappear")
}
}
When the app first launch, it will open WindowA automatically
And then I open WindowB and WindowC programatically.
Then I tap the close button on window bar below window.
If I close WindowB/WindowC, I can receive onDisappear event
If I close WindowA, I can't receive onDisappear event
If I reopen WindowA after it is closed and then close it again by tap the close button below window, I can receive onDisappear event
Is there any logic difference for the first window on app launch? How can I get onDisappear Event for it.
I'm using Xcode 16 beta 2
I have an app with fairly typical requirements - I need to insert some data (in my case from the network but could be anything) and I want to do it in the background to keep the UI responsive.
I'm using SwiftData.
I've created a ModelActor that does the importing and using the debugger I can confirm that the data is indeed being inserted.
On the UI side, I'm using @Query and a SwiftUI List to display the data but what I am seeing is that @Query is not updating as the data is being inserted. I have to quit and re-launch the app in order for the data to appear, almost like the context running the UI isn't communicating with the context in the ModelActor.
I've included a barebones sample project. To reproduce the issue, tap the 'Background Insert' button. You'll see logs that show items being inserted but the UI is not showing any data.
I've tested on the just released iOS 18b3 seed (22A5307f).
The sample project is here:
https://hanchor.s3.amazonaws.com/misc/SwiftDataBackgroundV2.zip
I recently compiled my macOS App with Swift 6 in Xcode 16 (was using Swift 5 previously) and noticed that AppKit Integration doesn't appear to be working as before. All my instances of NSHostingView which allow me to add a SwiftUI View to an AppKit NSWindow view controller no longer respond to mouse input anymore. All my NSHostingView instances display but refuse to accept any user interaction. Has anyone else noticed this and is there a workaround to get NSHostingView to once again be able to accept user/mouse events with Swift 6?
I am trying to configure scenes to capture a redirect URL after a successful login attempt. I am using OAuth code flow. This is the code I have so far:
ios_app.swift
import SwiftUI
@main
struct ios_appApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let persistenceController = PersistenceController.shared
@StateObject private var authState = AuthState()
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.environmentObject(authState)
}
}
}
AppDelegate.swift
import UIKit
import AWSMobileClient
import GoogleSignIn
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("AppDelegate: didFinishLaunchingWithOptions")
AWSMobileClient.default().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState)")
} else if let error = error {
print("Error initializing AWSMobileClient: \(error.localizedDescription)")
}
}
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
if let error = error {
print("Error restoring previous Google Sign-In: \(error.localizedDescription)")
}
}
return true
}
}
SceneDelegate.swift
import UIKit
import SwiftUI
import GoogleSignIn
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
print("SceneDelegate: scene willConnectTo")
guard let windowScene = (scene as? UIWindowScene) else {
print("SceneDelegate: Invalid windowScene")
return
}
let window = UIWindow(windowScene: windowScene)
let contentView = ContentView().environmentObject(AuthState())
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
print("SceneDelegate: window initialized and visible")
}
func sceneDidDisconnect(_ scene: UIScene) {
print("SceneDelegate: sceneDidDisconnect")
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("SceneDelegate: sceneDidBecomeActive")
}
func sceneWillResignActive(_ scene: UIScene) {
print("SceneDelegate: sceneWillResignActive")
}
func sceneWillEnterForeground(_ scene: UIScene) {
print("SceneDelegate: sceneWillEnterForeground")
}
func sceneDidEnterBackground(_ scene: UIScene) {
print("SceneDelegate: sceneDidEnterBackground")
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
print("SceneDelegate: No URL found in URLContexts")
return
}
print("SceneDelegate: openURLContexts with URL: \(url)")
if GIDSignIn.sharedInstance.handle(url) {
print("SceneDelegate: Google Sign-In handled URL")
return
}
if url.scheme == "myurlscheme" || (url.scheme == "https" && url.host == "mydomain.com" && url.path == "/mobile-auth-callback") {
print("SceneDelegate: Handling auth response for URL: \(url)")
AuthService.shared.handleOAuthCallback(url: url)
} else {
print("SceneDelegate: URL scheme not handled: \(url.scheme ?? "No scheme")")
}
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
print("SceneDelegate: continue userActivity")
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
if let url = userActivity.webpageURL {
handleUniversalLink(url: url)
}
}
}
private func handleUniversalLink(url: URL) {
print("SceneDelegate: Handling universal link: \(url)")
if url.path.contains("/mobile-auth-callback") {
let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems
let code = queryItems?.first(where: { $0.name == "code" })?.value
if let code = code {
print("SceneDelegate: Received code: \(code)")
AuthService.shared.exchangeCodeForToken(code: code)
} else {
print("SceneDelegate: No code found, handling email/password callback")
AuthService.shared.handleEmailPasswordCallback(url: url)
}
}
}
}
Also introduced this configuration in Info.plist:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>ios-app.SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
...Other parameters
</dict>
</plist>
I am using the simulator to launch my app and can see AppDelegate related logs but I am not seeing any SceneDelegate logs (I suppose because it is not being initialized nor called).
I have tried restarting the computer/Xcode, clean and rebuild the application but none of the things I tested work.
Is there any part of my code wrong? Any other idea here?
Hi, I can't get onScrollPhaseChange to fire when using a List. It works as expected when using a ScollView and LazyVStack.
Interestingly, onScrollGeometryChange gets called as expected for both List and ScrollView.
Has anyone successfully used onScrollPhaseChange with a List?
Hi everyone,
I'm currently working on an iOS app using SwiftUI, and I'm facing an issue with a vertical ScrollView. My goal is to have the ScrollView take up all the safe area space plus the top inset (with the bottom inset being an ultra-thin material) and enable paging behavior. However, I'm encountering two problems:
The initial height of the ScrollView is too high (dragging the view (even without changing the page) adjusts the size).
The paging offset of the ScrollView is incorrect (page views are not aligned).
I’ve tried many things and combinations in desperation, including padding, safeAreaPadding, contentMargins, frame, fixedSize, containerRelativeFrame with callback, custom layout, and others, but nothing seems to work.
If by any chance someone can help me find a solution, I’d greatly appreciate it.
I suspect there are issues with the height defined by the ScrollView when some safe areas are ignored, leading to strange behavior when trying to set the height. It's challenging to explain everything in a simple post, so if someone from the SwiftUI team could look into this potential bug, that would be incredibly helpful.
Thank you!
import SwiftUI
struct ScrollViewIssue: View {
@State private var items: [(String, Color)] = [
("One", .blue),
("Two", .green),
("Three", .red),
("Four", .purple)
]
var body: some View {
ZStack(alignment: .top) {
ScrollView(.vertical) {
VStack(spacing: 0) {
ForEach(items, id: \.0) { item in
ItemView(text: item.0, color: item.1)
.containerRelativeFrame([.horizontal, .vertical])
}
}
}
.printViewSize(id: "ScrollView")
.scrollTargetBehavior(.paging)
.ignoresSafeArea(edges: .top)
VStack {
Text("Title")
.foregroundStyle(Color.white)
.padding()
.frame(maxWidth: .infinity)
.background(Color.black.opacity(0.2))
Spacer()
}
}
.printViewSize(id: "ZStack")
.safeAreaInset(edge: .bottom) {
Rectangle()
.frame(width: .infinity, height: 0)
.overlay(.ultraThinMaterial)
}
}
}
struct ItemView: View {
let text: String
let color: Color
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 20)
.fill(color.opacity(0.2))
.overlay(
color,
in: RoundedRectangle(cornerRadius: 20)
.inset(by: 10 / 2)
.stroke(lineWidth: 10)
)
Text(text)
}
.printViewSize(id: "item")
}
}
struct ViewSizeKey: PreferenceKey {
static var defaultValue = CGSize()
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
value = nextValue()
}
}
extension View {
func printViewSize(id: String) -> some View {
background(
GeometryReader { proxy in
Color.clear
.preference(key: ViewSizeKey.self, value: proxy.size)
}
.onPreferenceChange(ViewSizeKey.self) { value in
print("\(id) size:\(value)")
}
)
}
}
#Preview {
ScrollViewIssue()
}
I am working on an iOS application using SwiftUI where I want to convert a JPG and a MOV file to a live photo. I am utilizing the LivePhoto Class from Github for this. The JPG and MOV files are displayed correctly in my WallpaperDetailView, but I am facing issues when trying to download the live photo to the gallery and generate the Live Photo.
Here is the relevant code and the errors I am encountering:
Console prints:
Play button should be visible Image URL fetched and set: Optional("https://firebasestorage.googleapis.com/...") Video is ready to play Video downloaded to: file:///var/mobile/Containers/Data/Application/.../tmp/CFNetworkDownload_7rW5ny.tmp Failed to generate Live Photo
I have verified that the app has the necessary permissions to access the Photo Library.
The JPEG and MOV files are successfully downloaded and can be displayed in the app.
The issue seems to occur when generating the Live Photo from the downloaded files.
struct WallpaperDetailView: View {
var wallpaper: Wallpaper
@State private var isLoading = false
@State private var isImageSaved = false
@State private var imageURL: URL?
@State private var livePhotoVideoURL: URL?
@State private var player: AVPlayer?
@State private var playerViewController: AVPlayerViewController?
@State private var isVideoReady = false
@State private var showBuffering = false
var body: some View {
ZStack {
if let imageURL = imageURL {
GeometryReader { geometry in
KFImage(imageURL)
.resizable()
...
}
}
if let playerViewController = playerViewController {
VideoPlayerViewController(playerViewController: playerViewController)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.clipped()
.edgesIgnoringSafeArea(.all)
}
}
.onAppear {
PHPhotoLibrary.requestAuthorization { status in
if status == .authorized {
loadImage()
} else {
print("User denied access to photo library")
}
}
}
private func loadImage() {
isLoading = true
if let imageURLString = wallpaper.imageURL, let imageURL = URL(string: imageURLString) {
self.imageURL = imageURL
if imageURL.scheme == "file" {
self.isLoading = false
print("Local image URL set: \(imageURL)")
} else {
fetchDownloadURL(from: imageURLString) { url in
self.imageURL = url
self.isLoading = false
print("Image URL fetched and set: \(String(describing: url))")
}
}
}
if let livePhotoVideoURLString = wallpaper.livePhotoVideoURL, let livePhotoVideoURL = URL(string: livePhotoVideoURLString) {
self.livePhotoVideoURL = livePhotoVideoURL
preloadAndPlayVideo(from: livePhotoVideoURL)
} else {
self.isLoading = false
print("No valid image or video URL")
}
}
private func preloadAndPlayVideo(from url: URL) {
self.player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = self.player
self.playerViewController = playerViewController
let playerItem = AVPlayerItem(url: url)
playerItem.preferredForwardBufferDuration = 1.0
self.player?.replaceCurrentItem(with: playerItem)
...
print("Live Photo Video URL set: \(url)")
}
private func saveWallpaperToPhotos() {
if let imageURL = imageURL, let livePhotoVideoURL = livePhotoVideoURL {
saveLivePhotoToPhotos(imageURL: imageURL, videoURL: livePhotoVideoURL)
} else if let imageURL = imageURL {
saveImageToPhotos(url: imageURL)
}
}
private func saveImageToPhotos(url: URL) {
...
}
private func saveLivePhotoToPhotos(imageURL: URL, videoURL: URL) {
isLoading = true
downloadVideo(from: videoURL) { localVideoURL in
guard let localVideoURL = localVideoURL else {
print("Failed to download video for Live Photo")
DispatchQueue.main.async {
self.isLoading = false
}
return
}
print("Video downloaded to: \(localVideoURL)")
self.generateAndSaveLivePhoto(imageURL: imageURL, videoURL: localVideoURL)
}
}
private func generateAndSaveLivePhoto(imageURL: URL, videoURL: URL) {
LivePhoto.generate(from: imageURL, videoURL: videoURL, progress: { percent in
print("Progress: \(percent)")
}, completion: { livePhoto, resources in
guard let resources = resources else {
print("Failed to generate Live Photo")
DispatchQueue.main.async {
self.isLoading = false
}
return
}
print("Live Photo generated with resources: \(resources)")
self.saveLivePhotoToLibrary(resources: resources)
})
}
private func saveLivePhotoToLibrary(resources: LivePhoto.LivePhotoResources) {
LivePhoto.saveToLibrary(resources) { success in
DispatchQueue.main.async {
if success {
self.isImageSaved = true
print("Live Photo saved successfully")
} else {
print("Failed to save Live Photo")
}
self.isLoading = false
}
}
}
private func fetchDownloadURL(from gsURL: String, completion: @escaping (URL?) -> Void) {
let storageRef = Storage.storage().reference(forURL: gsURL)
storageRef.downloadURL { url, error in
if let error = error {
print("Failed to fetch image URL: \(error)")
completion(nil)
} else {
completion(url)
}
}
}
private func downloadVideo(from url: URL, completion: @escaping (URL?) -> Void) {
let task = URLSession.shared.downloadTask(with: url) { localURL, response, error in
guard let localURL = localURL, error == nil else {
print("Failed to download video: \(String(describing: error))")
completion(nil)
return
}
completion(localURL)
}
task.resume()
}
}```
Topic:
Media Technologies
SubTopic:
Photos & Camera
Tags:
Files and Storage
Swift
SwiftUI
Photos and Imaging
For whatever reason SwiftUI sheets don't seem to be resizable anymore.
The exact same code/project produces resizable Sheets in XCode 15.4 but unresizable ones with Swift included in Xcode 16 beta 2.
Tried explicitly providing .fixedSize(horizontal false, vertical: false) everywhere humanly possible hoping for a fix but sheets are still stuck at an awkward size (turns out be the minWidth/minHeight if I provide in .frame).
Cannot select individual view in selectable mode in Xcode preview, it only show the whole blue device border.
Hi,
When I use 'listSectionSeparator' on hide the section separator on a List 'section', it works as expected on iOS, but doesn't have any effect on macOS. Is that a known issue? Are there any workarounds for this?
Here's a basic reproducible example:
import SwiftUI
struct TestItem: Identifiable, Hashable {
let id = UUID()
let itemValue: Int
var itemString: String {
get {
return "test \(itemValue)"
}
}
}
struct TestListSelection: View {
let testArray = [TestItem(itemValue: 1), TestItem(itemValue: 2), TestItem(itemValue: 3), TestItem(itemValue: 4)]
@State private var selectedItem: TestItem? = nil
var body: some View {
List (selection: $selectedItem) {
Section("Header") {
ForEach (testArray, id: \.self) { item in
Button {
print("main row tapped for \(item.itemValue)")
} label: {
HStack {
Text(item.itemString)
Spacer()
Button("Tap me") {
print("button tapped")
}
.buttonStyle(.borderless)
}
}
.buttonStyle(.plain)
}
}
.listSectionSeparator(.hidden) // has no effect on macOS
Section("2nd Header") {
ForEach (testArray, id: \.self) { item in
Text(item.itemString)
}
}
.listSectionSeparator(.hidden) // has no effect on macOS
}
.listStyle(.plain)
}
}
#Preview {
TestListSelection()
}