17

Is it posible to integrate local dependencies with SPM in xcode 11, File > Swift Packages > Add Package dependency... It only seems to populate and let me select git repos hosted on bitbucket

I am just not sure if I am approaching the issue the correct way, maybe I should be using a Package.swift file instead? I have a lot of experience with Cocoapods but I would like to branch out and try other tools, especially if they are going to be supported in xcode as it seams SPM will be (Im using xcode11 beta 5)

user6252584
  • 229
  • 2
  • 8

5 Answers5

16

This is the way I did it :

  • Drag and drop your package folder (in my example "DataStructures") from the finder directly into the Frameworks group of your target. You will see that the dropped item take a brown folder color (you can use the arrow to "get into it").
  • Go to your project target page, in the "Framework and Libraries" click the "+" button. Your package should show up in the "Workspace" area as a library.

enter image description here

AirXygène
  • 2,409
  • 15
  • 34
  • 2
    Would like to add a note that added package doesn't have an arrow to expand its content in case if it's opened in an another window of Xcode, just the same as it would be with sub-project. This confused me the first time. – Olexandr Stepanov Oct 07 '19 at 08:02
  • 2
    Not only that, but if your package's project is open, you won't see your package under "Workspace" in "Frameworks, Libraries, and Embedded Content". – Piotr Byzia Dec 17 '19 at 09:56
  • @AirXygène I did this way and works well for me. when I drag package into my project. but I have to go one more step. I want to have one of my package into another package. Both are at local path. Any idea how I can do this, because drag and drop one package into other didn't worked for me, and I also tried to have local path at dependancies and it removes my target and all. Any help will be appreciated. – Sagar Jun 05 '20 at 12:13
  • 1
    @Sagar You can’t have nested packages. But you can have packages that depend on other packages, and you therefore add all packages at the same level. You declare dependencies in the client package manifest file. – AirXygène Jun 05 '20 at 14:39
  • 2
    it's important to mention that you have to make sure that the package being added, ins't already opened in other Xcode editor....because Xcode :) – Mostfa Essam Aug 25 '22 at 11:56
6

Xcode 12

If you have a remote version of your package already in the project and want to work on a local. There are a few extra steps...

1. Make sure your local package's version is higher than the remote version

If you don't do that, it will continue to fetch the remote package

  1. Select your project in Project Navigator
  2. In the main window select Swift Packages
  3. Select the package you want to replace with a local and remove it
  4. Go to File > Swift Packages > Add Package Dependency...
  5. Drag the your local package folder to the package search bar and add the prefix file://
  6. Click on Next

Confirm that the minimum version is higher than the remote one.

D. Greg
  • 991
  • 1
  • 9
  • 21
  • 1
    I needed to work with a local Package.swift and the recommendation of "Importing dependency by drag & drop" in this guide was what worked for me https://medium.com/@bartekzabicki/how-to-add-dependency-spm-into-project-using-xcode11-84a2914f8f1 – kikeenrique Mar 02 '21 at 11:14
  • 1
    Note that you will technically need three slashes in the URL: file:///Users/name/... – Maxwell Apr 01 '21 at 20:38
2

Yep, Xcode makes this rudimentary task unnecessary difficult.

The trick is simple, though: in the sheet that pops up when you select 'Add Package Dependency…', in the URL field, you can enter a file:// URL. e.g. drag the package's folder into that field, which will place the path in it - e.g. '/Users/me/Documents/myShinyPackage/', then stick 'file://' to the front of it, and you'll be able to proceed.

Addendum: as some folks note in the comments below, this is problematic if you share the project with others, unless you standardise on paths. For individual use, however, it's fine.

Wade Tregaskis
  • 1,996
  • 11
  • 15
  • 3
    This solution is completely incompatible with version control systems. – Dmitry Serov Dec 21 '19 at 15:56
  • This should be the accept answer! If you git clone the repo locally, this solution works perfectly. – zrfrank May 22 '20 at 07:04
  • You cannot specify a relative path in the "file://" URL. I tried "./dir" and "$(PROJECT_DIR)/dir" but no luck. So ya, you cannot commit this to a repo and expect it to work for someone with a different path to their working directory. – Reid Ellis Dec 09 '20 at 18:59
2

Local Swift Package Manager(SPM)

[Dependency manager]

Key point is: file:// + git which you can use it in URL field

file://<path>
//e.g.
file:///Users/alex/Desktop/MyProject

Notes:

  • It works with git repository that is why please make sure that you committed your changes beforehand
  • when you add a SPM dependency File -> Swift Packages -> Add Package Dependency it is added into project -> Swift Packages and implicit dependency[About] is added to Frameworks, Libraries, and Embedded Content
  • when you committed new changes into framework do not forget File -> Swift Packages -> Update to Latest Package Versions

Product folder

<path_derived_data>/<randomizer>/Build/Products/Debug
//e.g.
/Users/alex/Library/Developer/Xcode/DerivedData/SPMExperiments-bmmqumztjedpekaajffnaegxzwzg/Build/Products/Debug

[Local CocoaPods]
[Local Carthage]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
  • Trying to fetch from a local repo (bare git), it keeps complaining that it fail to resolve... Not sure it's possible. Why does the remote have to be online? – bauerMusic Nov 12 '21 at 09:47
0

A new way(2022.11): Simply drag the local package repo to your main project Like this

Note: if you have that package open in another Xcode project you have to close it first.

By this way, Xcode will ignore the remote package in SPM. You can edit the local package repo directly and use it in main project.

Ethan.Li
  • 31
  • 3