- Introduction
- Use XcodeTool
- Create an empty project
- Add targets
- Add "Sources" folder
- Rename "info.plist"
- Move all plist on "Sources" folder
- Update "Build Settings"
- Update packaging
- One header file for all targets
- Add class for all platforms
- Configure targets for Carthage
- Configure project for SwiftPM
The purpose of this tutorial is to create a Swift Cross-Platform framework from the same sources for macOS, iOS, watchOS and tvOS platforms and use them in a project via Carthage. It will be also ready for SwiftPM.
You can follow this tutorial or just use XcodeTool and look at the section: Create a new component cross-platform
- Open Xcode
- Select
Create a new Xcode project
- Select
Cross platform
- Click on
Empty
- Click on
Next
- Enter the project name (for this tutorial we use
Template
) - Click on
Next
We must add a target for each platform
- On the section PROJECT click on (+)
- Select
iOS
- Select
Cocoa Touch Framework
- Click
Next
- Enter the project name
Template iOS
(replaceTemplate
by your framework name). Don't forgetiOS
it's important! - Click on
Next
- On the section PROJECT click on (+)
- Select
watchOS
- Select
Watch Framework
- Click
Next
- Enter the project name
Template watchOS
(replaceTemplate
by your framework name). Don't forgetwatchOS
it's important! - Click on
Next
- On the section PROJECT click on (+)
- Select
tvOS
- Select
TV Framework
- Click
Next
- Enter the project name
Template tvOS
(replaceTemplate
by your framework name). Don't forgettvOS
it's important! - Click on
Next
- On the section PROJECT click on (+)
- Select
macOS
- Select
Cocoa Framework
- Click
Next
- Enter the project name
Template macOS
(replaceTemplate
by your framework name). Don't forgetmacOS
it's important! - Click on
Next
- Rigth click on
Template
and selectAdd Files to Template
- Click on
New Folder
- Enter
Sources
and click onCreate
andAdd
- Open folder
Template iOS
- Rename
info.plist
toinfo-iOS.plist
- Open folder
Template watchOS
- Rename
info.plist
toinfo-watchOS.plist
- Open folder
Template tvOS
- Rename
info.plist
toinfo-tvOS.plist
- Open folder
Template macOS
- Rename
info.plist
toinfo-macOS.plist
Select all on the folder
And move them to the "Sources" folder
On the project remove plists
Rigth click on the Sources
folder and click on Add Files to "Template"...
Select all plist and click on Add
. Be careful don't check a target.
- On section
PROJECT
selectTemplate
- Selection section
Build Settings
- In search enter
product name
- For the property
Product Name
enter$(PROJECT_NAME)
- Select all targets
- Click on
Product Name
- Click on key
Delete
. All targets will have the product nameTemplate
.
- Select
Template iOS
target - Set property
info.plist File
toSources/info-iOS.plist
- Set property
Product Bundle Identifier
tocom.<your company>.$(PROJECT_NAME)
- Select
Template watchOS
target - Set property
info.plist File
toSources/info-watchOS.plist
- Set property
Product Bundle Identifier
tocom.<your company>.$(PROJECT_NAME)
- Select
Template tvOS
target - Set property
info.plist File
toSources/info-tvOS.plist
- Set property
Product Bundle Identifier
tocom.<your company>.$(PROJECT_NAME)
- Select
Template macOS
target - Set property
info.plist File
toSources/info-macOS.plist
- Set property
Product Bundle Identifier
tocom.<your company>.$(PROJECT_NAME)
- Select
Template iOS.h
- Change
#import <UIKit/UIKit.h>
by#import <Foundation/Foundation.h>
- Remove
iOS
- Open Finder and Rename
Template iOS.h
toTemplate.h
- Move
Template.h
to folderSources
- Remove folders
Template iOS
,Template macOS
,Template tvOS
andTemplate watchOS
- Remove folders
Template iOS
,Template macOS
,Template tvOS
andTemplate watchOS
- Add
Template.h
inSources
folder - Select
Template.h
- In
Target Membership
checkTemplate iOS
,Template watchOS
,Template tvOS
andTemplate macOS
- For each target select
Public
- Click on
Sources
- Click on
New File...
- Select
macOS
- Select
Swift File
- Click
Next
button
- Enter the name of your class
- Select all targets
- Click
Create
button
Your first class cross platform:
- Click on
Template iOS
optionManage Schemes...
- For all targets check
Shared
- Select the project
Template
- Rigth click and select
New File...
- Select
macOS
- Select
Swift File
- Click
Next
button
- Enter
Package
- Uncheck all targets
- Click on
Create
button - On the
Package.swift
enter:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Template",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Template",
targets: ["Template"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Template",
dependencies: [])
]
)