Vapor server and companion iOS app examples

我怕爱的太早我们不能终老 提交于 2019-11-29 02:32:58

I've been using Swift Packages for both iOS/macOS clients and Vapor backend development for some time. I'm in the process of gathering the lessons learned into a generic set of Open Source repositories: VaporExampleLabs/Synergy*.

It is possible to have Swift Packages which can be used on the Swift server side, and on iOS devices and in macOS applications. A greater separation between the views (Vapor View, iOS UIView, macOS NSView) and underlaying application logic, allows more code to be shared in Swift Packages across the platforms.

The VaporExampleLabs/Synergy* collection of packages & projects provides various working pieces of how to setup a Vapor server, iOS device client and a macOS client to use the same Swift Packages.

Each part highlights some key findings for the use of a Swift code shared in both Server backend and Client device software development.

Main Obervations:

  • Separate as much application logic from the views as possible for placement into Swift Packages.
  • Consider using Swift and/or C libraries which are across platform.
  • Xcode sometimes requires extra manual steps like adding the C header files in the build phase.
  • Swift Packages can be generated as Xcode projects for use as subprojects in the iOS and macOS applications. The Swift Package subprojects serve as a workaround until iOS/macOS/tvOS applications can build with SPM.

Note: The Synergy set of projects and packages are still a work in progress for gathering lessons learned. The goal is to create a full (self-contained) set of backend-to-client connected examples sharing common Swift Packages.


SynergySqliteC

Sometimes lower level libraries are not common between the server and device. For example, the database Object Relationship Model (ORM) is different for Vapor and iOS/macOS have CoreData. Vapor provides Fluent. iOS/macOS provides CoreData.

This example illustrates direct use the SQLite C source code at the lowest level.

SynergySqliteC ⇗ shows how to setup a cross platform C library into a Swift Package.

A pure C package may require manual addition of the *.h files to the project treee for building in an Xcode project.

SynergySQLiteFramework

SynergySQLiteFramework ⇗ provides a generic Swift framework for using the C-base Swift Package SynergySqliteC.

SynergyAPI

SynergyAPI ⇗ provides the Codable interface for the collection of Synergy applications. Uses the generics SQLite package SynergySQLiteFramework.

SynergyVapor

SynergyVapor ⇗ provides an example Vapor web application which uses the SynergyAPI.

Synergy Clients

Synergy ⇗ provides both an iOS and macOS client Xcode projects. Both the iOS and macOS clients use the SynergyAPI package.

Swift Packages in iOS/macOS Applications

Swift Package Manager Project stated the following prior to 2019.06.03:

Note that at this time the Package Manager has no support for iOS, watchOS, or tvOS platforms.

On 2019.06.03, the Swift Package Manager Project was updated to indicate some SPM support is present in Xcode 11.

Xcode 11 integrates with libSwiftPM to provide support for iOS, watchOS, and tvOS platforms.

Even though the Swift Package manager can not directly create an iOS or macOS application, Swift Packages can still be used as subprojects in the respective Xcode application projects.

cd SynergyAPI

swift package generate-xcodeproj \
    --xcconfig-overrides Package.iOS.xcconfig \
    --output SynergyAPI_iOS

swift package generate-xcodeproj \
    --xcconfig-overrides Package.macOS.xcconfig \
    --output SynergyAPI_macOS

Resources

This official list of Vapor projects contains a couple of iOS/server project pairs.

This is probably the best example from that list.

Server: https://github.com/cocoaheadsru/server

Client: https://github.com/cocoaheadsru/application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!