I had been adding Swift package dependencies to my iOS app project using File -> Swift Packages -> Add Package Dependency. But is it possible to use a Package.swift instead? I thought I would just be able to drop in a Package.swift and it would start using that to resolved my packages. I'm using Cocoapods too, so I can't generate a new xcodeproj file.
1 Answer
To manage your project's swift packages dependencies with Package.swift file you should make next steps:
- Create a workspace that has your project (if you don't have)
- Create empty swift package e.g. "Dependencies" inside your project's folder
- Add the package to your workspace (just drag and drop the folder with swift package)
- Link your project's target with "Dependencies": "Build Phases -> Link Binary With Libraries"
- Add your dependencies to Package.swift:
let package = Package(
name: "Dependencies",
...
dependencies: [
.package(url: "https://github.com/username/SwiftPackage.git", .exact("1.1.0")),
],
targets: [
.target(
name: "Dependencies",
dependencies: ["SwiftPackage"]),
...
]
)
Now all swift package dependencies are available in your project automatically after editing Package.swift file.
UPDATE
There is the second much stable option to add Dependencies swift package to your project:
- Create an empty swift package e.g. "Dependencies" inside your project's folder.
- Add your dependencies to Package.swift (see above).
- Create a local git repository in "Dependencies" folder and commit all files to
master. - Add "Dependencies" swift package in "Swift Packages" tab of your project's settings by providing a full path
file:///Users/.../Dependencies(Xcode allows adding by a full path only) and point it tomasterbranch. - If you need to use a relative path instead just do next trick: change
XCRemoteSwiftPackageReference > repositoryURLvalue manually tofile://Dependenciesin your project.pbxproj file and then you can see:
NOTE: you need to push all changes to master each time after changing your dependencies in Package.swift file to allow your project to see them.
3 Comments
Marty
I'm trying to avoid packages being added to the xcodeproj directly, as it seems to be very unstable and has been causing issues for me for a while. Packages that are listed in the "Swift Package Dependencies" section (remote packages) always work flawlessly. My workaround has been to make a new branch remotely, push changes to that, and point the app's package dependency to that branch. But I would rather have a Package.swift manifest file that I can write in Swift and play with.
iUrii
You can also use local git repository instead of pushing to a remote. To add one set full path to your swift package folder e.g.
file:///Users/.../Dependencies (XCode allows to add by a full path only) . And after there is a trick to make a relative path: change XCRemoteSwiftPackageReference > repositoryURL value to file://Dependencies in your project.pbxproj file.Marty
I guess that's the way to do it. Unfortunately you have to remove the remote reference and replace it with the local in the project file, but this works for what I need. If you make it an answer I'll award the bounty.
Package.swiftof this package, but I'm not sure how to make this work with CocoaPods.