O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hands-On Full-Stack Development with Swift

Book Description

Build full-stack shopping list applications from scratch for web and mobile platforms using Xcode, Vapor, and Swift

About 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 framework

Who This Book Is For

This 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 lists

In Detail

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.

Style and approach

A step-by-step tutorial-based approach that teaches you full-stack Swift through the development of a single application on several platforms.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Full-Stack Development with Swift
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with Server Swift
    1. Modern app development
    2. Swift's evolution
      1. Open source
      2. Server-side Swift
    3. Benefits of server-side Swift
    4. Getting started with Swift package manager
      1. Building a Swift package
      2. Publishing a Swift package
      3. Consuming a Swift package
      4. Installing the package's executable
    5. How do the web servers work?
      1. User requesting a web page
      2. Mobile application requesting data
      3. HTTP request and response
    6. Building a web server in Swift
    7. Server-side web frameworks
      1. Vapor
    8. Book roadmap
    9. Summary
  8. Creating the Native App
    1. Features of our Shopping List app
    2. Creating an app
    3. Blueprinting the Shopping List Item model
      1. Exercise
        1. Exercise answer
    4. Controlling the flow of our application using View Controller
    5. Wiring up the view
      1. Table View Controller
    6. Adding items to the list
    7. Editing the list
    8. Loading and auto-saving the Shopping List
    9. Multiple lists
      1. Refactoring to share code
      2. Blueprinting the Shopping List Model
      3. The Shopping List Table View Controller
    10. Summary
  9. Getting Started with Vapor
    1. What is Vapor?
    2. Building servers using Vapor's engine
      1. Building a basic HTTP server
      2. Building a static file server
      3. Building a WebSocket server
    3. Building a Vapor application from scratch
    4. Vapor toolbox
      1. Installing the Vapor toolbox
      2. Vapor toolbox commands
      3. Creating a Vapor application using the toolbox
    5. Vapor folder structure
      1. Vapor config
      2. Vapor droplet
      3. Views
      4. Controllers
    6. Summary
  10. Configuring Providers, Fluent, and Databases
    1. Shopping List API Vapor app
    2. What are Providers?
      1. Building your first Provider
      2. Exercise time
      3. Adding a Provider
    3. Getting started with databases
      1. What is MongoDB?
      2. How to install and run MongoDB
    4. What are ORM and Fluent?
      1. Fluent in action
        1. Creating an item
        2. Updating an item
        3. Getting all items
        4. Finding an item
        5. Finding items using filter
        6. Deleting an item
        7. Counting items
      2. Relations in Fluent
        1. One to one (parent-child relation)
        2. One to many
        3. Many to many
    5. Connecting with MongoDB
      1. Configuring Fluent config
      2. Mongo config - mongo.json
      3. Adding MongoProvider
    6. Summary
  11. Building a REST API using Vapor
    1. Routing in Vapor
      1. HTTP methods
      2. Routers
      3. Nested routing
      4. Dynamic routing
      5. Wildcard routing
      6. Routing parameters
    2. Vapor Models
      1. The Shopping List Model
      2. Preparation protocol
      3. JSONConvertible protocol
      4. ResponseRepresentable protocol
      5. Updateable protocol
      6. Item Model
    3. Controllers in Vapor
      1. RESTful Controller
      2. Shopping List controller
    4. REST API in action
      1. Creating the Shopping List
      2. Getting the Shopping List
      3. Updating the Shopping List
      4. Deleting the Shopping List
      5. Creating items
    5. Exercise
    6. Summary
  12. Consuming API in App
    1. Xcode Workspace
    2. Making network requests
      1. Network configuration
      2. Request helper
    3. Fetching data from the server
    4. Debugging the app and server side by side
    5. Adding a Shopping List
    6. Deleting a Shopping List
      1. Exercise
    7. Adding a Shopping List Item
    8. Deleting an item
    9. Checking and unchecking an item
    10. Summary
  13. Creating Web Views and Middleware
    1. View rendering in Vapor app
      1. What is Leaf?
    2. Adding Leaf Provider
    3. Serving JSON and HTML formats
      1. Creating a middleware
      2. Creating a BaseResourceController
    4. Adding JavaScript
      1. Creating a new Shopping List
      2. Deleting a Shopping List
      3. Adding an Item
      4. Deleting an Item
      5. Checking and unchecking an Item
    5. Summary
  14. Testing and CI
    1. Testing the Vapor application
      1. Setting up the test environment
      2. Running tests
    2. Testing RESTful routes
      1. Fetching all Shopping Lists
      2. Creating a Shopping List
      3. Deleting the Shopping List
      4. Updating the Shopping List
    3. Exercise
    4. Automated testing pipeline
      1. Enabling Travis build check on Pull request
    5. Summary
  15. Deploying the App
    1. Where can we deploy a Vapor App?
    2. Deploying to Heroku
      1. Priming the app for deployment
      2. Configuring and deploying Vapor to Heroku
      3. Adding the MongoDB Heroku addon
    3. Setting up Continuous Deployment
    4. Exercise
    5. Summary
  16. Adding Authentication
    1. Creating a User model
      1. Best practices for storing password
      2. Getting started with the User model
    2. User has many Shopping Lists
    3. Adding Registration and Login
    4. Showing user specific Shopping Lists
    5. Adding token-based authentication for app
      1. Testing the token-based authentication
    6. Adding authentication flow to iOS app
      1. Bringing it all together in the Storyboard
    7. Summary
  17. Building a tvOS App
    1. Shopping List app on tvOS
    2. Sharing code between iOS and tvOS
      1. Making code work with both iOS and tvOS apps
    3. Configuring the tvOS storyboard
    4. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think