Can you use Package.swift in an iOS app instead of using .xcodeproj?

Viewed 1879

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 Answers

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.

Related