23

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.

4
  • 2
    No, you can't use Swift Packages for iOS apps without having an Xcode project, it is needed for code signing, for example – SwiftPM can't do that. Commented May 21, 2020 at 20:56
  • 3
    Right that's what I want to do. I want to us an xcodeproj still, but I want to manage the Swift Package dependencies with a Package.swift manifest, not the interactive package editor in xcode. Commented May 22, 2020 at 21:02
  • Then you could probably add a new Swift Package to your Xcode project and declare all your dependencies in the Package.swift of this package, but I'm not sure how to make this work with CocoaPods. Commented May 25, 2020 at 8:33
  • I hope so. However, the answer is negative: Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects Commented Apr 24, 2022 at 7:47

1 Answer 1

10
+100

To manage your project's swift packages dependencies with Package.swift file you should make next steps:

  1. Create a workspace that has your project (if you don't have)
  2. Create empty swift package e.g. "Dependencies" inside your project's folder
  3. Add the package to your workspace (just drag and drop the folder with swift package)
  4. Link your project's target with "Dependencies": "Build Phases -> Link Binary With Libraries"
  5. 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:

  1. Create an empty swift package e.g. "Dependencies" inside your project's folder.
  2. Add your dependencies to Package.swift (see above).
  3. Create a local git repository in "Dependencies" folder and commit all files to master.
  4. 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 to master branch.
  5. If you need to use a relative path instead just do next trick: change XCRemoteSwiftPackageReference > repositoryURL value manually to file://Dependencies in your project.pbxproj file and then you can see: Relative path to local swift package

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.