Reader small image

You're reading from  An iOS Developer's Guide to SwiftUI

Product typeBook
Published inMay 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781801813624
Edition1st Edition
Languages
Right arrow
Author (1)
Michele Fadda
Michele Fadda
author image
Michele Fadda

Michele is currently working as a technical project and program manager at Eggon, an innovative startup in Padua, Italy, and as the director of FWLAB Limited, a UK App development studio. He specializes in skills such as Imperative Programming, Functional programming, Swift, Mobile Application Development. He started programming as a child with Assembly language and has worked for 20 years as a consultant using a variety of languages and technologies. He started developing for iOS with iOS on iOS v.3.0 in 2009. He has also developed many apps as a solo developer and has also participated in numerous projects.
Read more about Michele Fadda

Right arrow

Exploring the Environment – Xcode, Playgrounds, and SwiftUI

This chapter is an introduction to the tools used when working with SwiftUI, the new, exciting, efficient, and simple-to-use Apple framework for user interfaces (UIs). We’re going to cover the following main topics in this chapter:

  • Exploring Xcode and SwiftUI
  • Creating a multi-platform SwiftUI project
  • Using Swift Playgrounds to test fragments of code
  • Adding tests
  • The App submission process

By the end of this chapter, you will learn how to create a project from scratch. You will also learn how to create a project using the project templates and how to add tests and preview your SwiftUI views code.

Note

The opinions expressed in this book are solely those of the author and do not necessarily reflect the views or policies of his employers or other entities. Any reference to organizations, events, or individuals is purely fictional and intended for illustrative purposes only. Resemblance...

Technical requirements

You will require a recent Apple computer to run the examples and code in this book. In general, the more RAM and more powerful your system, the better. This book has been tested on an Intel MacBook Pro running macOS 13.6 (Ventura) with 16 GB of RAM. It will work just as fine on a more recent Apple Silicon machine.

To follow this chapter and the rest of the book, you will be required to install Xcode version 15.0 or later.

If you want to run code on a physical device with Xcode 15.0, you will be able to use the async/await pattern in concurrent programming for devices running at least iOS 13. We suggest updating your device to iOS 17 or later to follow this book.

The complete projects and code samples for the examples discussed in the book can be found under the GitHub repository: https://github.com/PacktPublishing/An-iOS-Developer-s-Guide-to-SwiftUI.

Exploring SwiftUI with Xcode

In this section, we are going to look into the Xcode UI in the context of a SwiftUI project.

Xcode is quite a large topic by itself. We will focus on SwiftUI and explain enough about how Xcode usage differs in SwiftUI from UIKit to get you started, how to create a SwiftUI project, and add targets for various Apple platforms. Also, we will explain how to add tests to a project.

A brief tour of Xcode

Xcode is an integrated development environment (IDE) for developing any code on any device that Apple produces. Xcode is therefore a massive and complex app capable of supporting many different types of projects and technologies. Not all of these types of projects are relevant to SwiftUI, so we will limit ourselves to Swift as a language and SwiftUI as a UI framework. In the past, the only supported language for app development was Objective-C and the only supported UI framework was different for each device platform, notably UIKit for iOS and AppKit...

Your first SwiftUI project

In this section, we will look into the process of creating a new project supporting SwiftUI. We will first look into the easiest and quickest way to create a SwiftUI project. Then, we’ll learn how to create a multi-platform project and at the end, there’s also a multi-platform exercise for you to practice.

Creating a SwiftUI project

To create an app, start by clicking on Create New Project… on the initial Welcome Xcode screen. Be warned that this part is likely to change graphically and in terms of the options presented, with newer versions of Xcode.

I will teach you how to create a basic project with an app template or a multi-platform project from scratch, but we can’t cover all the other options for all of the different kinds of projects Xcode can generate.

The following is the easiest and quickest way to create a basic project using an app template:

  1. Create an app for your desired platform by selecting Create...

Previews and the simulator

Now, for the sake of simplicity, we will start again by creating a simple single-view project.

You can create a new project from the Xcode menu: File | New | Project.

You will see that Xcode will automatically create a simple project and create a view struct and a preview struct inside the ContentView.swift file.

The first struct inside this file describes a simple SwiftUI view (Text) and the second struct is used to pre-render it so that you can have an idea of the finished result without the need to launch the simulator.

You can have multiple previews, even for different devices simultaneously, but that depends heavily on the memory available on your system and has been somewhat error prone in the past.

You can give each preview a name, by using the previewDisplayName modifier on the content view in the preview, for example, name the currently rendered device iPod.

You can also change the desired rendered device in code using the previewDevice...

Playgrounds

Swift Playgrounds is an excellent way to test Swift code without creating an app or a complete project.

Unfortunately, Swift Playgrounds was developed for UIKit and does not completely support SwiftUI.

In Xcode, there is an option to create a Playground containing a view, but that will create a view with ViewController using UIKit, not SwiftUI, and that’s not ideal for our purposes.

You can, however, start with an empty Playground and create your view in code with SwiftUI and then set it as the current view by using PlaygroundPage.current.setLiveView(ContentView()).

Your code will look like this:

import SwiftUI
import PlaygroundSupport
struct ContentView: View {
    var body: some View {
        Text("I love SwiftUI")
    }
}
PlaygroundPage.current.setLiveView(ContentView())

However, the support for SwiftUI in Swift Playgrounds is far from ideal, and not updated...

The app submission process

When submitting iOS and macOS applications to the Apple App Store, there are a number of steps to follow, such as getting certificates generated and creating mobile provisioning profiles.

You will need to use both the Apple Developer website and Xcode.

Certificates identify you as the developer of your apps and ensure the security and integrity of your apps. Different certificates will be required for development and distribution.

Certificates

There are two main types of certificates that are relevant to app development:

  • Development certificate: This is used to authorize the installation of applications on physical devices while developing the application.
  • Distribution certificate: This is required for application distribution in the App Store or ad hoc (on selected specific devices, the latter usually for testing purposes).

    Additional certificates can be needed for specific purposes, for example, enabling push messages.

The...

Summary

We have just covered some initial ground.

You should now be able to create your projects and add several target platforms to them.

You have been given a brief introduction about using Xcode to create and modify a simple text view, and you should be able to preview, modify, and run your first very simple app with static text and a color background.

You have seen the basics of project creation; you know how to modify a project to add tests or support for another platform on your own.

You know the limits and advantages of Swift Playgrounds rather than previews and can solve the most common Xcode problems you face as a SwiftUI beginner.

In the next chapter, we will explore SwiftUI views and their modifiers in greater detail, and we will create more complex screens.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
An iOS Developer's Guide to SwiftUI
Published in: May 2024Publisher: PacktISBN-13: 9781801813624
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Michele Fadda

Michele is currently working as a technical project and program manager at Eggon, an innovative startup in Padua, Italy, and as the director of FWLAB Limited, a UK App development studio. He specializes in skills such as Imperative Programming, Functional programming, Swift, Mobile Application Development. He started programming as a child with Assembly language and has worked for 20 years as a consultant using a variety of languages and technologies. He started developing for iOS with iOS on iOS v.3.0 in 2009. He has also developed many apps as a solo developer and has also participated in numerous projects.
Read more about Michele Fadda