Adding Swift 3 Packages to Xcode 8 Using the Swift Package Manager

Adding Swift 3 packages to Xcode 8 using the Swift package manager

Xcode and the SPM can work together, but as far as I can tell you do need to take one step on the command line.

Put your package manifest file into the same directory as the Xcode project, and then invoke swift package generate-xcodeproj

The package manager will pull down your dependencies and rewrite the .xcodeproj file to refer to them.

It will preserve any existing source, but the directory structure will be reconfigured to SPM's preferred arrangement:

PROJECT_DIR
├── Sources
│ └── ProjectName
│ ├── YourCode.swift
│ └── YourOtherCode.swift
├── Dependencies
│ └── SomeDependency
│ ├── DependencyCode.swift
│ └── OtherDependencyCode.swift
└── Package.swift

N.B., I haven't tested this extensively on a live project; given the fact that SPM docs still say WIP, please make sure you've made a recent commit.

Add forked Swift Package in Xcode project

I finally worked around this limitation by manually editing both the project.pbxproj and Packages.resolved files, so that they point to the specific commit in the fork of the repository.

To do this, close Xcode and then open the two files with a plain text editor.

In the project.pbxproj file, change the url of the repo and the parameters needed to correctly specify the version rule. In my case, I wrote the hash of the commit I needed:

/* Begin XCRemoteSwiftPackageReference section */
7902F77227C64GF9001583F1 /* XCRemoteSwiftPackageReference "Cuckoo" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/ajpallares/Cuckoo";
requirement = {
kind = revision;
revision = a9d239ff1bb93fe0204f8285d513f3139b51fbbb;
};
};

Do the same for the Packages.resolved file:

{
"package": "Cuckoo",
"repositoryURL": "https://github.com/ajpallares/Cuckoo",
"state": {
"branch": null,
"revision": "a9d239ff1bb93fe0204f8285d513f3139b51fbbb",
"version": "null"
}

Obviously, this is not the ideal solution but at least it works ¯\(ツ)

In fact, this seems to be an intended limitation of Swift Package Manager. See:

  • https://forums.swift.org/t/dependency-mirroring-and-forking/13902
  • https://forums.swift.org/t/replace-dependency-in-graph-with-a-fork/39718

How to use Swift Package Manager(SPM) in cli?


I have followed those steps to solve the problem.

  1. set git HTTP proxy
git config --global http.proxy http://0.0.0.0:1087
git config --global http.proxy socks5://0.0.0.0:1080

  1. set ssh proxy for host

Append those lines at the end of ~/.ssh/config to add a socks5 proxy

Host github.com
ProxyCommand nc -X 5 -x 0.0.0.0:1080 %h %p

  1. use xcodebuild command to resolve package dependencies

Remember you must add the -scmProvider system option to force xcodebuild to use system git and git configurations, or it will use the xcode built-in git which will never go through the proxy.

xcodebuild -resolvePackageDependencies -scmProvider system

Some references here:

  • Building Swift Packages or Apps that Use Them in Continuous Integration Workflows

Xcode: Using packages in Xcode 14

There is a change in the syntax for local packages, the .package(url: … syntax that used to work for packages within the project does not work anymore.

With Xcode 14 local packages that do no have their own repo MUST be referred as:

dependencies: [
.package(path: "../MyLibrary"),
]

Now this works just fine in Xcode 14 beta, but if you're going back to the Xcode release version, you'll get numerous errors. To fix those, reset the package caches by calling

File > Packages > Reset Package Caches

I had the same issue in a project with 14 internal packages, some of them dependent on each other, and it runs fine now in both Xcode 13.4 and Xcode 14 beta.

Hope it helps

How to update swift package imported to Xcode Project via Swift Package Manager?


Option 1: (Xcode 13 and above)

Right-click on the package from the left navigation pan and select Update Package

Xcode 13

Option 2:

Double click on the package in the tab you mentioned and change the version to anything else. It will then recheck the remote repo. The benefit of doing this is to only update the selected package. (Also, it's better to have the current using version be set in the package.)

Option 3:

From File -> Swift Packages -> Update to Latest Package Versions

SS



Related Topics



Leave a reply



Submit