Sharing Swift Code Between iOS and Server Applications
In this tutorial, you’ll learn how share code between iOS and server applications. By Christian Weinberger.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Sharing Swift Code Between iOS and Server Applications
25 mins
- Getting Started
- Why Share Code?
- Exploring the Vapor Project
- Running the Vapor Project
- Spinning up the Database
- Running the Migrations
- Testing the API Using a REST Client
- Exploring the iOS Project
- Running the iOS Project
- Identifying Sharable Components
- Creating a Shared Package in Vapor
- Migrating UserAPIModel
- Migrating CategoryAPIModel
- Migrating AcronymAPIModel
- Integrating With Your Vapor App
- Setting Up a Local Git Repository for Your Library
- Adding the Library to Your Vapor App
- Configuring the API Models
- Integrating With Your iOS App
- Adding the Library to Your iOS App
- Using the Shared API Models
- Cleaning Up
- Where to Go From Here?
Cleaning Up
Stop your Vapor project to stop the Vapor server.
To stop the PostgreSQL database, head to the Terminal window where docker-compose is still running, and type Control-C.
To remove the images and containers created by docker-compose, in Terminal, in the same directory as your docker-compose file, run:
docker-compose down -v --rmi all --remove-orphans
Where to Go From Here?
You can download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.
In this article, you’ve learned how to create a library to share Swift code between server-side apps and iOS apps! You’ve built a shared library using SwiftPM. You’ve also learned how to integrate the library into an iOS app and a Server-Side Swift app.
Now that you have code shared between two different apps, try adding new properties to the models and see how easy it is to keep them working together.
If you’re looking for a challenge beyond the scope of this article, here are a couple things you can try:
- Migrate
Endpoints
to the shared package, similar to what you did with the API models. - Share
TILError
from the Vapor project with iOS.
To learn more about Server-Side Swift and Vapor, check out:
- Server-Side Swift with Vapor
- Server Side Swift with Vapor: Basic Controllers
- Server Side Swift with Vapor: Using Fluent and Persisting Models
We hope you enjoyed this tutorial. If you have any questions, comments or ideas on what else to share between iOS and Vapor, please join the forum discussion below!