How to Install Package in Xcode via Swift Package Manager

Swift: Install package so available to any Xcode Project?

This is not something that Swift supports at the current time.

What you could do is clone the Git repo for a project, and then reference it as a local dependency, but I wouldn't recommend this.

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

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

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.

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