Swift Packages

RSS for tag

Create reusable code, organize it in a lightweight way, and share it across Xcode projects and with other developers using Swift Packages.

Posts under Swift Packages tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

SPM Resolution Challenges
I've been relying on SPM to manage some packages related to a project I'm working on, and while it mostly worked well when the dependencies were small, it has become increasingly difficult to make updates to package versions and have Xcode use those updates (now around 20 total depencies). For example, suppose I have this working state: Package A (private in Github) -> tag 1.2.2 Xcode SPM 'Up to Next Major Version" -> 1.00 < 2.00 Now, I want to update package A to be a new version (e.g. 1.3.0). I update the package, give it a new tag and push the tags to git. Now, if I open my project without performing any action, the package version will almost always not update. Things I can do: 'Update Package' from the Package Dependency list. (Has no effect) 'Update to latest package versions' from the Package Dependency header. (Has no effect) Resolve package versions / Reset package caches (Has no effect) What used to work in the past was to remove the dependency in Xcode, and re-add it. 90% of the time, it wouldn't add on first try, so I would have to try to add it, cancel out of the dialog once, and try again. That used to work. Now, a new package version won't add at all. I just get the 'Unable to resolve dependencies' dialog. 'Add anyway' leaves my project in an unusable state. I'm now in a position where I have to close, clean, restart, reset caches, etc for long stretches of time until with some stroke of luck the versions update correctly and the project will build. This is hugely disruptive to my development flow and am hoping to find a reliable solution that doesn't take 30+ minutes of rebuilding and retrying before the project is in a usable state with my updated package.
0
0
52
1d
Nested xcframeworks in SPM
My organization delivers an xcframework, let's call it MyFramework.xcframework. This framework also depends on another local framework, SecondFramework.xcframework. We want to deliver the MyFramework using Swift Package Manager to our customers. The SecondFramework is embedded within MyFramework. Then the customer only needs to care about MyFramework. Our issue is that apps using our Swift Package fail to recognize the SecondFramework, displaying the message "SecondFramework module not found." Is this not possible or currently supported in SPM? Or do we need to change our approach? I appreciate any help or pointers. Thanks!
1
0
88
2d
What is the purpose of `originHash` in Package.resolved
Whenever I switch branches, Xcode seems to update the originHash field in Package.resolved. I then have to tell Xcode to manually resolve packages again. That sounds like a bug, and I can just file a bug about that. But what I want to know is… why is the originHash changing in the first place? What does that represent? Should I be committing the value that it's trying to change to? Or should I be discarding that change?
0
2
156
1w
Xcode NSMetaDataQuery error on device running IOS 17.5 - [ERROR] couldn't fetch remote operation IDs
Xcode 15.4 running on various IOS simulators and hardware devices from IOS 14.5 to 17.5. Part of my code presents a backup/restore page to the user which uses NSMetaDataQuery to update the GUI for files being uploaded or downloaded in iCloud. On every device I run the code everything works as expected EXCEPT one which is an iPhone 11 running IOS 17.5 (as of yesterday 17.5.1); there I get the following error once I start the query: [ERROR] couldn't fetch remote operation IDs: NSError: Cocoa 257 "The file couldn’t be opened because you don’t have permission to view it." "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)"" Due to this error I am getting no query updates and thus unable to display whether the file needs to upload, download or is synchronised. I am not initiating any upload or download of the backup file since it is placed in the ubiquitous container and I leave the up/download of the file over to IOS; all I do with the query is monitor the status of the file and take appropriate action to show the user the percentage of up/downloaded file. As said before it is only the one device causing me headaches so I don't know whether it has anything to do with IOS 17.5 that Apple have made changes that I am unaware of. I have access to an iPhone and an iPad running some version of IOS 16 and it's performing flawlessly. I have no other IOS17+ device to test on. The code runs very well on any 17.5 simulator, but we all know there are always some differences running code on a Device vs Simulator. Running 'startAccessingSecurityScopedResource()' which has been suggested by some returns 'true' on the simulator and 'false' on a device, but even then all devices work except one; so that does not seem to be the solution. Changing the query predicate has not helped either. How do I drill down to find the culprit - I'm at my wits' end. My very simple query initializer, startup & observers: (Please note, the code shown here is what's left after commenting out everything else. This was done to show that the problem really DOES lie with the Query) query = NSMetadataQuery.init() query.operationQueue = .main query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope] query.predicate = NSPredicate(format: "%K LIKE %@", NSMetadataItemFSNameKey, fileUrl.lastPathComponent) query.operationQueue?.addOperation({ [weak self] in self?.query.start() self?.query.enableUpdates() }) } func addNotificationObservers() { NotificationCenter.default.addObserver( self, selector: #selector(queryDidStart(_:)), name: .NSMetadataQueryDidStartGathering, object: query) NotificationCenter.default.addObserver( self, selector: #selector(queryGathering(_:)), name: .NSMetadataQueryGatheringProgress, object: query) NotificationCenter.default.addObserver( self, selector: #selector(queryDidUpdate(_:)), name: .NSMetadataQueryDidUpdate, object: query) NotificationCenter.default.addObserver( self, selector: #selector(queryDidFinishGathering(_:)), name: .NSMetadataQueryDidFinishGathering, object: query) }
3
0
124
2d
How to exclude RealityKitContent from Swift package for iOS?
I've created an app for visionOS that uses a custom package that includes RealityKitContent as well (as a sub-package). I now want to turn this app into a multi-platform app that also supports iOS. When I try to compile the app for this platform, I get this error message: Building for 'iphoneos', but realitytool only supports [xros, xrsimulator] Thus, I want to exclude the RealityKitContent from my package for iOS, but I don't really know how. The Apple docs are pretty complicated, and ChatGPT did only give me solutions that did not work at all. I also tried to post this on the Swift forum, but no-one could help me there either - so I am trying my luck here. Here is my Package.swift file: // swift-tools-version: 5.10 import PackageDescription let package = Package( name: "Overlays", platforms: [ .iOS(.v17), .visionOS(.v1) ], products: [ .library( name: "Overlays", targets: ["Overlays"]), ], dependencies: [ .package( path: "../BackendServices" ), .package( path: "../MeteorDDP" ), .package( path: "Packages/OverlaysRealityKitContent" ), ], targets: [ .target( name: "Overlays", dependencies: ["BackendServices", "MeteorDDP", "OverlaysRealityKitContent"] ), .testTarget( name: "OverlaysTests", dependencies: ["Overlays"]), ] ) Based on a recommendation in the Swift forum, I also tried this: dependencies: [ ... .package( name: "OverlaysRealityKitContent", path: "Packages/OverlaysRealityKitContent" ), ], targets: [ .target( name: "Overlays", dependencies: [ "BackendServices", "MeteorDDP", .product(name: "OverlaysRealityKitContent", package: "OverlaysRealityKitContent", condition: .when(platforms: [.visionOS])) ] ), ... ] but this won't work either. The problem seems to be that the package is listed under dependencies, which makes the realitytool kick in. Is there a way to avoid this? I definitely need the RealityKitContent package being part of the Overlay package, since the latter depends on the content (on visionOS). And I would not want to split the package up in two parts (one for iOS and one for visionOS), if possible.
0
0
183
1w
Weird Swift overriding cocoapods
I don't even use Swift. I use Flutter. I am getting this odd always_embed_swift library warning that seems to be overriding my runner target on xcode. I'll just attach the image down below of what I'm dealing with. Please an apple engineer or someone just help me because this has been a struggle lol. I need to change this but can't find the setting to do so.
0
0
118
1w
How might I get didSet behaviour on an AppStorage var?
I've defined a value stored in UserDefaults. In a view struct I have code that can successfully update the stored value. I've also added an @AppStorage var in an instance of a class, that can read this value and run business logic that depends on the current stored value. But what I really want to do, is have code in my class that gets automatically called when the value stored in UserDefaults gets updated. Basically I want to do this: @AppStorage("languageChoice") var languageChoice: LanguageChoice = .all { didSet { print("hello") } } Unfortunately didSet closures in @AppStorage vars do not appear to get called :-( My clumsy attempts to use combine have all ended in tears from the compiler. Any/all suggestions are greatly appreciated. thanks, Mike
2
0
148
1w
xcodebuild -resolvePackageDependencies stopped working after Mac Os update
My config is as follow MacBook Pro with M3 Pro and Sonoma 14.5 OS XCode 15.2 Build version 15C500b swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5) Target: arm64-apple-macosx14.0 Darwin lonwx2049951 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:13:18 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6030 arm64 I am behind corp proxy and have quite limited privileges to install or use any tools/commands requiring admin privileges. The App itself plus a few company submodules are hosted on Azure Devops and this side works as usual. The App also uses a few dependencies hosted on GitHub and that need to be resolve every time I'm switching/creating new branches Until Monday I was using following terminal command to do so xcodebuild -resolvePackageDependencies -scmProvider system However, on Monday when I tried the above I was presented with error on each of the dependencies, see attached depenedencies_error.txt The only change that happen was the Mac OS update on Monday morning when I turn on my Mac from 14.4 to 14.5 Is it possible that this update removed some of the settings? I have tried the following: deleting DerivedData folder, cleaning and rebuilding project cloning the dependencies manually adding PAT token using SSH for fetching deleting Package.resolved file cloning project from Azure repo into different location Any suggestions will be appreciated.
1
0
140
1w
Xcode 15.4 - Missing package product "<package name>"
Hello, I'm having this issue that Xcode tells me it cannot find the Swift Packages I'm using within my App. When I first open Xcode, all the Package Dependencies are listed correctly in my project navigator. Also, building the app for the first time runs without any issues. But right after the first build is finished, the Package Dependencies are not listed any more and the build fails, telling me that all the packages are missing. I have to manually click on File -> Packages -> Reset Package Caches, after that it works again. But only for exactly one build. I basically have to do this step for every single build. I found out that there is this Package.resolved file which lists all the used packages with their respective version and some kind of hash. This file is located inside the .xcodeproj file, in my case it's this location: MyApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved This file is deleted after the successful build. And I think that's the reason why all of a sudden Xcode screams that the packages are missing. After resetting the package caches this file re-appears. But it seems it has to be regenerated every single time. This bug is VERY annoying and exists already since the last view versions of Xcode, not just the latest one. I was hoping this bug is fixed at some point. Current Xcode version: 15.4 (15F31d) MacBook Air 13", M3, 2024 running macOS Sonoma 14.5 Just in case, my Package.resolved file is attached below. Package.resolved
0
1
269
1w
SwiftUI Preview - Undefined symbols for architecture arm64
Starting on Xcode 15.3 and 15.4 my SwiftUI Previews stopped working with the following error: == PREVIEW UPDATE ERROR:     LinkDylibError: Failed to build ContentView.swift          Linking failed: linker command failed with exit code 1 (use -v to see invocation)          ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found     Undefined symbols for architecture arm64:       "static MyLibrary.DisplayUtil.getText() -> Swift.String", referenced from:           closure #1 () -> SwiftUI.Text in (extension in Demo_Broken_Preview_PreviewReplacement_ContentView_1):Demo_Broken_Preview.ContentView.(__preview__body in _1BA320C8FB5388C953E1E463345C3D72).getter : some in ContentView.1.preview-thunk.o       "type metadata accessor for MyLibrary.DisplayUtil", referenced from:           closure #1 () -> SwiftUI.Text in (extension in Demo_Broken_Preview_PreviewReplacement_ContentView_1):Demo_Broken_Preview.ContentView.(__preview__body in _1BA320C8FB5388C953E1E463345C3D72).getter : some in ContentView.1.preview-thunk.o     ld: symbol(s) not found for architecture arm64     clang: error: linker command failed with exit code 1 (use -v to see invocation) After much trial and error, I narrowed it down to my SwiftUI views using properties or functions inside components in Swift Packages. This is a simple example of my Swift Package: public class DisplayUtil {     public func getText() -> String {         return "Text"     }          public static func getText() -> String {         return "Text"     } } And my SwiftUI View import SwiftUI import MyLibrary struct ContentView: View {     var body: some View {         VStack {             Text(DisplayUtil.getText())         }         .padding()     } } #Preview {     ContentView() } The same code works fine on Xcode 15.2 Link to the sample project: https://www.icloud.com/iclouddrive/0c00AD0esi2PwqqiRggokfwGw#Demo%5FBroken%5FPreview Is anybody else having a similar issue?
1
0
230
2w
Swift Package not able to add into the project
Hi There, I have a strange problem: the Swift package cannot be added to my iOS project. The problem might be due to the project settings, but despite trying for a long time, I still haven't been able to figure it out. My project is a React Native project and uses CocoaPods. When I drop the Swift package into my React Native project, the package becomes a folder. When I drop the Swift package into other iOS projects, it works without any problems. If I try to use "Package Dependencies" to add the Swift package, it gets stuck on "Preparing to validate..." My project seems unable to add any Swift packages; it cannot add other Swift packages either. The Swift package is local. However, it works fine in other projects, even in newly created React Native projects. The problem might be with my project itself, but I have no idea where to check now. please guide me with any advice, I will appreciate it!
0
0
127
2w
Unable to import ObjC interface from a package in a public ObjC file in a framework
Problem Statement: Unable to import .h file from an ObjC SPM to a .h file in an ObjC file in a framework with mix of ObjC and Swift code The issue is: in order to support access of ObjC file in Swift code in a framework we need to use umbrella header (in place of bridging header). Once the file is imported in Umbrella header and made public it will no longer allow import of .h file from package in its interface Project Structure: a. Package: ObjCPackage ObjCPackage |- Package.swift |- ObjCPackage MathsUtilities.h (class interface) MathsUtilities.m (class implementation) NiceLogs.h (protocol) b. Project: ObjCSwiftFramework ObjCSwiftFramework |- ObjCSwiftFramework.h (umbrella header) |- Calculation.h (objc class interface) |- Calculation.m (objc class implementation) |- SwiftCalci.swift (swift class) Details: #import <ObjCSwiftFramework/Calculation.h> added in ObjCSwiftFramework.h as Calculation has to be used in SwiftCalci Calculation.h marked as public in target membership in Xcode so that it can be added in umbrella header ObjCSwiftFramework.h #import "NiceLogs.h" in Calculation.h gives error Here is a small sample which I created to demonstrate the problem: ObjCSwiftFramework
0
0
137
2w
My game (made with unity) becomes unplayable due to the scrolling shortcuts in the safe area at the bottom.
Hello. My game, which I have been preparing for a long time on Unity, has been completed. But I have a big problem. My game is a game focused on constantly drawing lines. For this reason, I need to draw lines in the safe area at the bottom of the iPhone. However, due to the shortcuts on the iPhone, my game is constantly put in the background or switched to another application. Since there are so many levels in my game, it is almost impossible to redesign the levels and since I am a game with lines drawn, I will always have this problem again. What I want is to cancel the shortcut to switch to another application and put it in the background when I swipe from left to right or right to left when my game opens. When my game opens, the priority is on the lines in my game. I also marked the defer system gestures on edges section on Unity for postponement, but it did not help. Also, postponing it will not solve the problem. For example, they disabled these shortcuts in the (mini metro) game and you can never use these shortcuts in the game. I did a lot of research on forum sites and chatgpt and tried different codes, but I couldn't find a solution. Can you help me with this? When my game is opened, I want to prevent and cancel the transition to another application by drawing a line left and right in the safe area section at the bottom and the game from being placed in the background. I would be very happy if you could help me with a hint or a code on what to do.
0
0
213
2w
iOS Bluetooth Slow to Send information
On versions iOS 14+, my app which is connected by bluetooth to another device will become slow to write information. This isn't a couple hundred ms or a second or two, but upwards of 10 minutes for it to be sent and then 400 ms to be processed by the device and send its response back. I've confirmed with PacketLogger and with system logs that what commands we are sending with .writeValue are taking over 10 minutes. This is the timestamp of when we are writing to the device This is the PacketLogger timestamp of when the phone actually wrote to our device This doesn't occur immediately, but is intermittent and once it does begin to occur, there's nothing to do to get out of it except for connecting once again to the device. Which, overtime, the issue begins to spring up again. So why is it taking 13 minutes for it to go through the phone and finally being sent?
1
0
190
3w