Multi-Target Swift Framework erstellen – Distribution

Archiving our Frameworks

The defactoway to distribute a framework is by archiving it. Select the generic device destination on your project build configuration. You can switch between destinations by pressing ^ + ⌥ + ⌘ + [ and ^ + ⌥ + ⌘ + ].
Before we can archive our frameworks, we need to set the Skip Install setting to NO.

Set Skip Install to NO for all framework targets
To archive your framework, choose Product -> Archive in the menu. The archive will appear on the Organizer. To export it, select Export -> Save Built Products.

Export Framework
Your framework will be exported to a folder with the framework name and export date.

Exported Framework
At this point we can zip our framework and distribute it.

CocoaPods PodSpec

To distribute our frameworks via CocoaPods, we need to create a podspec file. In the root of your repository type:

$ pod spec create MyFramework

Specification created at MyFramework.podspec

Now, let’s add the podspec file to Xcode, for easy editing, but without adding it to any of the targets. Right-click on the project navigator and select Add Files to „MyFramework“….

Then proceed to edit the MyFramework.podspec file as follows:

Pod::Spec.new do |s|
  s.name         = "MyFramework"
  s.version      = "0.0.1"
  s.summary      = "MyFramework with a Car to track miles."
  s.homepage     = "https://github.com/eneko/MyFramework.swift"
  s.license      = "MIT"
  s.author       = { "Eneko Alonso" => "[email protected]" }

  s.ios.deployment_target = "8.0"
  s.osx.deployment_target = "10.9"
  s.tvos.deployment_target = "9.0"

  s.source       = { :git => "https://github.com/eneko/MyFramework.swift.git", :tag => s.version }
  s.source_files = "Sources/*.swift"
end

To verify the pod works as expected, use the command (add –private if your repository is not public):

$ pod lib lint
-> MyFramework (0.0.1)

MyFramework passed validation.

The minimum platform versions for a Swift application/framework are iOS 8.0, OS X 10.9 and tvOS 9.0.
For more information about making Pods see Making a CocoaPod, Using Pod Lib Createand Private Pods.

Swift Package Manager

To distribute your library via Swift Package Manager, all that is required is to create an empty Package.swift configuration file on the root of your repository.

This is because Swift Package Manager will include by default only those files in the Sources folder, which in our case, contains the source code we want to distribute.

For more information about the Swift Package Manager see Swift Page Manager


CocoaPod dependencies

As mentioned before, Framework projects can import dependencies via CocoaPods. Since we have created only one project for all three frameworks, adding CocoaPods will be super easy.
In the terminal, navigate to the folder containing your Xcode project and type the following command:

$ pod init

This will create a Podfile file with some content for your multiple targets. Alternatively, you can also create the Podfile manually with any text editor.
Add any pod dependencies. In most cases, the dependencies will be the same for all three frameworks, so we can create a ruby function to contain all the pods as follows:

use_frameworks!

def pods()
    # Your favorite pods here
    # pod 'SwiftyJSON'
end

target 'MyFrameworkiOS' do
    pods()
end

target 'MyFrameworkiOSTests' do
    pods()
end

target 'MyFrameworkOSX' do
    pods()
end

target 'MyFrameworkOSXTests' do
    pods()
end

target 'MyFrameworkTVOS' do
    pods()
end

target 'MyFrameworkTVOSTests' do
    pods()
end

Once you have listed your dependencies, run this command to install them (it is a good habit to close your project on Xcode, since moving forward we will use the workspace instead):

$ pod install

Open the new workspace created by CocoaPods:

$ open MyFrameworks.xcworkspace

At this point, your workspace should contain two projects our framework project with our three targets and a second project from CocoaPods.