Step 1: SDK Installation

SDK can be installed via both Cocoapods and Swift Package Manager.

Please find the latest version of the SDK here.

Cocoapods

  • Open your app’s project file .xcodeproj.
  • Add the following line into the dependencies section of your project’s Podfile:
pod 'OtplessSwiftLP', 'latest_version'

Make sure to run the following commands in your root folder to fetch the dependency.

pod repo update
pod install

Swift Package Manager

  1. In Xcode, click File > Swift Packages > Add Package Dependency.
  2. In the dialog that appears, enter the repository URL: https://github.com/otpless-tech/iOS-LP.
  3. Select the dependency rule as exact version and use the latest version.

Step 2: Setup SDK in your App

Add the following keys in your info.plist file:

info.plist
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>otpless.{{YOUR_APP_ID}}</string>
        </array>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>otpless</string>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
    <string>otpless</string>
    <string>gootpless</string>
    <string>com.otpless.ios.app.otpless</string>
    <string>googlegmail</string>
</array>

Import the SDK at the top of your ViewController.swift:

import OtplessSwiftLP

Step 3: Implement the ConnectResponseDelegate

Your ViewController should conform to ConnectResponseDelegate:

LoginViewController.swift
func onConnectResponse(_ response: [String: Any]) {
    if let error = response["error"] as? String {
        print("Error: \(error)")
    } else if token = response["token"] as? String {
        print("Token: \(token)")
        // Send this token to your server to validate and get user details.
    } else {
        // Unknown error occurred
        print("Unknown response: \(response)")
    }
}

Step 4: Initialize the SDK, Set Delegate and Start

Set the response delegate and optionally enable socket logging:

LoginViewController.swift
override func viewDidLoad() {
    super.viewDidLoad()
    
    OtplessSwiftLP.shared.setResponseDelegate(self)

    // Initialize SDK
    OtplessSwiftLP.shared.initialize(appId: "YOUR_APP_ID", secret: "YOUR_SECRET")
}

To start the authentication process, use:

LoginViewController.swift
func start() {
  OtplessSwiftLP.shared.start(vc: self)
}

Step 5: Stop the process

When your login page is closed or login is successful, stop the Otpless’ authentication process:

LoginViewController.swift
OtplessSwiftLP.shared.cease()

Make sure that initialize() is called again if you call cease().