How to use pod package to create a compiled framework - frameworks

I am trying to use the cocoa pods package extension to create a compiled framework.
The example project with source code and pod spec can be found here:
https://github.com/avnerbarr/TestFramework.git
The resulting output can be found here:
https://github.com/avnerbarr/TestFrameworkCompiled
The main idea is that I want to create a project using cocoa pods.
When I will need to distribute the close source - I need all of the dependencies to be contained in the resulting framework.
I don't want my end users to be forced to use cocoa pods.
But it would be great if I could leverage the cocoa pods abilities to compile and package everything together.
How should I do this?

You can use the vendored_frameworks attribute for this. It allows you to specify a path to a compiled framework that will be linked with the users application.
spec.ios.vendored_frameworks = 'Frameworks/MyFramework.framework'

Related

Can the Swift compiler create a single executable for my application?

I'm currently trying to create an executable for my Vapor application, but I just can't run it on my server.
My expectation was that it would be able to create a single application with all my code and the dependencies. My current references are .NET and Go, where you can create a single executable from the code and deploy this standalone file - which makes it amazingly simple to deploy.
I tried compiling and executing it on Docker, but no such luck - it failed with some libSwift something missing.
So, can Swift do something like this?
Looking forward to your answers
You can build a binary that statically links the standard library or build a completely self contained binary using the static linking flags introduced in Swift 5.3.1 - -static-stdlib and -static-executable. See this forum post for more details.
Note that swift still has some dependencies on ICU and libc so aren't completely portable yet

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

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

Sharing code across Swift OpenWhisk actions

I'm trying out some OpenWhisk actions in Swift. For better or worse, almost all of the OpenWhisk documentation is about javascript. When writing actions in javascript, it looks like you can package up code as an npm module and require/import it into any action. With Swift, there's no indication that there's any way to share code. I found one sample project (https://github.com/SwiftOnTheServer/DrinkChooser by the inestimable #rob-allen) which uses the clever trick of pre-processing the source code files before building them:
cat actions/_common.swift actions/myaction.swift > build/myaction.swift
Is there an official way to share code across actions?
Many thanks.
Building Swift binaries locally and creating actions from those binaries will allow you to do this. Swift's package manager will generate multiple executables if you have the correct directory layout.
This example project which uses The Serverless Framework to build and deploy multiple binaries as OpenWhisk actions.
Creating actions from Swift sources files does not support providing multiple sources files for code sharing.

pods in a framework, import that framework for main project in swift

The idea is to have an api framework (new project -> cocoa touch framework) for example "SuperApi". SuperApi use pods like Alamofire, ObjectMapper and whatever I need.
Next I want to integrate that SuperApi into my app like a normal framework.
I tried different ways. I tried to add it at "Embedded Binaries" and/or tried to add superApi at "Linked Frameworks and Libraries". Without success. I always linked the whole framework-folder.
import SuperApi threw always an error: could not find framework
Then I read something about workspaces where I can have a lot of projects parallel. This seems really overpowered for an api.
Create a framework without pods (xcodeproj) is no problem. There are a lot of descriptions in web. But what is a (good) solution for my problem?
I don't want to write my own http-networking + JSON parser. Thanks for input.
Why don't you add your SuperApi as a pod of your main project? You can have it either as a local pod, directly linking the last commit or using your own private repo
There's a very nice tutorial by Ray Wenderlich: https://www.raywenderlich.com/99386/create-cocoapod-swift
Thanks Eli. I found this video, it's more up to date
https://www.youtube.com/watch?v=gNMNeqXKnzw&t=964s (thanks to you Mr. Levi Bostian - you made my day)
Unfortunately this video is a litte bit outdated too.
You run into a Swift 2/3 issue. So you have to create a .swift-version in your git. For example Alamofire did it like this: https://github.com/Alamofire/Alamofire/blob/master/.swift-version
You can do it like this in your pod-folder: echo 3.0 > .swift-version
I ran into a second issue, because my pod wasn't up to date. So update your pod sudo gem install cocoapods --pre
Last but not least I had a warning in my podspec, so I have to use pod spec lint --allow warnings
Better way is to fix the warning ;)

Using installed pod in swift project

I'm trying to create a swift program that uses sockets. In order to do that, I'm trying to use the SwiftSocket library by installing it using CocoaPods.
My Podfile is basic:
target 'socket' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for socket
pod 'SwiftSocket'
end
According to the installation guide of SwiftSocket, after installing the pod I should be able to use the TCPClient right away.
Still, I fail to use the class in my main.swift file as it doesn't find the class.
I've searched the web to see what I'm missing here, but all I found are guides on how to bridge Pods written in ObjectiveC but not on pods written in Swift.
Any help?
Thanks
EDIT: With jamshes reginahit suggestion, I've added SwiftSocket.framework to the Linked Frameworks and Libraries, in addition to the Pods_socket framework that was already present.
Now the build is successful, but I gut a runtime error of Thread1: signal SIGABRT with the payload:
yld: Library not loaded: #rpath/SwiftSocket.framework/Versions/A/SwiftSocket
Referenced from: /Users/jonathan/Library/Developer/Xcode/DerivedData/socket-buglawjxihebcabvcihcbdrtkcxt/Build/Products/Debug/socket
Reason: image not found
(lldb)
EDIT2: Something was funky with my Xcode. I've reinstalled it and now it seems to work fine. Thanks to everybody for the help. :)
I would like to comment, but I don't have enough reputation.
Anyway did you write something like:
import SwiftSocket
in the class where you need it? Also, did you open the project with the xcworkspace extension?
If nothing works try to clean and rebuild the project
Based on my checking of the SwiftSocket Library, it seems that what you did should be fine (it should be pod 'SwiftSocket' referring to "Installation" section), I assume that you missed to add :
import SwiftSocket
in your main.swift class.
And yes, they are not mentioning that in "Code examples" section because they -probably- assume that importing it in your .swift file should be obvious.
Once CocoaPods is finished installing, you need to start using the .xcworkspace instead of your .xcproject file. So close your project, open the workspace (same directory), and import SwiftSocket.
Your Podfile seems correct, after that, you need to launch a terminal from your project directory:
cd ~/Desktop/MyProject/
Then run: pod install command.
This will create a .xcworkspace file and a Pods directory.
Now you have to open the .xcworkspace file with Xcode.
Donc forget to import yout pod like this: import SwiftSocket
Also take a look at the CocoaPods documentation: https://guides.cocoapods.org/using/using-cocoapods.html