Dependency Management for iOS projects with the Swift package manager

iOS is not yet supported by the Swift Package Manager but there is a way to make it work. This article describes how to set up a framework project and how to use it as a dependency of an iOS app project with the Swift Package Manager.

WARNING: This is experimental / for exploration only. Do not use this for real projects unless you’re willing to deal with Xcode build errors and projects settings. Even if it works well now, it might break with new Xcode releases.


Creating a HelloKit package

Creating the HelloApp iOS project

Updating the generated Xcode projects - Common Tasks

You can use the Rakefiles to update the generated Xcode projects. Here is example code for some common tasks:

Setting the iOS Deployment target:

project.build_configurations.each { |config|
  config.build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = "10.0"

Adding resources to a target (this might be possible in the future with the Swift package manager: SwiftPM: Need a way to include resources with targets):

target = {|t|"SomeTarget"}.first
group = project.main_group["Sources/SomeFolder"]
file = group.new_file(File.expand_path("Sources/SomeFolder/cat.png"))

Setting an environment variable for a shared scheme:

scheme ="SomeProject.xcodeproj/xcshareddata/xcschemes/SomeSharedScheme.xcscheme")
action = scheme.launch_action
env = action.environment_variables
env["FOO"] = "BAR"
scheme.launch_action.environment_variables = env
scheme.launch_action = action!

Adding a system framework as linked framework (for Objective C projects) - from this gist (has some other examples of xcodeproj usage):

def add_system_frameworks(target, names)

    build_phase = target.frameworks_build_phase
    framework_group = target.project.frameworks_group

    names.each do |name|
        path = "System/Library/Frameworks/#{name}.framework"
        file_ref = framework_group.new_reference(path) = "#{name}.framework"
        file_ref.source_tree = 'SDKROOT'
        build_file = build_phase.add_file_reference(file_ref)


Further work / ideas

More information

Ralf Ebert Ralf Ebert is an independent software developer and trainer for Mac OS X and iOS. He makes the Page Layers and Straight ahead apps and conducts iOS trainings in Germany since 2009.