Swift package manager for project migrated from Swift 2 - ios

I have a Swift 2 project, now converted to Swift 3. I was using HTMLKit pod, but the problem is that HTMLKit is no more on CocoaPods, but on swift package manager.
How can I add Swift Package Manager support to my existing project?
People say add dependency to package.swift but I have no such file in my project. How can I create this file?

Repositories under Swift Package Manager have certain layout. At the root of the repo you have Package.swift that provides a manifest for the repository, its name, type, dependencies, etc. You add it manually, just like any other source file.
However, before you do that please go through the documentation on SPM. It could help you to understand some specifics (like where do the dependencies end up in your repo once you check them out, how to arrange your own code so that SPM will handle it correctly, which folders to create, etc).

Related

Generate Package.swift from Xcode

I've added some dependencies to my project using Xcode and I need to generate a Package.swift file for Travis-CI.
Is there a solution other than manually add all my dependencies to this file?
You are looking at the problem the wrong way.
Package.swift should be the basis of your framework, not the Xcode project. You can generate an Xcode project from a Package file, but you cannot generate a package file from an Xcode project.
The main reason behind this is that Xcode projects support many things that Swift Package Manager (and hence Package.swift files) do not support - such as mixed language targets or signing & capabilities.
So if your project setup is fully supported by SPM, you should simply throw away the Xcode project and rely on the Package.swift file completely, otherwise you'll need to keep using Xcode projects and shouldn't use SPM until it supports your requirements.

Mixing CocoaPods with Swift package

I have an iOS project, that is divided into various frameworks using Swift packages. Now I need to create another swift package, which would include:
Some dependencies from other swift packages
Other dependencies from CocoaPods (GoogleMLKit/ObjectDetectionCustom to be exact)
How can I do this? Is there some way to import cocoa pod into the swift package?
For now, I implemented all this new functionality inside the main iOS app project, but I really don't like this solution. I would love to have this in a separate package.
You could perhaps include the GoogleMLKit/ObjectDetectionCustom package (if there is one) inside the package you are importing into your main project
For example I use Reachability package inside my own package which I then include in my main project

podspec with dependency to another ios framework project

I had developed a swift farmework to share with other developers(Lets name it B). This framework is using another ios framework project that I created recently with objective-c (lets name it A).
Now, I want to share framework B with cocoa pod. I wondering how should I link these two project in the podspec file. Does I need to share both of them with pod? or is there any other solution that just share project B?
I was having same question when I was trying to create a swift framework that is using obj-c framework, but didn't find an excellent solution.
So I've made a dependency in podspec file and now my swift framework can be installed by a podfile and it works fine. When I call pod install/update it installs/updates my framework and a dependency framework
The dependency can be created by one line
spec.dependency 'SomeOtherPod'
Check this link https://guides.cocoapods.org/syntax/podspec.html
Also you can check my podspec
The only thing that I don't like is that I have one warning in my project now:
Multiple build commands for output file
..../Build/Products/Debug-iphonesimulator/NMSSH/NMSSH.framework/Headers/NMSSH.h
Still trying to find how to solve it
Hope this will help you
You have 2 options:
1. Distribute both pods and use spec.dependency in your podspec file (see Woof's answer)
2. Add A source files directly to B's source files

Add a framework to an existing project without using cocoapods

I've got an existing project where i want to add the framework called CoreActionSheetPicker from
https://github.com/skywinder/ActionSheetPicker-3.0
The problem is i cant seem to add the framework to my project? when i pull the framework over to my existing project none of the files below is added and when i try to import it says it does not exist
import CoreActionSheetPicker
I want to do this without cocoaPods. What is the steps in order to do such? i'm using swift. Do i first need to create a WorkSpace?
I've just cloned it, and it appears the project file is invalid. You can see this by trying to open it. You should raise the issue with the owner on GitHub, which is how you're supposed to ask questions about projects there. Then you will get feedback directly from the creator or at least someone else who knows about that project.
As for adding a project,
Download the source
Drag the .xcodeproj into your project within Xcode
Add the framework in Build Phases / link binary with libraries
Add it as a Build Phase / target dependency.
Note that at the moment, you should always builds 3rd part libraries with your swift project, and not just include the binary. See here about binary compatibility of frameworks:
https://developer.apple.com/swift/blog/?id=2

How to reuse Swift code in other projects?

I wrote a class in Swift. I want to use this code in two separate iOS app projects that I wrote. Both the shared code and the apps are written in Swift. What is the best way of doing that?
I tried to create both a framework and a library in Swift and then add it as a sub-project to my app. In both cases I could not make the app see the module. I tried to add the shared module to "Target Dependencies" and "Link Binary With Libraries" of the main app's target. No luck - the app still can not see the classes of the shared module.
Using Xcode6 Beta6 at the moment.
Solution
As Konstantin Koval and Heliem pointed out, all I needed is to use public in my class and method declarations, in the shared module. Now all works, both if I use workspace and if I add the module as a subproject.
Update
I just found an excellent easy solution for reusing code between projects in Swift. It is called Carthage (https://github.com/Carthage/Carthage). This is not a plug as I am not affiliated with it in any way, I just really like it. It works in iOS 8+.
Create a new project (iOS Cocoa Touch Framework) for your reusable code
Move your classes to that Framework project
Mark your methods and classes, that should be visible to others as public
Create Workspace.
You can create a workspace on step 1. When you create new Framework project, Xcode will ask you if you want to create new workspace and add that project to workspace. This is the best approach
Add both your project and Framework to the workspace
Select you project target -> General tab. Add Framework and Libraries (add your library here)
When you want to use code from your Library in swift file, import it using import 'LibTargetName'
You can take a more programatic approach by using SWM (Swift Modules): https://github.com/jankuca/swm
It is very similar to npm (node.js package manager) or bower (client-side module manager); you declare your dependencies in a swiftmodule.json file like below. It does not have a central module registry like the two mentioned JS solutions, it only accepts git URLs.
{
"name": "ProjectName",
"dependencies": {
"Dependency": "git://github.com/…/….git"
}
}
…run swm install and have the Dependency module (compiled into a *.swiftmodule binary) ready for import in the .modules/ directory.
import Dependency
And if you prefer to skip Xcode for app development, you can also build your whole app using swm build (more info in the project's readme).
The project is still in early stages but it makes itself useful a lot for me at least. It provides the most clean (and clear) way of creating importable modules.
Here is a video which is very straightforward: http://eonil-observatory.tumblr.com/post/117205738262/a-proper-way-to-add-a-subproject-to-another-xcode
The video is for OS X instead of iOS. But you will get it and figure out the process for iOS.
Let's assume that AppA needs to reused code from SharedProject.
The following process works for me in Xcode 7 Beta:
Create SharedProject. Project type must be Framework instead of Application. You write common code in this project and mark the code as public.
Open AppA in Xcode, open the folder which contains SharedProject in Finder. Drag the .xcodeproj file of SharedProject from Finder and drop it into the root folder of AppA in Xcode Project Navigator.
AppA --> Build Phases --> Link Binary with Libraries. Add SharedProject.
import SharedProject and reuse code from SharedProject!
Edit:
Nowadays I suggest you use Carthage. It's better than the home made solution above.

Resources