How do I use Swift Package Manager with an existing macOS project? - swift

I've been using Cocoapods to manage dependencies for my Swift project. I came across this package which is not listed at Cocoapods. Instead it suggests using Swift Package Manager. However, whenever I try to use Swift Package Manager to do basically anything, it ends up completely destroying my entire project.
So, in order to figure out how to actually use Swift Package Manager, I'm playing with it in a test project.
Here's what I've tried:
Create a new project from Xcode
File -> New -> Project -> Cocoa App
Product Name: basic-ssh-test
This creates a basic application which loads a blank window when I hit "Run". Just for fun I added print("test") to the applicationDidFinishLaunching function in my AppDelegate so the debug window says "test" when I run the program.
Now I want to add the Shout package as a dependency.
swift package init --type executable
This creates the following files:
Creating executable package: basic-ssh-test
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/basic-ssh-test/main.swift
Creating Tests/
Now I'm going to add the "Shout" dependency to my new Package.swift file:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "basic-ssh-test",
dependencies: [
.package(url: "https://github.com/jakeheis/Shout", from: "0.2.0")
],
targets: [
.target(
name: "basic-ssh-test",
dependencies: ["Shout"]),
]
)
And I'll pull in the dependencies:
swift package resolve
This results in the dependencies being pulled into the .build directory:
Fetching https://github.com/jakeheis/Shout
Fetching https://github.com/jakeheis/CSSH
Fetching https://github.com/IBM-Swift/BlueSocket
Cloning https://github.com/IBM-Swift/BlueSocket
Resolving https://github.com/IBM-Swift/BlueSocket at 0.12.91
Cloning https://github.com/jakeheis/CSSH
Resolving https://github.com/jakeheis/CSSH at 1.0.3
Cloning https://github.com/jakeheis/Shout
Resolving https://github.com/jakeheis/Shout at 0.3.0
Now I regenerate the xcodeproj file:
swift package generate-xcodeproj
Now when I open the xcodeproj file there is a new group called Sources that has a single directory basic-ssh-test with a single swift file in it main.swift with print("Hello, world!").
The code that I'm actually interested in running is now in a blue folder called basic-ssh-test. All of the necessary files are still in there, but instead of running my application, Xcode is running the main.swift file. I can tell because the debug output is "Hello, world!" instead of "test".
I've read a couple of tutorials that claim that Swift Package Manager will move my source files and continue to build the same as before, but that's clearly not the case.
There's also no longer a "Main Interface" option in my Build Settings, so I can't select "MainMenu.xib" as my application starting point.
This is essentially the same thing that happens when I try to use Swift Project Manager with my existing project. It pulls in the dependencies, but it basically ignores my entire existing project and just runs the "hello world" code.
How do I use Swift Package Manager to add a dependency to an existing project without ignoring the existing codebase?

I think SPM is only compatible with Mac command line executables or libraries. This explicitly states SPM doesn't support iOS, watchOS, or tvOS platforms at all. But, since macOS AppKit/Cocoa application targets are very similar to iOS or tvOS Xcode targets, I would say this statement implies that SPM can't be used with macOS Cocoa applications out of the box either, which is what I think you're hoping for.
It looks like there is some work here on how to use it with iOS application targets which should largely translate to a macOS Cocoa target.

Until the time of this writing answer, Swift Package Manager do not support the iOS, tvOS, and watchOS. Instead, you will have to add the files from the package directly to your project.
I would suggest you creating a Dependencies group in your project and a group below that with the package name, like this answer:
So, first you will add the files dependency that you want to import into your project to the name of the package name that included in Dependencies group that we make before.
After you add the files then you can access the code as you usually would when you write it yourself like the image below. No need imports. You can see more details in here. hope it helps.

You can create a windowed application using SPM (I have a whole project sitting waiting for ABI that does this) you just need some boiler plate in swift.main. I’ll dig it out.
However the method others are suggesting is never going to work (with current versions of SPM).
However, what I am seeing is that you really want to use SPM to manage dependencies (keeping up to date etc). So I think you could do that much more easily.
Rather than creating a executable as your default target, instead create a library (perhaps call it basic-ssh-dependencies). Generate your Xcode project and drag THAT into your primary Xcode project and configure your target to to depend on it. When you update the library from SPM for changes in dependencies you should re-gen your Xcode-proj.
Let me know your mileage and any wrinkles, or if I’ve misunderstood what you are trying to achieve.

Update for XCode 11
This is A LOT easier now with XCode 11. As of this writing the current version of XCode is 11.3.1.
For an existing XCode project simply:
go to File->Swift Packages->Add Package Dependency...
enter the URL of the hosted package you want to add
choose the rules about which versions your app will use
click Finish

Related

Having trouble with adding a Swift package to MacOS 12.X+

I am writing my first MacOS App from the helloworld starter app. I want to eventually write swift code that calls a thrift server running on the same machine.. but this libraries instructions simply state to modify a file I don't seem to have.
Are these instructions only applicable to iOS ?
### Installing
#### Swift Package Manager
Add the following to your Package.swift file
.package(name: "TwitterApacheThrift", url: "https://github.com/twitter/ios-twitter-apache-thrift", .upToNextMajor(from: "1.0.0"))
#### Carthage
Add the following to your Cartfile
github "twitter/ios-twitter-apache-thrift"
Source : https://github.com/twitter/ios-twitter-apache-thrift
MY Screenshot of my files
I tried the add package dialog with the URL but got an error
You are not adding correct URL. It needs to be "https://github.com/twitter/ios-twitter-apache-thrift.git" instead of "https://github.com/twitter/ios-twitter-apache-thrift" . Means you need to add .git suffix. Also the instruction for installing that you posted
is meant for integrating thrift Package into another package as package dependency. If you just need to integrate the thrift package into your project, use the correct URL that I provided with standard way of adding package, as you tried before.

