目录
目录README.md

Frontegg_SwiftUI_SDK

Frontegg is a web platform where SaaS companies can set up their fully managed, scalable and brand aware - SaaS features and integrate them into their SaaS portals in up to 5 lines of code.

Table of Contents

Project Requirements

Supported Languages

Swift: The minimum supported Swift version is now 5.3.

Supported Platforms

Major platform versions are supported, starting from:

  • iOS 14

Getting Started

Prepare Frontegg workspace

Navigate to Frontegg Portal Settings, If you don’t have application follow integration steps after signing up. Copy FronteggDomain to future steps from Frontegg Portal Domain

Setup Hosted Login

Add frontegg package to the project

  • Open you project
  • Choose File -> Add Packages
  • Enter https://github.com/frontegg/frontegg-ios-swift in search field
  • Press Add Package

Create Frontegg plist file

To setup your SwiftUI application to communicate with Frontegg, you have to create a new file named Frontegg.plist under your root project directory, this file will store values to be used variables by Frontegg SDK:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>baseUrl</key>
    <string>https://[DOMAIN_HOST_FROM_PREVIOUS_STEP]</string>
    <key>clientId</key>
    <string>[CLIENT_ID_FROM_PREVIOUS_STEP]</string>
</dict>
</plist>

SwiftUI integration

  • Add Frontegg Wrapper

    • To use Frontegg SDK you have to wrap you Application Scene with FronteggWrapper

      
      import SwiftUI
      import FronteggSwift
      
      @main
      struct demoApp: App {
          var body: some Scene {
              WindowGroup {
                  FronteggWrapper {
                      MyApp()
                  }
              }
          }
      }
    • Modify MyApp.swift file to render content if user is authenticated:

      1. Add @EnvironmentObject var fronteggAuth: FronteggAuth to
      2. Render your entire application based on fronteggAuth.isAuthenticated
      struct MyApp: View {
        @EnvironmentObject var fronteggAuth: FronteggAuth
        
        var body: some View {
          ZStack {
            if fronteggAuth.isAuthenticated {
              [YOU APPLICATION TABS / ROUTER / VIEWS]
            } else  {
              FronteggLoginPage()
            }
          }
        }
      }
    • Add custom loading screen

    To use your own LoadingView / SplashScreen:

    • Build your loading view in separated file
    • Pass LoadingView as AnyView to the FronteggWrapper
      FronteggWrapper(loaderView: AnyView(LoaderView())) {
        MyApp()
      }

UIKit integration

  • Add Frontegg UIKit Wrapper

    • Add Frontegg to the AppDelegate file

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
      
            FronteggApp.shared.didFinishLaunchingWithOptions()
          
            return true
        }
    • Create FronteggController class that extends AbstractFronteggController from FronteggSwift

        //
        //  FronteggController.swift
        //
        
        import UIKit
        import FronteggSwift
        
        class FronteggController: AbstractFronteggController {
        
            override func navigateToAuthenticated(){
                // This function will be called when the user is authenticated
                // to navigate your application to the authenticated screen
                
                let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let viewController = mainStoryboard.instantiateViewController(withIdentifier: "authenticatedScreen")
                self.view.window?.rootViewController = viewController
                self.view.window?.makeKeyAndVisible()
            }
        
        }
    • Create new ViewController and set FronteggController as view custom class from the previous step ViewController custom class

    • Mark FronteggController as Storyboard Entry Point ViewController entry point

    • Setup SceneDelegate for Frontegg universal links:

        func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
            if let url = URLContexts.first?.url,
                url.startAccessingSecurityScopedResource() {
                defer  {
                    url.stopAccessingSecurityScopedResource()
                }
                if url.absoluteString.hasPrefix( FronteggApp.shared.baseUrl ) {
                    FronteggApp.shared.auth.pendingAppLink = url
                    window?.rootViewController = FronteggController()
                    window?.makeKeyAndVisible()
                }
                
            }
        }
        func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
            if let url = userActivity.webpageURL,
               url.absoluteString.hasPrefix( FronteggApp.shared.baseUrl ){
                FronteggApp.shared.auth.pendingAppLink = url
                window?.rootViewController = FronteggController()
                window?.makeKeyAndVisible()
            }
        }
    • Access authenticated user by FronteggApp.shared.auth

          
        //
        //  ExampleViewController.swift
        //
        
        import UIKit
        import SwiftUI
        import FronteggSwift
        import Combine
        
        
        class ExampleViewController: UIViewController {
        
            // Label to display logged in user's email
            @IBOutlet weak var label: UILabel!
            
            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
                
                // subscribe to isAuthenticated and navigate to login page
                // if the user is not authenticated
      
                let sub = AnySubscriber<Bool, Never>(
                    receiveSubscription: {query in
                        query.request(.unlimited)
                    }, receiveValue: { isAuthenticated in
                        if(!isAuthenticated){
                            self.view.window?.rootViewController = FronteggController()
                            self.view.window?.makeKeyAndVisible()
                            return .none
                        }
                        return .unlimited
                        })
                FronteggApp.shared.auth.$isAuthenticated.subscribe(sub)
        
                label.text = FronteggApp.shared.auth.user?.email ?? "Unknown"
            }
             
            @IBAction func logoutButton (){
                FronteggApp.shared.auth.logout()
            }
      
        }
        

Config iOS associated domain

Configuring your iOS associated domain is required for Magic Link authentication / Reset Password / Activate Account.

In order to add your iOS associated domain to your Frontegg application, you will need to update in each of your integrated Frontegg Environments the iOS associated domain that you would like to use with that Environment. Send a POST request to https://api.frontegg.com/vendors/resources/associated-domains/v1/ios with the following payload:

{
    “appId”:[YOUR_ASSOCIATED_DOMAIN]
}

In order to use our API’s, follow this guide to generate a vendor token.

关于
1.1 MB
邀请码