I'm just trying to display an image that is stored in the local filesystem, but the more I dig into this the more confused I get.
So previously I used this code (it's simplified):
func findImage(name: String) -> UIImage? {
do {
let url = try FileManager.default.url(for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
.appendingPathComponent("MyFolder")
.appendingPathComponent("\(name).png")
guard let image = UIImage(contentsOfFile: url.path) else {
return nil
}
return image
} catch {
print(error.localizedDescription)
}
return nil
}
Notice I create the URL with just .appendingPathComponent() and turning URL to path via url.path.
It works! So what's the question?
In Improving performance and stability when accessing the file system I've read that you better use the new appendingPathComponent(_:isDirectory:), that's good, will do.
Also url.path is deprecated in iOS18. Should I use url.path(percentEncoded:) instead? What should be the value of percentEncoded when accessing the local filesystem?
In this adjacent thread I've read:
Don't use UIImage(contentsOfFile:) either, because it's a path-based API. There's no URL-based equivalent, which is an Apple clue that should be doing something else.
Is this true? Then how should I store and load my images?
Just FYI, I create images like this:
private func generateThumbnail(name: String) {
guard let drawingWidth = canvasGeo?.size.width,
let drawingHeight = canvasGeo?.size.height else { return }
let thumbnailRect = CGRect(x: 0, y: 0, width: drawingWidth, height: drawingHeight)
Task {
UITraitCollection(userInterfaceStyle: .light).performAsCurrent {
let image = self.canvasView.drawing.image(from: thumbnailRect, scale: UIScreen.main.scale)
guard let data = image.pngData() else { return } // -- HERE
do {
try FileManager.default.createDirectory(at: try FileManager.default.url(for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
.appendingPathComponent("MyFolder"),
withIntermediateDirectories: true,
attributes: nil)
let filename = "\(name).png"
let url = try FileManager.default.url(for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
.appendingPathComponent("MyFolder")
.appendingPathComponent(filename)
try data.write(to: url, options: .atomic) // -- and HERE
} catch {
print(error.localizedDescription)
}
}
}
}
My usecase — just save the user's PencilKit Canvas as an image and display it back to him on a different View. I'm on SwiftUI and iOS 16+.
Would be happy to learn the correct way, thanks!
Swift
RSS for tagSwift is a powerful and intuitive programming language for Apple platforms and beyond.
Posts under Swift tag
200 Posts
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi,
I am in need of a solution that takes data from SwiftData classes and generates a PDF with the given data. Any guidance would be greatly appreciated.
In my project, i want to add emoji to my video but emoji image becomes dark when add in hdr video, im trying to convert my emoji image to hdr format or create with hdr format but seems not work. I hope someone has experiences in this case could help me.
I have an app on the App Store for many years enabling users to post text into clouds in augmented reality. Yet last week abruptly upon installing the app on the iPhone the screen started going totally dark and a list of little comprehensible logs came up of the kind:
ARSCNCompositor <0x300ad0e00>: ARSCNCompositor (0, 0) initialization failed. Matting is not set up properly.
many times, then
RWorldTrackingTechnique <0x106235180>: Unable to update pose [PredictorFailure] for timestamp 870.392108
ARWorldTrackingTechnique <0x106235180>: Unable to predict pose [1] for timestamp 870.392108
again several times and then:
ARWorldTrackingTechnique <0x106235180>: SLAM error callback: Error Domain=Slam Error Code=7 "Non fatal error occurred due to significant drop in a IMU data" UserInfo={NSDescription=Non fatal error occurred due to significant drop in a IMU data, NSLocalizedFailureReason=SlamEngineNodeGroup Failure: IMU issue: gyro data stream verification failed [Significant data drop]. Failed on timestamp: 870.413247, Last known timestamp: 865.350198, Delta: 5.063049, System timestamp: 870.415781, Delta between system and frame: 0.002534. }
and then again the pose issues several times.
I hoped the new beta version would have solved the issue, but it was not the case. Unfortunately I do not know if that depends on the beta version or some other issue, given the app may be not installed on the Mac simulator.
I use the iTunes Library framework in one of my apps, starting with macOS Sequoia 15.1 i can't create the ITLibrary object anymore with the following error:
Connection to amplibraryd was interrupted. clientName:iTunesLibrary(ITLibraryLoader)
Error connecting to the server via proxy object Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.amp.library.framework" UserInfo={NSDebugDescription=connection to service named com.apple.amp.library.framework}
configure failed: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.amp.library.framework" UserInfo={NSDebugDescription=connection to service named com.apple.amp.library.framework}
I created a new sandboxed macOS app, added the music folder read permission and it reproduced the error:
import SwiftUI
import iTunesLibrary
@main
struct ITLibraryLoaderApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
Button("Load ITLibrary") {
loadLibrary()
}
}
func loadLibrary() {
do {
let _ = try ITLibrary(apiVersion: "1.0", options: .none)
}
catch {
print(error)
}
}
}
I restarted my developer machine and the music app with no luck.
I'm pretty new to Swift and SwiftUI. I'm making my first app for sorting a gallery with some extra features.
I was using my own iPhone for testing and just started testing my app on other Apple products.
Everything works fine on iPad Air M1, iPhone 15 Pro, iPhone 15 Pro Max, iPhone 13, iPhone XS (Simulator), and iPhone 11 Pro (Simulator). However, when I tried to show my app to a family member with an iPhone 11, I came across an issue.
Issue Description:
My app takes all photos from iPhone's native gallery, then you can sort it by some spesific filters and delete pictures. It just looks like the native gallery. (I can add photos later if needed) You can just scroll the gallery by swiping up and down. You can press the select button and start selecting pictures to delete.
I recently added a drag-to-select-multiple-pictures feature. This makes it feel more like the native iOS experience, eliminating the need to tap each picture individually.
However, on the iPhone 11, the moment you open the app, you can't scroll. Scrolling is completely locked. You can still select pictures by tapping or dragging, so it's not a touch area issue. The same issue persists on the iPhone 11 simulator.
And I think I found the problematic part in my (sadly messy) ContentView.swift file;
ScrollView {
RefreshControl(coordinateSpace: .named("refresh")) {
await viewModel.refreshMediaItems()
}
LazyVGrid(columns: gridColumns, spacing: UIDevice.current.userInterfaceIdiom == .pad ? 12 : 4) {
let items = viewModel.filteredItems(typeFilter: mediaTypeFilter, specialFilter: specialFilter)
ForEach(Array(zip(items.indices, items)), id: \.1.id) { index, item in
MediaThumbnailView(
item: item,
isSelected: selectedItems.contains(item.id),
viewModel: viewModel,
onLongPress: {
if !isSelectionMode {
toggleSelectionMode()
selectedItems.insert(item.id)
}
},
onTap: {
if isSelectionMode {
toggleSelection(item: item)
} else {
viewModel.selectItem(item)
}
}
)
.aspectRatio(1, contentMode: .fit)
.background(
GeometryReader { geometry in
let frame = geometry.frame(in: .named("grid"))
Color.clear.preference(
key: ItemBoundsPreferenceKey.self,
value: [ItemBounds(id: item.id, bounds: frame, index: index)]
)
}
)
}
}
.padding(.horizontal, 2)
.coordinateSpace(name: "grid")
.onPreferenceChange(ItemBoundsPreferenceKey.self) { bounds in
itemBounds = Dictionary(uniqueKeysWithValues: bounds.map { ($0.id, $0) })
itemIndices = Dictionary(uniqueKeysWithValues: bounds.map { ($0.id, $0.index) })
}
.gesture(
DragGesture(minimumDistance: 0)
.onChanged { gesture in
if isSelectionMode {
let location = gesture.location
if !isDragging {
startDragging(at: location, in: itemBounds)
}
updateSelection(at: location, in: itemBounds)
}
}
.onEnded { _ in
endDragging()
}
)
}
.coordinateSpace(name: "refresh")
}
you can see the .gesture(.... part. I realised that this DragGesture and ScrollView blocks each other (somehow only on iPhone 11)
highPriorityGesture also won't work.
When I change it with simultaneousGesture, scroll starts to work again.
BUT - since it's simultaneous, when multiple selection mode is activated, when I'm dragging my finger gallery also starts to scroll and it becomes a very unpleasant experience. After this issue I realised on native gallery iOS locks scroll when you are dragging for multiple selection and just when you release your finger you can scroll again even if the multiple selection mode is active.
I tried a million things, asked claude, chatgpt etc. etc.
Found some similar issues on stackoverflow but they were all related to iOS 18, not spesific to an iPhone. My app works fine on iOS 18 (15 Pro Max)
iOS 18 drag gesture blocks scrollview
Here are the some of the things I've tried: using highPriorityGesture and simultenousgesture together, tried to lock the scroll briefly while dragging, implement much complicated versions of these things with the help of claude, try to check if isSelectionMode is true or not
All of them broke other things/won't work.
Probably there's something pretty simple that I'm just missing; but iPhone 11 being the single problematic device confuses me. I don't want to mess too much with my already fragile logic.
I have spent a long time refactoring lots of older Swift code to compile without error in Swift 6.
The app is a v3 audio unit host and audio unit.
Having installed Sonoma and XCode 16 I compile the code using Swift 6 and it compiles and runs without any warnings or errors.
My host will load my AU no problem.
LOGIC PRO is still the ONLY audio unit host that will load native Mac V3 audio units and so I like to test my code using Logic.
In Sonoma with XCode 16...
My AU passes the most stringent AUVAL tests both in terminal and Logic pro.
If I compile the AU source in Swift 5 Logic will see the AU, load it and run it without problems.
But when I compile the AU in Swift 6 Logic sees the AU, will scan it and verify it passes the tests but will not load the AU. In XCode I see a log message that a "helper application failed to run" but the debugger never connects to the AU and I don't think Logic even gets as far as instantiating the AU.
So... what is causing this? I'm stumped..
Developing AUv3 is a brain-aching maze of undocumented hurdles and I'm hoping someone might have found a solution for this one. Meanwhile I guess my only option is to continue using the Swift 5 compiler.
(appending a little note just to mention that all the DSP code is written in C/C++, Swift is used mainly for the user interface and also does some offline thready work )
Hi,,
How can I make a rectangle become taller as I swipe down on my trackpad?"
struct BlueRectangleView: View {
var body: some View {
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 200, height: 100)
.cornerRadius(10)
.shadow(radius: 5)
.padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
}
}
struct BlueRectangleView_Previews: PreviewProvider {
static var previews: some View {
BlueRectangleView()
}
}
Hi,
I'm trying to implement WeatherKit for the first time and I am seeing this error when calling for the weather:
Failed to generate jwt token for: com.apple.weatherkit.authservice with error: Error Domain=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors Code=2 "(null)"
and
Encountered an error when fetching weather data subset; location=<+51.57750785,-2.08241362> +/- 6.55m (speed -1.00 mps / course -1.00) @ 11/01/2025, 11:50:45 Greenwich Mean Time, error=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors 2 Error Domain=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors Code=2 "(null)"
I have:
Enabled the WeatherKit capability in my iOS app in XCode
Added Weatherkit to the AppID in the developer portal
Any suggestions what else I can check?
I'm running iOS 18.2.1 on 15 ProMax with XCode 16.2
The app I'm making uses the CallDirectory extension to identify callers.
Where is the contact data added by "func addIdentificationEntry" in "CallDirectory Extension" stored?
The app calls "CXCallDirectoryManager.sharedInstance.reloadExtension" and adds contact data by processing "CXCallDirectoryExtensionContext.addIdentificationEntry" written in "func beginRequest" of "CallDirectory Extension".
I thought that if I uninstalled the app, I would lose the contact data that added by App.
However, when I uninstalled the app and reinstalled it, it seemed to retain contact data that added by App before I uninstalled the app,
even though the app hasn't added any contact data yet.
Will the contact data added to the "CallDirectory Extension" be retained on iPhone even after uninstalling the app?
---Note---
I found a similar post on the forum about contact data added by addIdentificationEntry.
・CallKit - addIdentificationEntry is it secure?
[https://developer.apple.com/forums/thread/662182]
Answer of this post that "the added data is cached in a SQLite database",
but I couldn't find any official Apple documentation on this, so I'm not sure if this is correct.
If contact data is cached in SQLite, will that data remain on the iPhone when uninstalled and not deleted?
●Development environment
Mac OS Sonoma 14.7.1
Xcode 16.0
Swift
●Test Device
iPhoneSE3
iOS:17.6.1
Description
When you try to open an iOS app using Universal links from another app while the opening app is being updating, Safari will open an error screen(app will not launch).
Is this a specification of universal links on iOS?
Also is it possible for the Main app to detect that the application trying to open Universal Link is being updating?
Devices
iPad 9thGen/10thGen
I want to make my buttons inline with each other, but spaced apart, a bit like the apple topbar BUT in swift.
My code:
struct NormalPageView: View {
var body: some View {
VStack {
NavigationView {
Form {
Section {
Image(systemName: "house.fill")
Spacer()
Image(systemName: "plus")
Spacer()
Image(systemName: "gearshape.fill")
}
}
}
The AVCam sample code by Apple fails to build in Swift 6 language settings due to failed concurrency checks ((the only modification to make in that code is to append @preconcurrency to import AVFoundation).
Here is a minimally reproducible sample code for one of the errors:
import Foundation
final class Recorder {
var writer = Writer()
var isRecording = false
func startRecording() {
Task { [writer] in
await writer.startRecording()
print("started recording")
}
}
func stopRecording() {
Task { [writer] in
await writer.stopRecording()
print("stopped recording")
}
}
func observeValues() {
Task {
for await value in await writer.$isRecording.values {
isRecording = value
}
}
}
}
actor Writer {
@Published private(set) public var isRecording = false
func startRecording() {
isRecording = true
}
func stopRecording() {
isRecording = false
}
}
The function observeValues gives an error:
Non-sendable type 'Published<Bool>.Publisher' in implicitly asynchronous access to actor-isolated property '$isRecording' cannot cross actor boundary
I tried everything to fix it but all in vain. Can someone please point out if the architecture of AVCam sample code is flawed or there is an easy fix?
Hello everyone, I have a small error 23. I wanted to know if you also already have this error so in the simulator it works, I have no error, but once I do in the TestFlight I have the error that appears error 23, can anyone help me on this? Thanks a lot.
I am trying to give bottom padding to tabbar i.e ** tabBarFrame.origin.y = view.frame.height - tabBarHeight - 30** but it is not moving up from bottom, it gets sticked to bottom = 0 and the tabbar content moving up taher than tabbar itself..
Code snippet is -
`i override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let tabBarHeight: CGFloat = 70 // Custom height for the capsule tab bar
var tabBarFrame = tabBar.frame
tabBarFrame.size.height = tabBarHeight
tabBarFrame.size.width = view.frame.width - 40
tabBarFrame.origin.y = view.frame.height - tabBarHeight - 30
tabBarFrame.origin.x = 20
tabBar.frame = tabBarFrame
tabBar.layer.cornerRadius = tabBarHeight / 2
tabBar.clipsToBounds = true
view.bringSubviewToFront(tabBar)
}`
Can anyone please help to resolve the issue for iOS 18, it is coming in iOS 18 rest previous versions are fine with the code.
Hi everyone,
I’m working on an iOS app using both UITableViewDiffableDataSource and SwiftUI, and I’m facing two separate but puzzling issues:
UITableViewDiffableDataSource Not Reusing Cells on first applying after initial Snapshot. After applying first time it is working as expected from second time.
SwiftUI View’s inside UITableViewCell onDisappear Not Triggering the on first changes of snapshot after initial snapshot.
With normal UITableView it is working fine.
Issue causing - it is causing player & cells to retain memory extensively
Sample gist code for reproducing with diffable (DiffableTableViewExampleViewController) and working fine without diffable (RegularTableViewExampleViewController)
https://gist.github.com/SURYAKANTSHARMA/d83fa9e7e0de309e27485100ba5aed17
Any insights or suggestions for these issues would be greatly appreciated!
Thanks in advance!
Hi All,
I have a finder sync extension that passes data back to my main app. It currently writes to a plist file in my group container folder. Since updating to macOS 15, I have been getting this pop-up every time I trigger this writing to the plist after the finder sync extension loads.
This is how I write to the plist from my finder sync extension:
let appGroupDefaults = UserDefaults(suiteName: "group.team_id.Finder-Sync-Extension-Test-Project")
let items = FIFinderSyncController.default().selectedItemURLs()
DispatchQueue.main.async {
let url = items?.first?.absoluteString
var file = items?.first?.lastPathComponent
if let defaults = appGroupDefaults{
defaults.set(url, forKey: "targetURL")
defaults.synchronize()
}
self.showWindow(with: NSExtensionContext())
}
This is how I read the plist from my main app:
if let defaults = UserDefaults(suiteName: "group.team_id.Finder-Sync-Extension-Test-Project") {
defaults.synchronize()
if let clickedUrl = defaults.string(forKey: "targetURL") {
window = NSWindow(contentRect: NSScreen.main?.frame ?? .zero,
styleMask: [.miniaturizable, .closable, .resizable, .titled],
backing: .buffered,
defer: false)
window?.title = "My App"
window?.makeKeyAndOrderFront(nil)
textField.stringValue = clickedUrl
window?.contentView?.addSubview(textField)
}
}
It is fine if this popup happens once and the user's choice gets remembered. I just don't want it to happen every time.
Any help on if this is the correct way to pass data between the finder sync extension and the main app or on how to get macOS to remember the choice of the user would be great.
Thanks,
James
I have an app that tracks Wi-Fi and Cellular data usage. Many users want to see tracking per sim, and I can't find a way to achieve that. It seems like iOS reports data usage from every sim into pdp_ip0 interface.
Is there any place to look into?
I must be missing something here. I want to put a landscape image in a geometry reader that contains a ZStack that contains an image and an overlay centred on top of the Image.
I would like the ZStack and GeoReader's sizes to be the size of Image. (ie I want geometry.size to be the size of the image, which can be used to control the offset of the overlay's position.)
Unfortunately the ZStack also includes the space above the image (ie the top safeArea) and the GeometryReader also includes all the space below the Image. (so geometry.size.height is greater than the height of Image)
I've gone down rabbit holes of adding other items above/below, but I don't seem to be able to prevent the GeometryReader from being vertically greedy.
eg the Text(" ") above the ZStack in the VStack solves the ZStack claiming the top safe area. But adding Text(" ") below the ZStack does not prevent the GeometryReader from claiming more vertical space below the image.
Any/all guidance greatly appreciated.
struct ContentView: View {
var body: some View {
VStack {
// Text(" ")
GeometryReader { geometry in
ZStack {
Image(
uiImage: .init(imageLiteralResourceName: "LandscapeSample")
)
.resizable()
.aspectRatio(contentMode: .fit)
Text("Hello, world!")
.background(.white)
}
.background(.red)
}
.background(.blue)
// Text(" ")
}
}
}
Hello together,
since Xcode Version > 15 the following error handling causes following error "Pattern of type 'DecodingError' cannot match 'Never'
func getSupportedCountries() async {
// fetch all documents from collection "seasons" from firestore
let queryCountries = try? await db.collection("countries").getDocuments()
if queryCountries != nil {
self.countries = (queryCountries!.documents.compactMap({ (queryDocumentSnapshot) -> Country? in
let result = Result { try? queryDocumentSnapshot.data(as: Country.self) }
switch result {
case .success(let country):
if let country = country {
// A country value was successfully initialized from the DocumentSnapshot
self.errorMessage = nil
return country
}
else {
// A nil value was successfully initialized from the DocumentSnapshot,
// or the DocumentSnapshot was nil
self.errorMessage = "Document doesn't exist."
return nil
}
case .failure(let error):
// A Country value could not be initialized from the DocumentSnapshot
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
return nil
}
}))
} else {
self.errorMessage = "No documents in 'countries' collection"
return
}
}
the interesting part of the code where XCODE shows an error is from "switch error" downwards.
Does anyone of you have an idea what's wrong?
Ay help appreciated !
Thx, Peter