Skip to content

Swift library for parsing & displaying Typeform questionnaires.

Notifications You must be signed in to change notification settings

Nice-Healthcare/typeform-swift

Repository files navigation

typeform-swift

Swift library for parsing and displaying Typeform questionnaires.

If you're just getting started with Typeform, be sure to check out the Developer Platform Documentation.

Installation

This library is distributed using Swift Package Manager. Add it to your project via the Xcode Package Dependencies as a dependency within your Package.swift manifest:

let package = Package(
    ...
    // Package Dependencies
    dependencies: [
        .package(url: "https://github.com/Nice-Healthcare/typeform-swift.git", .upToNextMajor(from: "0.3.0"))
    ],
    ...
    // Target Dependencies
    dependencies: [
        .product(name: "Typeform", package: "typeform-swift")
    ]
)

Modules

This library contains multiple modules which comprise a complete toolset for decoding, logic parsing, and display of Typeform questionnaires.

Typeform

This module contains the Typeform schema and additional models for navigating the logic and completion of a form. Start with the Form structure, which is the entity retrieved from the Typeform API.

Forms contain Fields and Logic which act up the fields. Given a particular Position - the screen presented or question being asked - and Responses - answers given to previous questions - the next position can be determined.

let form = Form()
let position = Position.screen(form.firstScreen!)
let responses: [Reference: ResponseValue] = [:]
let nextPosition = try form.next(from: position, given: responses)

TypeformPreview

This module is used primarily internal to the library. It provides some example resources for decoding/parsing tests as well as references to support SwiftUI previews.

TypeformUI

The TypeformUI module is a SwiftUI implementation used to display and navigate through a Typeform questionnaire. It is designed to be self-contained and customizable to fit in with your apps design.

The FormView is initialized with a Form and a Conclusion function/lambda callback. In most cases, the conclusion will include the responses given by the individual questioned. (The only instance where they are not provided is the canceled case.)

struct MyView: View {
    let form: Form

    @State private var presentForm: Bool = false

    var body: some View {
        Button {
            presentForm = true
        } label: {
            Text("Present Form")
        }
        .sheet(isPresented: $presentForm) {
            NavigationView {
                FormView(form: form) { conclusion in
                    presentForm = false
                    // handle conclusion
                }
            }
            .navigationViewStyle(.stack)
            .interactiveDismissDisabled()
        }
    }
}

Supported Question Types

Note, this library is incomplete; not every question type or condition has been implemented. Want to help fix a bug or make improvements? Consider becoming a contributor!

All of the structural components are supported: Welcome Screen, Ending, Statement, and Question Group. Learn more about Typeform Question Types. The currently supported types are:

  • Date
  • Short Text
  • Long Text
  • Number
  • Rating
  • Multiple Choice
  • Yes/No
  • Dropdown
  • Opinion Scale

Customization

Every app is unique, so the ability to customize the look and feel of the questions presentation was built in to the library from the start.

Settings

The TypeformUI.Settings structure allows for the customization of several aspects of the libraries interface presentation & localization.

Navigation Style (Layout)

There are several navigation styles available:

callToAction inline navigation
Call-to-Action - Navigation Style Inline - Navigation Style Navigation Bar - Navigation Style
The 'Call-to-Action' navigation style anchors a button to the bottom of the views safe area. The button is always visible and content scrolls behind it. The 'Inline' navigation style anchors the a button to the bottom of the content and scrolls with it. The 'Navigation' navigation style fixes the button to the 'Navigation Bar'.

Typography & Theme

Four typography styles are used in the UI presentation and can be customized through Settings.Typography. Each typography style can be set with a SwiftUI.Font and SwiftUI.Color.

Title Font Prompt, Caption & Body Fonts
Title Font Example Other Font Examples

The 'Title' font is used primarily on Welcome Screens, Thank-You/Ending Screens, and Statement Fields.

Many other elements can be configured including colors, borders, padding, and spacing.

Headers & Footers

A form can further be customized by providing an optional header and/or footer. These are completely custom views that will be displayed along with each field (question).

About

Swift library for parsing & displaying Typeform questionnaires.

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages