UIOnboarding Documentation

repository·main·Indexed 21 days ago

https://github.com/lascic/uionboarding

An animated, configurable welcome screen library for iOS and iPadOS inspired by Apple's Stocks app. It provides UIOnboardingViewController for UIKit and supports SwiftUI integration via UIViewControllerRepresentable. The library includes UIOnboardingViewConfiguration for customizing app icons, titles, features, and buttons, and supports multiple device form factors and accessibility features.

Tokens
2.7K
Snippets
5
Records
6
Agent score
24%

What's inside UIOnboarding

  1. Integrate UIOnboarding with SwiftUI

    main

    Since UIOnboardingViewController is a UIKit component, you must wrap it using the UIViewControllerRepresentable protocol to use it in SwiftUI.

    Recommended pattern:

    1. Create an OnboardingView struct conforming to UIViewControllerRepresentable.
    2. Use a Coordinator class within the struct to implement UIOnboardingViewControllerDelegate.
    3. Present the OnboardingView using the .fullScreenCover() modifier.
    4. Use .edgesIgnoringSafeArea(.all) to ensure the onboarding covers the entire screen.
    import SwiftUI
    import UIOnboarding
    
    struct OnboardingView: UIViewControllerRepresentable {
        typealias UIViewControllerType = UIOnboardingViewController
    
        func makeUIViewController(context: Context) -> UIOnboardingViewController {
            let onboardingController: UIOnboardingViewController = .init(withConfiguration: .setUp())
            onboardingController.delegate = context.coordinator
            return onboardingController
        }
        
        func updateUIViewController(_ uiViewController: UIOnboardingViewController, context: Context) {}
        
        class Coordinator: NSObject, UIOnboardingViewControllerDelegate {
            func didFinishOnboarding(onboardingViewController: UIOnboardingViewController) {
                onboardingViewController.dismiss(animated: true, completion: nil)
            }
        }
    
        func makeCoordinator() -> Coordinator {
            return .init()
        }
    }
    
    // Usage in a SwiftUI View
    struct ContentView: View {
        @State private var showingOnboarding = true
        
        var body: some View {
            NavigationView {
                Text("Hello, UIOnboarding!")
                    .fullScreenCover(isPresented: $showingOnboarding) {
                        OnboardingView()
                            .edgesIgnoringSafeArea(.all)
                    }
            }
        }
    }
  2. Present UIOnboardingViewController in UIKit

    main

    To use UIOnboardingViewController in a UIKit application, initialize it with a UIOnboardingViewConfiguration and present it from a view controller that is embedded in a UINavigationController. The onboarding controller is designed to be presented in full screen.

    To handle dismissal, implement the UIOnboardingViewControllerDelegate method didFinishOnboarding(onboardingViewController:).

    // In the view controller you're presenting
    import UIKit
    import UIOnboarding
    
    let onboardingController: UIOnboardingViewController = .init(withConfiguration: .setUp())
    onboardingController.delegate = self
    navigationController?.present(onboardingController, animated: false)
    
    // Dismissing via delegate
    extension ViewController: UIOnboardingViewControllerDelegate {
        func didFinishOnboarding(onboardingViewController: UIOnboardingViewController) {
            onboardingViewController.modalTransitionStyle = .crossDissolve
            onboardingViewController.dismiss(animated: true, completion: nil)
        }
    }
  3. Install UIOnboarding via Swift Package Manager

    main

    To add UIOnboarding to your Xcode project, use the Swift Package Manager. Navigate to File > Add Packages... in Xcode and enter the repository URL. You can choose to pin to a specific version (starting from 2.0.0) or use the main branch.

    .package(url: "https://github.com/lascic/UIOnboarding.git", from: "2.0.0")
    // or
    .package(url: "https://github.com/lascic/UIOnboarding.git", branch: "main")
  4. Use the UIOnboarding Demo Projects

    main

    If you want to see how the library is implemented in different environments, you can use the demo projects located in the /Demo directory of the repository. These include examples for both UIKit and SwiftUI.

    To run them:

    1. Clone the repository or download the /Demo folder as a .zip file.
    2. Open the project in Xcode.
    3. Configure the projects with your own provisioning profile before building and running on a simulator or physical device.
  5. Show onboarding only on first launch

    main

    To ensure the onboarding screen is only presented once per user, use UserDefaults to track completion. Check the flag before presenting the onboarding, and set the flag to true inside the didFinishOnboarding delegate method.

    // Check if onboarding was already completed
    if !UserDefaults.standard.bool(forKey: "hasCompletedOnboarding") {
        showOnboarding()
    }
    
    // Inside the delegate method
    func didFinishOnboarding(onboardingViewController: UIOnboardingViewController) {
        onboardingViewController.dismiss(animated: true) {
            UserDefaults.standard.set(true, forKey: "hasCompletedOnboarding")
        }
    }
  6. Configure UIOnboardingViewConfiguration

    main

    The UIOnboardingViewConfiguration object requires six non-optional components to set up the onboarding experience. You can create a helper struct to manage these components and extend UIOnboardingViewConfiguration to provide a static setup method.

    Required components:

    1. appIcon: A UIImage representing the app icon.
    2. firstTitleLine: An NSMutableAttributedString for the first line of the welcome title.
    3. secondTitleLine: An NSMutableAttributedString for the second line of the welcome title. (Provide an empty string if only one line is needed).
    4. features: An Array<UIOnboardingFeature> containing core feature descriptions.
    5. textViewConfiguration: A UIOnboardingTextViewConfiguration for notice text (e.g., Privacy Policy).
    6. buttonConfiguration: A UIOnboardingButtonConfiguration for the continuation button.
    import UIKit
    import UIOnboarding
    
    // 1. Define a helper to create the components
    struct UIOnboardingHelper {
        static func setUpIcon() -> UIImage { .init(named: "onboarding-icon")! }
        static func setUpFirstTitleLine() -> NSMutableAttributedString { .init(string: "Welcome to") }
        static func setUpSecondTitleLine() -> NSMutableAttributedString { .init(string: "My App") }
        
        static func setUpFeatures() -> [UIOnboardingFeature] {
            return [.init(icon: .init(named: "f1")!, title: "Feature 1", description: "Desc 1")]
        }
    
        static func setUpNotice() -> UIOnboardingTextViewConfiguration {
            return .init(icon: .init(named: "notice-icon")!, text: "Notice text", linkTitle: "Learn more", link: "https://example.com", linkColor: .blue)
        }
    
        static func setUpButton() -> UIOnboardingButtonConfiguration {
            return .init(title: "Continue", backgroundColor: .blue)
        }
    }
    
    // 2. Extend the configuration to provide a setup method
    extension UIOnboardingViewConfiguration {
        static func setUp() -> UIOnboardingViewConfiguration {
            return .init(
                appIcon: UIOnboardingHelper.setUpIcon(),
                firstTitleLine: UIOnboardingHelper.setUpFirstTitleLine(),
                secondTitleLine: UIOnboardingHelper.setUpSecondTitleLine(),
                features: UIOnboardingHelper.setUpFeatures(),
                textViewConfiguration: UIOnboardingHelper.setUpNotice(),
                buttonConfiguration: UIOnboardingHelper.setUpButton()
            )
        }
    }
    
    // 3. Use it
    let config = UIOnboardingViewConfiguration.setUp()
    let vc = UIOnboardingViewController(withConfiguration: config)