Change Swift Version Project is Built With

I'm using Vapor, and I've run into a problem where the change to Xcode 13.3 from 13.1 now means the project (without changes) will no longer build.
How can I change the version of Swift that a Vapor project will build to? Since this isn't an app, the usual convenient configuration UI isn't available, so I presume this has to be done in a configuration file, or via the terminal. Just don't know how, is all...
I think what you mean is the version of swift-tools that gets invoked rather than swift. Change the first line of Package.swift in your project's folder to use the current version needed by the latest vapor version 4:
// swift-tools-version:5.5
Then use Xcode to try to build. If this doesn't work, try Using the Clean Build Folder menu option off the Product menu and try again.
Failing this, use terminal to remove the .build folder in your project's folder and do a swift package update followed by a swift build. I know of at least one swift compiler bug that means a compile that fails with swift build will succeed with swift build -c release so it could be worth trying both.
If all this fails, include more detail in your question to show the exact cause of 'will no longer build'.

No such module 'IQKeyboardManagerSwift'

New to Xcode (dabble a little). I paid a developer to build an iOS app for me. He sent me the source code and I have opened it in Xcode 13.2.1. I needed to edit the info.plist to include a description why location was required (got this done). When I try to run the build, I get the error 'no such module 'IQKeyboardManagerSwift'. In AppDelegate, I see 'import IQKeyboardManagerSwift --- No such module 'IQKeyboardManagerSwift'. I went to the Podfile directory and ran 'pod install'. Output says:
Analyzing dependencies
Downloading dependencies
Generating Pods project
Integrating client project
Pod installation complete! There are 7 dependencies from the Podfile and 8 total pods installed.
I'm still getting the error when trying to run the build. I tried cleaning the build folder and running the build again but still, same error. What am I missing?
I'm not sure why this happens, but one way to solve your issue is to go into your build settings and define the Framework Search Paths to a folder that contains the frameworks in question. If the frameworks are placed in your project directory, simply set the framework search path to $(SRCROOT) and set it to recursive.
Is the codebase completely in Swift?, else you will have to include import in the Bridging header file
If in swift you can try:
Select project name -> Select Build Settings(tab) and search Framework Search Paths double click and set the desired path($(SRCROOT)) to recursive
The missing module handles a number of keyboard behaviors so you don't have to reinvent that wheel. Check out the developer's website and get it for yourself. I've been using it for years. The developer has helped me more than once get past some obstacles.

Embedding Frameworks from SwiftPM into Cocoa Application (XCODE)

I'm using a project called "HAP" (https://github.com/Bouke/HAP) that is built using SwiftPM.
The included example "HAP-server", works great from Command Line and I have used the params "swift package generate-xcodeproj" to create an XCODE project.
I can also load the HAP in XCODE and build/debug the target "HAP-server".
I'd like to use this framework in my Cocoa Application and have added the HAP.xcodeproj into my project. I added the produced files as "Embedded Binaries" and "Linked Frameworks and Libraries".
That allows me to "import HAP" into my view controller class.
However, when I try to build my Cocoa target, I get the following message -- seems related to the dependency "Kitura-net" from the HAP project...
"Missing required modules: 'CCurl', 'CHTTPParser'"
What is the best way to use frameworks from a SwiftPM in my project?
I assume it has something to do with Search headers... anyone have an idea?
UPDATE #1:
I have tried adding to my Cocoa Project that is importing the library that is using Kitura by "Link Binary With Libraries", libcurl.4.dylib from usr/lib/ but it still gives me the same error.
UPDATE #2:
UPDATE #3
I ended up figuring it out by using this method: Importing CommonCrypto in a Swift framework
I needed to create a modulemap for each of the items it was throwing a fit about, in my case: CCURL & CHTTPParser. Once I did that, I could compile.
You can see an example of embedding Kitura in an Xcode project in this repo https://github.com/IBM-Swift/Kitura-HelloWorld-iOS. Makefile in this repo runs build scripts to fix an Xcode Project to run Kitura on iOS. You need to compile curl for iOS - see instructions in the README.
From what I remember, you need to add the directory with curl headers to Header Search Paths, the directory with libcurl.a to Library Search Paths, and also add -lz flag to Other Linker Flags.

swift package manager outputs not shown in Xcode

I have started a project with swift package manager:
swift package init --type executable
then i made it an xcode project
swift package generate-xcodeproj
Now i am trying to build it from Xcode, it says build-succeeded but i can't see any result at output:
For example, should not this code puts something to output:
This is a bug: generate-xcodeproj generates two conflicting schemes.
Go to "Manage Schemes" and delete one of the duplicates.
Then "Edit" the remaining one: in its "Run" tab, "Executable" may be set to "None", in this case you have to change it to your executable target (it will be listed in the popup).
Build again and you're good to go.