WWDCKit - Creating and Using a Swift Package

Related tags

Utility WWDCKit
Overview

WWDCKit - Creating and Using a Swift Package

1. Create the Package in Xcode

  1. Navigate to File >> New >> Package.
  2. Give the Package a name e.g "WWDCKit".

Package.swift

let package = Package(
    name: "WWDCKit",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "WWDCKit",
            targets: ["WWDCKit"]),
    ],
    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 this package depends on.
        .target(
            name: "WWDCKit",
            dependencies: []),
        .testTarget(
            name: "WWDCKitTests",
            dependencies: ["WWDCKit"]),
    ]
)

2. Update the Package.swift as needed

  1. Let's restrict this Package to iOS 15.
let package = Package(
    name: "WWDCKit",
    platforms: [
        .iOS(.v15)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "WWDCKit",
            targets: ["WWDCKit"]),
    ],
    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 this package depends on.
        .target(
            name: "WWDCKit",
            dependencies: []),
        .testTarget(
            name: "WWDCKitTests",
            dependencies: ["WWDCKit"]),
    ]
)

3. Create a new SwiftUI View

For the purposes of this demo we will create a SwiftUI view called WWDCView. This view will be available via this Swift Package.

  1. Create a new SwiftUI view file called WWDCView inside the "Sources" folder.
import SwiftUI

struct WWDCView: View {
    var body: some View {
        Text("WWDC 2022")
    }
}

struct WWDCView_Previews: PreviewProvider {
    static var previews: some View {
        WWDCView()
    }
}

4. Updated WWDCView

Pay close attention to the public access modifiers. Any part of your Package you need exposed to clients will be required to have the public access modifier applied to the type including the initializer here.

some View { content .multilineTextAlignment(.center) .padding(.bottom, 10) } } extension View { func textStyle() -> some View { modifier(TextStyle()) } }">
import SwiftUI

public struct WWDCView: View {
    public init() {}

    public var body: some View {
        ScrollView {
            HStack {
                Text("WWDC22")
                    .font(.headline)
                Spacer()
                Text("Overview")
                Text("Swift Student Challenge")
            }
            .padding()
            .background(Color.black)
            Image(systemName: "chevron.left.forwardslash.chevron.right")
                .font(.largeTitle)
            VStack(alignment: .center) {
                Text("Call to code.")
                    .font(.largeTitle)
                    .padding(.bottom, 10)
                Text("Join developers worldwide from June 6 to 10 for an inspiring week of technology and community. Get a first look at Apple’s latest platforms and technologies in sessions, explore the newest tools and tips, and connect with Apple experts in labs and digital lounges. All online and at no cost.")
                    .textStyle()
                Text("In addition to the online conference, Apple will host a special day for developers and students at Apple Park on June 6 to watch the keynote and State of the Union videos together, along with the online community. Space is limited and details on how to apply to attend will be provided soon.")
                    .textStyle()
                Text("Wherever you watch from, get ready for a fantastic WWDC.")
                    .textStyle()
                Text("Talented students can showcase their creativity for the opportunity to receive an award in the [Swift Student Challenge](https://developer.apple.com/wwdc22/swift-student-challenge/).")
                    .textStyle()
            }.padding()
        }
        .foregroundColor(.white)
        .background(.primary)
    }
}

struct WWDCView_Previews: PreviewProvider {
    static var previews: some View {
        WWDCView()
    }
}


struct TextStyle: ViewModifier {
    func body(content: Content) -> some View {
        content
            .multilineTextAlignment(.center)
            .padding(.bottom, 10)
    }
}

extension View {
    func textStyle() -> some View {
        modifier(TextStyle())
    }
}

5. Publishing the Swift Package

For now since we're testing out this package we won't add a tag to the commit which allows for versioning.


6. Adding Resources e.g images to your Package

  1. Add a "Resources" folder within your "PackageName" folder. e.g WWDCKit/Resources.
  2. Place any image assets inside the newly created "Resources" folder.
  3. Update the Package.swift file.
.target(
    name: "WWDCKit",
    dependencies: [],
    resources: [.process("Resources")]),
  1. Use the extension below to access the bundles resource.
extension Image {
    init(packageResource name: String, ofType type: String) {
        guard let path = Bundle.module.path(forResource: name, ofType: type),
              let image = UIImage(contentsOfFile: path) else {
            self.init(name)
            return
        }
        self.init(uiImage: image)
    }
}
  1. Usage:
