
- 356 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Hands-On Full-Stack Development with Swift
About this book
Build full-stack shopping list applications from scratch for web and mobile platforms using Xcode, Vapor, and SwiftAbout This Book• Build, package, and deploy an end-to-end app solution for mobile and web with Swift 4• Increase developer productivity by creating reusable client and server components• Develop backend services for your apps and websites using Vapor frameworkWho This Book Is ForThis book is for developers who are looking to build full-stack web and native mobile applications using Swift. An understanding of HTML, CSS, and JavaScript would be beneficial when building server-rendered pages with Vapor.What You Will Learn• Get accustomed to server-side programming as well as the Vapor framework• Learn how to build a RESTful API• Make network requests from your app and handle error states when a network request fails• Deploy your app to Heroku using the CLI command• Write a test for the Vapor backend• Create a tvOS version of your shopping list app and explore code-sharing with an iOS platform• Add registration and authentication so that users can have their own shopping listsIn DetailMaking 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.Style and approachA step-by-step tutorial-based approach that teaches you full-stack Swift through the development of a single application on several platforms.
Frequently asked questions
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Information
Configuring Providers, Fluent, and Databases
- How to Bootstrap an API based Vapor application?
- What is a Vapor Provider, and how to use it in our application?
- How to build a Provider of our own?
- What is Fluent, and how to use the Fluent provider?
- Databases and MongoDB, and how to get started with them
- How to connect your application to MongoDB to fetch and save data in the database?
Shopping List API Vapor app
- Open the Terminal and create a new API based Vapor application using the toolbox:
$ vapor new ShoppingListServer --template=ankurp/api-template
- This will create the new application based on the API template. Go into this folder in the Terminal and create an Xcode project file using the following command:
$ vapor xcode -y
- The preceding command will create an Xcode project file for the Vapor application and open up the Xcode project. We can now write our code in the Xcode IDE, instead of using a plain text editor to write code for our Vapor application. To run our Vapor application, we need to switch the Scheme to Run, and make sure that My Mac is selected before clicking the play button to start our server:

- Open the browser to http://localhost:8080/hello and you should see a JSON object in the browser:

What are Providers?
public protocol Provider {
static var repositoryName: String { get }
static var publicDir: String { get }
static var viewsDir: String { get }
init(config: Config) throws
func boot(_ config: Config) throws
func boot(_ droplet: Droplet) throws
func beforeRun(_ droplet: Droplet) throws
} Building your first Provider
- Create a folder, call it HealthcheckProvider, and open the folder in the Terminal.
- Once you are in the HealthcheckProvider folder, run the following command in the Terminal to initialize a Swift package:
$ swift package init
- Inside Package.swift, add Vapor to the dependencies section:
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.4.4")),
],
- Also, add "Vapor" as the dependency for the HealthcheckProvider target under the targets section:
.target(
name: "HealthcheckProvider",
dependencies: ["Vapor"]),
- Now, go to the Sources/HealthcheckProvider folder and rename the HealthcheckProvider.swift file to Provider.swift.
- Once the file is renamed, we can start adding code to our provider. To make a Provider, we need to follow the Provider protocol defined by Vapor. This is easy to do; we implement the Vapor.Provider protocol in our Provider class as follows:
import Vapor
public final class Provider: Vapor.Provider {
- Then, we need to add the repositoryName static variable in our class, as required by the Provider protocol; this will be the name of our Provider. We will also define a variable called healthcheckUrl that will hold the route for our healthcheck endpoint:
public static let repositoryName: String = "healthcheck-provider"
public var healthCheckUrl: String?
- Next, we will add an initializer for our class, which will take a Config object and set the healthcheckUrl based on the health check URL defined in the healthcheck.json files under the Config folder:
public init(config: Config) throws {
if let healthCheckUrl = config["healthcheck", "url"]?.string {
self.healthCheckUrl = healthCheckUrl
}
} - Next, we need to define the boot method, as defined in the protocol. Since we do not do anything in this method, we can leave the body of the method empty. This method is called after the Provider has been initialized:
public func boot(_ config: Config) throws {} - There is another boot method that needs to be defined and this is called by the droplet when it is initialized. The Droplet object is passed into this boot method and it is a good place to define our healthcheck route:
public func boot(_ drop: Droplet) {
guard let healthCheckUrl = self.healthCheckUrl else {
return drop.console.warning("MISSING: healthcheck.json config in Config folder. Healthcheck URL not addded.")
}
drop.get(healthCheckUrl) { req in
return try Response(status: .ok, json: JSON(["status": "up"]))
}
} - The last protocol method we need to define is the beforeRun method, whic...
Table of contents
- Title Page
- Copyright and Credits
- Dedication
- Packt Upsell
- Contributors
- Preface
- Getting Started with Server Swift
- Creating the Native App
- Getting Started with Vapor
- Configuring Providers, Fluent, and Databases
- Building a REST API using Vapor
- Consuming API in App
- Creating Web Views and Middleware
- Testing and CI
- Deploying the App
- Adding Authentication
- Building a tvOS App
- Other Books You May Enjoy