Book Image

Hands-On Full-Stack Development with Swift

By : Ankur Patel
Book Image

Hands-On Full-Stack Development with Swift

By: Ankur Patel

Overview of this book

Making Swift an open-source language enabled it to share code between a native app and a server. Building a scalable and secure server backend opens up new possibilities, such as building an entire application written in one language—Swift. This book gives you a detailed walk-through of tasks such as developing a native shopping list app with Swift and creating a full-stack backend using Vapor (which serves as an API server for the mobile app). You'll also discover how to build a web server to support dynamic web pages in browsers, thereby creating a rich application experience. You’ll begin by planning and then building a native iOS app using Swift. Then, you'll get to grips with building web pages and creating web views of your native app using Vapor. To put things into perspective, you'll learn how to build an entire full-stack web application and an API server for your native mobile app, followed by learning how to deploy the app to the cloud, and add registration and authentication to it. Once you get acquainted with creating applications, you'll build a tvOS version of the shopping list app and explore how easy is it to create an app for a different platform with maximum code shareability. Towards the end, you’ll also learn how to create an entire app for different platforms in Swift, thus enhancing your productivity.
Table of Contents (19 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Preface

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.

Who this book is for

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.

What this book covers

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 5Building 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 8Testing 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.

To get the most out of this book

You should have basic knowledge of the following topics:

  1. Swift
  2. Xcode
  3. Storyboard and Autolayout
  4. HTML
  5. JavaScript
  6. CSS
  7. Terminal/Command Line Tools

You should also use macOS as we will be using Xcode to build our native apps and our server app.

Download the example code files

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:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. 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!

Conventions used

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"

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

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.

Reviews

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.