Image(packageResource: "wwdc22", ofType: "png")
    .resizable()
    .aspectRatio(contentMode: .fit)

Resource


7. Using this Swift Package

  1. Create a demo app iOS app called "WWDCPackageDemo" (ensure SwiftUI is the selected Interface).
  2. Navigate to File >> Add Packages...
  3. Enter the following url https://github.com/alexpaul/WWDCKit in the Search field dialog.
  4. For "Dependency Rule" select "Branch" and main should be the default.
  5. Select "Add Package".
  6. The "WWDCKit" Package is now able to be used.

8. Using the WWDCKit Package

  1. Navigate to ContentView.swift.
  2. Add import WWDCKit.
  3. Modify the code so it looks similar to the following:
import SwiftUI
import WWDCKit

struct ContentView: View {
    var body: some View {
        WWDCView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Simulator

Simulator Screen Shot - iPhone 12 Pro - 2022-04-06 at 05 55 11

SwiftUI Preview

Screen Shot 2022-04-06 at 5 44 31 AM


Resources

You might also like...
Mechanical editing support for Package.swift manifests

Mechanical editing support for Package.swift manifests. Implements Swift Evolution proposal SE-301

Swift Package for Decoding RSS Feeds.

SyndiKit Swift Package built on top of XMLCoder for Decoding RSS Feeds. Check out the DocC-Built Site! Table of Contents Introduction Features Install

A simple Swift package for counting the Syllables in a sentence.

A simple Swift package for counting the Syllables in a sentence.

Azure Maps iOS SDK binary distribution for Swift Package Manager
Azure Maps iOS SDK binary distribution for Swift Package Manager

Azure Maps Control for iOS Installation In your Xcode iOS Project settings, under Project setting’s Package Dependencies, click on + button to add pac

Numerals is a package containing additional numeric types for the Swift programming language.

swift-numerals Numerals is a package containing additional numeric types for the Swift programming language. Contents The package currently provides t

Swift package adding measurable types.

swift-measures Measures is a package containing measurable types for the Swift programming language. Contents The package currently provides the follo

Swift package for accessing SF Symbols in a type safe manner.

Swift Package Information Code Coverage Swift package for accessing SF Symbols in a type safe manner. Features 💫 Contains all SF Symbols - 1.0, 2.0,

This is a Swift package with support for macOS that allows to start Java Jar's with the default or a custom JVM.

Jar.swift jar runner for macos Jar.swift is created and maintaned with ❥ by Sascha Muellner. What? This is a Swift package with support for macOS that

Swift package containing collection protocols.
Swift package containing collection protocols.

swift-collection-protocols A package containing collection protocols, for the Swift programming language. Overview No overview available. Availability

Owner
Alex Paul
Software Engineer at New York Times. Community iOS Advisor at pursuit.org.
Alex Paul
Pavel Surový 0 Jan 1, 2022
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Zamzam Inc. 261 Dec 15, 2022
Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Mathias Quintero 9 Sep 25, 2022
Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Swift Markdown is a Swift package for parsing, building, editing, and analyzing Markdown documents.

Apple 2k Dec 28, 2022
A command-line tool and Swift Package for generating class diagrams powered by PlantUML

SwiftPlantUML Generate UML class diagrams from swift code with this Command Line Interface (CLI) and Swift Package. Use one or more Swift files as inp

null 374 Jan 3, 2023
Swift package adding fraction and percentage types.

swift-rationals Rationals is a package containing Fraction and Percentage types for the Swift programming language. Contents The package currently pro

Alexandre H. Saad 0 Jul 28, 2022
Units is a Swift package to manipulate, compare, and convert between physical quantities.

Units ?? Units is a Swift package to manipulate, compare, and convert between physical quantities. This package models measurements, which are a numer

Jay Herron 3 Jun 22, 2022
Approximate is a Swift package that provides implementations of floating point comparisons for the Swift ecosystem

Approximate Approximate floating point equality comparisons for the Swift Programming Language. Introduction Approximate is a Swift package that provi

Christopher Blanchard 1 Jun 1, 2022
A simple swift package that provides a Swift Concurrency equivalent to `@Published`.

AsyncValue This is a simple package that provides a convenience property wrapper around AsyncStream that behaves almost identically to @Published. Ins

Brent Mifsud 33 Oct 3, 2022
Swift implementation of the package url spec

PackageURL Swift implementation of the package url specification. Requirements Swift 5.3+ Usage import PackageURL let purl: PackageURL = "pkg:swift/a

Mattt 21 Jun 14, 2022