- Open Xcode. File > New > Package
- Select Library > Next
- Choose the location that will be your root folder, enter the Package name > Create
- Rename the module created for you to something different than the Package and Product name.
- Under [Package name] > Sources > [Package Name] > [Package Name].swift, replace with this SwiftUI view for later use:
import SwiftUI
public struct TestLabel: View {
public init() {}
public var body: some View {
Text("Testing")
}
}
- File > New > Project
- Choose platform (e.g. iOS) > Select App > Next
- Enter product name > Change any project settings if needed > Next
- Open the Package root folder from Step 1 > Create
- Switch to the Package window and drag the Package into the Project window, positioning it directly under the Project (should be above the folder)
- Close both Package and Project window
- Create a new file under
[Product Name]/Package.swift
and paste below code. This prevents Xcode from re-importing the Source/Test code.
// swift-tools-version:5.2
// Leave blank. This is only here so that Xcode doesn't display it.
import PackageDescription
let package = Package(
name: "client",
products: [],
targets: []
)
- Open [Product Name]/[Product Name].xcodeproj
- Rename the folder [Product Name] to the platform, e.g.
iOS
- To test that the app can import the module:
- Under
[Package Name]/Package.swift
, add platforms: [.iOS(.v17)],
just under the name
attribute
- Open the Project Target > General > Under Framework, Libraries, and Embedded Content > Click Add > Search for [Package Name] and select the library with the test label
- Open
ContentView.swift
and add import [Package Name]
and add in TestLabel()
into the view
- Build and run app - you should see the test label in the view.