49,19 €
This is a comprehensive guide designed to equip developers with the skills to seamlessly integrate back-end and front-end technologies using Swift and Vapor. The book starts with an overview of full-stack development, including the history and advantages of this approach, before diving into the essential tools and environments needed to get started. Readers will learn how to set up Xcode and the Vapor toolbox, create routes, and implement MVC patterns in Vapor, as well as work with JSON data.
The book then covers advanced topics such as asynchronous programming, HTML rendering, PostgreSQL integration, and error handling within Vapor. Simultaneously, it guides readers through building sophisticated iOS user interfaces using Auto Layout, SwiftUI, and Core Data for data persistence. The content culminates in a full-stack implementation that brings together server-side logic and a connected iOS application, followed by discussions on advanced topics like middleware, WebSockets, and security.
Finally, the book provides in-depth instructions on deploying iOS and Vapor applications using Heroku, Docker, and the App Store. Throughout, real-world projects and exercises reinforce learning, making this an indispensable resource for mastering full-stack iOS development.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 186
Veröffentlichungsjahr: 2024
FULL-STACK iOSDEVELOPMENTWITH SWIFT AND VAPOR
LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY
By purchasing or using this book and companion files (the “Work”), you agree that this license grants permission to use the contents contained herein, including the disc, but does not give you the right of ownership to any of the textual content in the book / disc or ownership to any of the information or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work.
MERCURY LEARNING AND INFORMATION (“MLI” or “the Publisher”) and anyone involved in the creation, writing, or production of the companion disc, accompanying algorithms, code, or computer programs (“the software”), and any accompanying Web site or software of the Work, cannot and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to ensure the accuracy and functionality of the textual material and/or programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold “as is” without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship).
The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work.
The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and/or files, and only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” varies from state to state and might not apply to the purchaser of this product.
Companion files (sample code and figures) are available for downloading by writing to the publisher (with proof of purchase) at [email protected].
FULL-STACK iOSDEVELOPMENTWITH SWIFT AND VAPOR
Hem Dutt
MERCURY LEARNINGAND INFORMATION
Boston, Massachusetts
Copyright ©2024 by MERCURY LEARNING AND INFORMATION.An Imprint of DeGruyter Inc. All rights reserved. Reprinted and revised with permission.
Original title and copyright: Full-Stack iOS Development with Swift and Vapor.Copyright ©2023 by BPB Publications. All rights reserved. ISBN: 978-93-5551-832-3.
This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display, or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher.
Publisher: David Pallai
MERCURY LEARNING AND INFORMATION
121 High Street, 3rd Floor
Boston, MA 02110
www.merclearning.com
800-232-0223
H. Dutt. Full-Stack iOS Development with Swift and Vapor.
ISBN: 978-1-50152-257-4
The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others.
Library of Congress Control Number: 2024939127
242526321 This book is printed on acid-free paper in the United States of America.
Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at 800-232-0223 (toll free).
All of our titles are available in digital format at academiccourseware.com and other digital vendors. Companion files (code samples and figures) are available for downloading (with proof of purchase) by writing to the publisher at [email protected]. The sole obligation of MERCURY LEARNING AND INFORMATION to the purchaser is to replace the files, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product.
This book is dedicated to students and software engineers embarking on their journey as full-stack developers or exploring the dynamic iOS domain. May it inspire and empower you to unlock your full potential in this exciting field.Together, let us embrace the world of full-stack iOS development and strive for excellence.
CONTENTS
Preface
Acknowledgments
About the Author
Chapter 1: Full-Stack Development Overview
Introduction
Structure
Introduction to Full-Stack Development
Brief History
Full-Stack: What Does It Mean?
Minimum Viable Product (MVP)
Airbnb
Foursquare
Problems with Full-Stack Development
Advantages of Full-Stack Development
Swift on Server and Vapor
Swift Packages for Back-End Development
SwiftNIO
AsyncHTTPClient
Swift AWS Lambda Runtime
Soto—AWS SDK Swift
Conclusion
Chapter 2: Setting Up the Environment
Introduction
Structure
Installation of Xcode
Installation of the Vapor Toolbox
Hello World Project (Vapor)
Build and Run Project
Folder Structure
Public Folder
Sources
Run
Tests
Swift Package Manager
Hello World Project (iOS)
Project Structure
Add Hello World Label
Run Xcode Project
Conclusion
Chapter 3: Routing, MVC, and JSON in Vapor
Introduction
Structure
Objectives
Routes
Router Methods
Basic Routes
Nested Routes
Route Parameters
Anything Routes and Catch-All Routes
Query Strings
Route Groups
Model-View-Controller (MVC)
Working with JSON
Posting JSON and Postman App
Conclusion
Chapter 4: Async and HTML Rendering in Vapor
Introduction
Structure
Objectives
Async
Async Await
Migrating to Async/Await
Logging
Environment
Errors
Abort
Abort Error
Debuggable Error
Stack Traces
Swift Backtrace
Error Traces
ErrorMiddleware
Leaf
Conclusion
Chapter 5: PostgreSQL Integration in Vapor
Introduction
Structure
Objectives
Data Persistence with Vapor
Installing and Setting Up PostgreSQL
Fluent ORM
Adding Fluent to a New Project
Adding Fluent to an Existing Project
CRUD Operations
Migrations
Postico
Create and Save Model
Create Model
Save Model
Conclusion
Chapter 6: Building User Interfaces for iOS
Introduction
Structure
Auto Layout with Storyboards
Swift UI
Working with Text
Working with Images
Working with Stacks
Conclusion
Chapter 7: Data Persistence with Core Data and SQLite in iOS
Introduction
Structure
Core Data
Core Data Stack
Include Core Data in a New Project
Include Core Data in an Existing Project
CRUD Operations
Codegen
Category/Extension
Core Data Migrations
Lightweight Data Migration
Networking
Protocol Support
Conclusion
Chapter 8: Full-Stack Implementation
Introduction
Structure
Objectives
Project Outline
Setup Remote Database
Server App
Models
Migrations
Controllers
Config and Routes
iOS App
Models
Networking
User Interface
Test Run
Conclusion
Chapter 9: Advanced Full-Stack Concepts
Introduction
Structure
Objectives
Middleware
Creating Middleware
WebSockets
Messages
Sending
Receiving
Closing
APNS
Security
Authentication
Basic Authentication
Bearer Authentication
Composition
Session
JWT
KeyChain
Adding a Password to Keychain
Conclusion
Chapter 10: Deploying iOS and Vapor Applications
Introduction
Structure
Objectives
Vapor App Deployment
Heroku
Docker
Set up Docker
Build and Run
Production Deployment
iOS App Deployment
Code Signing
Create App Store Connect Record for the App
Add New App
Archive and Upload the App
Configure the App’s Metadata in App Store Connect
Submit App for Review
Conclusion
Index
PREFACE
In this book, we explore the combined power of the Swift programming language, the Vapor framework, and iOS development in order to master the realm of full-stack iOS development.
In today’s interconnected world, the demand for versatile developers who can seamlessly bridge the gap between the backend and frontend is skyrocketing. As the boundaries between server-side and client-side become increasingly blurred, mastering full-stack development has become a valuable skill set.
This book is designed to cater to a wide range of readers, from aspiring developers and students to seasoned iOS professionals seeking to expand their expertise. Whether you are taking your first steps in Swift or are already well-versed in the language, this book equips you with the knowledge and tools necessary to navigate the world of full-stack iOS development with confidence.
We begin by laying the foundation, exploring the essentials of Vapor, Swift, and iOS app development. From there, we cover backend development, covering topics such as persisting data, working with models, and integrating APIs. Simultaneously, we discuss front-end development, unraveling the intricacies of creating compelling user interfaces, networking, and authentication.
Throughout this journey, we emphasize best practices, security considerations, and performance optimization techniques to ensure that you not only build functional applications but also create robust, secure, and high-performing ones.
Real-world projects and hands-on exercises will guide you, allowing you to apply your newly acquired knowledge to practical scenarios. You will witness the power of integrating Swift and Vapor, leveraging their synergistic potential to develop innovative full-stack iOS applications.
I invite you to embark on this transformative journey of becoming a full-stack iOS developer:
Chapter 1: Full-Stack Development Overview – This chapter aims to give a basic understanding of the term “full-stack development,” a brief history of its emergence, and the concept of a minimum viable product. We will also explore the problems and advantages associated with full-stack development and provide a brief introduction to Swift on the server.
Chapter 2: Setting Up the Environment – This chapter aims to provide a basic understanding of the tools and SDKs necessary to begin with Vapor and iOS development. In this chapter, we will cover the installation processes for Xcode, Vapor Toolbox, and starter projects in Vapor, as well as for iOS.
Chapter 3: Routing, MVC, and JSON in Vapor – This chapter aims to provide a basic understanding of the process of creating routes for the server application, a brief understanding of the MVC design pattern, and the creation of controllers in a Vapor application. Furthermore, we will explore the JSON format and handling JSON in a Vapor app and extend this discussion. We will also cover the Postman app that can be used for testing the routes.
Chapter 4: Async and HTML Rendering in Vapor – This chapter aims to extend the basic understanding of Async, Logging, Capturing, Stack Traces, and finally, handling HTML rendering in a Vapor project. In this chapter, we will implement a small part of the code to highlight HTML rendering on a Web page using Leaf and Vapor routes.
Chapter 5: PostgreSQL Integration in Vapor – In this chapter, we will study the integration of PostgreSQL with Vapor. PostgreSQL is an open-source, relational database system that focuses on extensibility and standards. It is designed for enterprise use and also has native support for geometric primitives such as coordinates, which come in handy when working with Fluent. It also supports these primitives and saves nested types, such as dictionaries, directly into PostgreSQL.
Chapter 6: Building User Interfaces for iOS – The aim of this chapter is to further the understanding of the basic building blocks of iOS UI development and complete the circle of full-stack development with Swift.
Chapter 7: Data Persistence with Core Data and SQLite in iOS – Implement data persistence on iOS using Core Data with SQLite as a persistent store. In this chapter, we will write our very first Core Data implementation for storing data in an iOS app. By the end of this chapter, readers will be able to: model data using Xcode’s model editor; add new records to Core Data; fetch a set of records from Core Data; display the fetched records; and learn the basics of Networking.
Chapter 8: Full-Stack Implementation – We implemented small sample codes to facilitate understanding of Vapor and iOS app development. These samples were discussed in isolation to enable readers to grab specific concepts without worrying about the larger picture. In this chapter, we will specifically look into the larger picture and the full-stack implementation of an app.
Chapter 9: Advanced Full-Stack Concepts – In this chapter, we will explore some advanced topics related to full-stack development, which are especially important with respect to overall system design and architecture. Mastering these concepts is essential for commercial application development.
Chapter 10: Deploying iOS and Vapor Applications – The objective of this chapter is to thoroughly examine the deployment process(es) for both our iOS and Vapor apps, making them accessible to the public. For Vapor apps, we will explore deployment via Heroku and Docker. Meanwhile, for iOS apps, the App Store serves as the sole avenue for deployment, and we will cover that process as well in this chapter.
Companion Files: Code samples and figures from the text are available for downloading by writing to the publisher at [email protected].
By the end of this book, you will have the knowledge and confidence to leverage the combined power of the Swift programming language, the Vapor framework, and iOS development in order to master the realm of full-stack iOS development.
ACKNOWLEDGMENTS
I have to start by thanking my beloved wife, Payal Bhardwaj, for keeping me motivated throughout my journey as a writer. She is always by my side and supports me in fulfilling my dreams, however impossible they seem to others. Thank you a ton, my dear, for being the pillar of my strength. You are a superwoman.
I must offer special thanks to my parents, who nurtured my childhood and, despite their limited means, provided me with the best they could do and shaped my character.
I would also like to thank the technical reviewers and editors for helping me shape the chapters and content of this book.
I would also like to thank my colleagues and friends, who have always believed in me and encouraged me.
Finally, and most importantly, I would like to acknowledge my two beautiful children, Adwit and Anika. Thanks, Adwit, for being such a sweetheart and adorable baby and filling the environment with unconditional love, and Anika, for being such a powerhouse and my super girl! I love you both so much.
ABOUT THE AUTHOR
Hem Dutt began his software engineering career in 2010 as a macOS (OS X) and iOS application developer and thereafter designed and developed numerous native macOS and iOS applications for various clients across the globe, while working for multiple MNCs. With more than a decade of experience working on macOS and iOS, Hem Dutt has developed and managed applications in multiple domains, including healthcare, insurance, VPN clients, publishing, IOT, and VoIP. His passion for designing and developing secure, reliable, and modular software is evident from his blogs, client awards and recommendations, and open-source projects. Prior to this book, he authored “Interprocess Communication with macOS: Apple IPC Methods,” cementing his expertise in the Apple ecosystem.
CHAPTER 1
FULL-STACK DEVELOPMENT OVERVIEW
INTRODUCTION
This chapter aims to provide readers with a basic understanding of the term full-stack development, tracing a brief history of the term and that of the minimum viable product concept. We will also explore the problems and advantages of full-stack development and provide a brief introduction to Swift on the server.
STRUCTURE
In this chapter, we will cover the following topics:
■Introduction to full-stack development
■Brief history
■Minimum viable product (MVP)
■Problems with full-stack development
■Advantages of full-stack development
■Swift on server and Vapor
■Swift packages for back-end development
INTRODUCTION TO FULL-STACK DEVELOPMENT
The term “full-stack developer” is commonly used in the software industry, typically referring to a Web developer who can build the front-end and back-end of a Web app. Instead of specializing, a full-stack developer is able to work across the back-end and front-end spectrums of app development.
It is an already established fact that being a specialist in one field or technology and gaining mastery in that particular aspect of technology has distinct advantages, but in the modern world, as technology is rapidly changing and evolving, many companies are seeking talented developers who are able to understand and work on the entire spectrum of front- and back-end technologies and are able to create a usable end product. The survey provided by HackerRank on the most sought-after talent pool in 2020 provides a good insight into the demand for full-stack developers.
As per the HackerRank report, across company sizes, hiring managers agree that full-stack developers are a top priority. According to 38% of hiring managers, it is the #1 role to fill in 2020. Back-end developers and data scientists were ranked second and third priorities, respectively.
Emphasis on full-stack developers was most pronounced in small companies (1–49 employees), 43% of which ranked the role as their top priority.
Though the qualities that define a full-stack developer are a subject of debate, most agree that they should have a basic understanding (or better) of all layers of a tech stack and should be able to generate a minimum viable product on their own. This is why they are especially important in small organizations, where fewer employees often have to do the job of many. See the following figure.
FIGURE 1.1 2020’s most in-demand talent pool.(source: https://info.hackerrank.com/rs/487-WAY-049/images/HackerRank-2020-Developer-Skills-Report.pdf)
As is clear from the report, these developers, also known as full-stack developers, are once again in demand. Does this mean this is not a new phenomenon? Yes, this role has a long history and has had its share of ups and downs, as well as arguments and disagreements from all kinds of people about what a full-stack developer really means and what the level of expertise of the developer in different aspects of the stack should be.
Full-stack developers are useful as generalists who can quickly come up with a minimum viable product (MVP) on their own. They can also be very helpful in providing insight into the entire application infrastructure and contributing to all its parts. It is a sought-after ability for many roles in the software development industry.
BRIEF HISTORY
If we take a broader view, full-stack development has been integral to the programming world since the very beginning, but it was not understood in its current context before.
Full-stack development in the public domain only came to light in 2008, when designing for the Web as well as mobile became mainstream. Earlier, this term was used with varying understandings regularly in the 1970s as well as the 1980s.
The main reason for this was that, at that time, there was not much difference between a back-end programmer and a front-end programmer. Slowly, with time, the distinction between front-end and back-end became defined, and two different streams of application development came into existence, namely, front-end and back-end development. In 2008, the concept of full-stack Web development gained momentum, and with passing years, it has become one of the most in-demand job roles of the present time.
According to the 2021 developer survey by Stack Overflow, over 49.47% of developers describe themselves as full-stack developers. See the following figure.
FIGURE 1.2 Developer roles.(source: https://insights.stackoverflow.com/survey/2021#developer-profile-developer-roles)
During all these times, the term full-stack has gained traction in the Web developer community, but an obvious question is whether it can be applied to mobile application development. This is an interesting question: What constitutes a full-stack mobile app developer?
As we know, mobile app developers work on the client side of applications, or, in loose terms, front-end, and therefore, it might seem logical to assume that a mobile app developer simply needs the skill to develop a back-end to be a full-stack developer.
However, this is not as simple as it looks, and we are going to explore why it is a lot more complicated in the context of an iOS developer.
Full-Stack: What Does It Mean?
The term “stack” here refers to the collection of technologies needed to build an application. For example:
LAMP (Linux, Apache, MySQL, and PHP) or MEAN (MongoDB, Express, Angular, and NodeJS) or MERN (MongoDB, Express, ReactJS, and NodeJS), and so on are technology stacks that have all the parts needed to build a minimum viable product of a Web app.
To understand the term “full-stack” in terms of iOS development, let us use the MERN preceding example and substitute React with Swift to replace the front-end part in a Web app stack with native Swift. Therefore, a full-stack on iOS might resemble MESN (MongoDB, Express, Swift, and NodeJS).
MINIMUM VIABLE PRODUCT (MVP)
As discussed in previous sections, full-stack developers are useful as generalists who can quickly come up with a minimum viable product (MVP) on their own. Let us understand what an MVP is.
A minimum viable product, or MVP, is a product with only enough features to onboard initial targeted customers and validate a product-market fit for a business idea early in the product development cycle. In the software industry, the MVP can actually help the product team receive early user feedback and make it possible to iterate and improve the product.
The basic idea of agile methodology is built on a process for validating and iterating products based on short user input cycles, and so the MVP plays a central role in agile development.
MVP can be understood as the initial version of a new product that allows a team to get the maximum feedback and customer validation from customers with the least amount of effort.
A company might decide to develop and release a minimum viable product because of the following:
■The company wants to release the product to the market as quickly as possible with basic features to gain an early-mover advantage.
■The company wants to test the idea with real target customers before committing a large budget to the product’s full development.
MVP has the following two distinct features:
■It has enough features for consumers to purchase the product.
■It has a feedback mechanism for users so that the company can collect real data for product-market fit.
If you are still wondering what this would look like in the real world, let us examine the stories of a couple of brands that launched successful MVPs.
Airbnb
With limited funds to build the business, the founders used their own apartments to validate their idea of creating a market offering peer-to-peer rental housing online. They created a minimalist Web site, conducted marketing campaigns about their property, and found several customers almost immediately.
Foursquare