This book is about building cross-platform software solution using Swift. The book will take the reader on a journey of building an app for iOS and extending the app to a different platform, such as the web and tvOS. The app will start out simple, but get more and more complex as it progresses. Toward the end, we will have a product that will work on iOS, tvOS, and in the browser and have a server component, all written entirely in Swift.
Through this journey, we will learn how Swift has progressed from a language used just for iOS to a language that can be used on the server side. We will also learn how to build server-side packages using Swift and Vapor, which is one of the most popular Swift packages for building web servers. Using Vapor, we will build a full-stack web application that will act as an API server for our iOS and tvOS app, and will also be our web server, which will render a web view of our app. Several technologies will be covered while building the backend, including MongoDB, which is a non-relational database.
We will be using Swift 4 throughout the book and will cover new features introduced in this version of Swift. We will use Xcode 9 as our IDE to build for these different platforms, and readers will learn how to share code and development tools to make development fun and productive. We will also use Vapor 2.0 to build our server in Swift, and learn how the framework makes it easy to build rich backends for our application.
I hope that on this journey, you will learn how to write code in these different application stacks. By the end of this book, you should feel comfortable building your next product using Swift. From building a native app, to the backend, to a marketing page, or web app, you will have the knowledge to get hands-on with Swift to build your next big idea.
This book is intended for developers familiar with Swift and web development on the client side who want to build both a full-stack web application and a native mobile application using the Swift and Vapor framework. An understanding of how HTML and CSS work and knowledge of JavaScript will be helpful when building server-rendered pages with Vapor.
Chapter 1, Getting Started with Server Swift, dives into the world of server-side Swift and shows you how to build and use Swift packages, and how to build a simple HTTP server using pure Swift.
Chapter 2, Creating the Native App, explains how to build a Shopping List app in pure Swift, using Xcode and Storyboard. At the end of this chapter, you will have a fully-functioning app that persists data offline on the iPhone in a secure way.
Chapter 3, Getting Started with Vapor, delves deep into Vapor features and packages and shows how to get started with using Vapor to start building rich web applications.
Chapter 4, Configuring Providers, Fluent, and Databases, provides a solid background on ORM for Swift and shows how to set up a database for a Vapor app using Fluent and Providers.
Chapter 5, Building REST API Using Vapor, explores how to build a RESTful API using Vapor for our Shopping List app and goes into detail about how to create RESTful routes and controllers.
Chapter 6, Consuming API in App, contains details on how to refactor our iOS app to consume the RESTful API we built in Chapter 5, Building REST API using Vapor, and how to make network requests to our API when creating, reading, updating, and deleting data from our iOS app.
Chapter 7, Creating Web Views and Middleware, shows how to create HTML views in our Vapor app and demonstrates the use of Middleware to conditionally load HTML views for a browser and a render view as JSON for an API request from the iOS app.
Chapter 8, Testing and CI, contains information on how to test a Vapor app and how to set up a Continuous Integration pipeline to automatically run tests before code is merged using Travis CI.
Chapter 9, Deploying the App, contains deployment options for a Vapor app and shows how to deploy a Vapor app to Heroku and set up an automated deployment pipeline when code is merged into the Git repo.
Chapter 10, Adding Authentication, demonstrates how to add authentication to a Vapor app so that users can log in or register and own the Shopping List that they create. This chapter also demonstrates how an iOS app is updated to support token-based authentication implemented in the Vapor app.
Chapter 11, Building a tvOS App, wraps up the book by demonstrating how easy it is to build for another platform with maximum code shareability between the iOS and tvOS apps and how a small team of Swift developers can build a multiplatform full-stack application using Swift.
You should have basic knowledge of the following topics:
- Swift
- Xcode
- Storyboard and Autolayout
- HTML
- JavaScript
- CSS
- Terminal/Command Line Tools
You should also use macOS as we will be using Xcode to build our native apps and our server app.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
- Log in or register at www.packtpub.com.
- Select the
SUPPORT
tab. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR/7-Zip for Windows
- Zipeg/iZip/UnRarX for Mac
- 7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Hands-On-Full-Stack-Development-with-Swift. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Go to our ShoppingListTableViewController.swift
file and update one line inside the didSelectAdd
method. "
A block of code is set as follows:
html, body, #map { height: 100%; margin: 0; padding: 0 }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
@IBAction func didSelectAdd(_ sender: UIBarButtonItem) { requestInput(title: "Shopping list name", message: "Enter name for the new shopping list:", handler: { listName in let listCount = self.lists.count ShoppingList(name: listName).save() { list in self.lists.append(list) self.tableView.insertRows(at: [IndexPath(row: listCount, section: 0)], with: .top) } }) }
Any command-line input or output is written as follows:
~ $ mongod --config /usr/local/etc/mongod.conf
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Build and run the Run
Scheme on the My Mac
platform"
Feedback from our readers is always welcome.
General feedback: Email [email protected]
and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected]
.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.