OkVerify iOS

The OkVerify iOS framework enables you to verify a user's OkHi address automatically

Getting started

Make sure you have completed the OkHi on your iOS app steps

For OkVerify to work correctly "Always" Location permission needs to be granted by your users. The library provides a OkHiLocationService class that enables you to manage these permission requirements as shown below. Make sure to add "Location updates" at Background Modes under Signing & Capabilities of your target as illustrated in the previous steps

Installation

Add Swift Package dependency

https://github.com/OkHi/okverify-xcframework.git

Usage

Configure the OkHiAuth object

In your AppDelegate.swift file configure the OkHi Auth object with your client key and branch ID. Make sure to also setup your app's context with meta data about your application

import OkCore
import OkCollect

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let okHiAppContext = OkHiAppContext().withAppMeta(name: "My Awesome App", version: "1.0.0", build: "1")
    let okHiAuth = OkHiAuth(
        branchId: "<my_branch_id>",
        clientKey: "<my_client_key>",
        environment: Environment.sandbox, // make sure to change this to prod when you're ready to go live 🚀
        appContext: okHiAppContext
    )
    OkHiCollect.initialize(with: okHiAuth)
    OkHiVerify.initialize(with: okHiAuth) // include this!
    return true
}

Start verifying addresses

To start verifying addresses we'll extend the code outlined in OkCollect iOS section and demonstrate how you can use these two libraries

import UIKit
import OkCore
import OkCollect
import OkVerify

class ViewController: UIViewController {
    private let okCollect = OkHiCollect() // initialise the OkCollect class
    private let okhiLocationService = OkHiLocationService() // optionally use the OkHiLocationService to manage location permissions
    private var okVerify:OkHiVerify? // define a OkHiVerify object that will be initialised with the OkHiUser
    private let okHiUser = OkHiUser(phoneNumber: "+254712345678").with(firstName: "Julius").with(lastName: "Kiano") // define your OkHiUser
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        okCollect.delegate = self // set your view controller as an OkCollect delegate
        okhiLocationService.delegate = self // set your view controller as an OkHiLocationService delegate
        okVerify = OkHiVerify(user: okHiUser) // initialise your okVerify variable
        okVerify?.delegate = self // set your view controller as an OkHiVerify delegate
    }

    @IBAction func onRequestPermissionButtonPress(_ sender: UIButton) {
        if !okhiLocationService.isLocationPermissionGranted() {
            okhiLocationService.requestLocationPermission(withBackgroundLocationPermission: true)
        }
    }
    
    @IBAction func onCreateAddressPress(_ sender: UIButton) {
        let okHiTheme = OkHiTheme().with(logoUrl: "https://cdn.okhi.co/icon.png").with(appBarColor: "#ba0c2f").with(appName: "OkHi")
        let okHiConfig = OkHiConfig().enableStreetView().enableAppBar()
        guard let vc = okCollect.viewController(with: okHiUser, okHiTheme: okHiTheme, okHiConfig: okHiConfig) else {
            return
        }
        self.present(vc, animated: true, completion: nil)
        
    }
    
    func showMessage(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
    
    func startVerification(location: OkHiLocation) {
        okVerify?.start(location: location) // start the address verification process
    }
    
}

extension ViewController: OkHiLocationServiceDelegate {
    func okHiLocationService(locationService: OkHiLocationService, didChangeLocationPermissionStatus locationPermissionType: LocationPermissionType, result: Bool) {
        if locationPermissionType == LocationPermissionType.always && result {
            showMessage(title: "Location Permission", message: "Status granted")
        }
        
    }
}

extension ViewController: OkCollectWebviewDelegate {
    func collect(didEncounterError error: OkHiError) {
        showMessage(title: error.code, message: error.message)
    }
    
    func collect(didSelectAddress user: OkHiUser, location: OkHiLocation) {
        startVerification(location: location)
    }
}

extension ViewController: OkVerifyDelegate {
    func verify(_ okVerify: OkHiVerify, didEncounterError error: OkHiError) {
        showMessage(title: error.code, message: error.message)
    }
    
    func verify(_ okVerify: OkHiVerify, didStart locationId: String) {
        showMessage(title: "Verification started", message: locationId)
    }
    
    func verify(_ okVerify: OkHiVerify, didEnd locationId: String) {
        
    }
}

Recipes

Start verification without OkCollect

Its possible to construct your own OkHiLocation object using an identifier (provided by us, when a user creates an address using OkCollect) and a pair of coordinates

import UIKit
import OkCore
import OkVerify

class ViewController: UIViewController {
    
    private let okHiUser = OkHiUser(phoneNumber: "+254712345678").with(firstName: "Julius").with(lastName: "Kiano") // define your OkHiUser
    private let okhiLocation = OkHiLocation(identifier: "XthXJwz0qO", lat: -1.2664398, lon: 36.7657483) // define your OkHiLocation
    private var okVerify:OkHiVerify? // define a OkHiVerify object that will be initialised with the OkHiUser
    private let okhiLocationService = OkHiLocationService() // optionally use the OkHiLocationService to manage location permissions
    
    override func viewDidLoad() {
        super.viewDidLoad()
        okVerify = OkHiVerify(user: okHiUser)
        okVerify?.delegate = self
    }
    
    @IBAction func onButtonPress(_ sender: UIButton) {
        if okhiLocationService.isLocationPermissionGranted() && okhiLocationService.isLocationServicesAvailable() {
            startAddressVerification()
        }
    }
    
    func startAddressVerification() {
        okVerify?.start(location: okhiLocation)
    }
}

extension ViewController: OkVerifyDelegate {
    func verify(_ okVerify: OkHiVerify, didEncounterError error: OkHiError) {
        // handle errors
    }
    
    func verify(_ okVerify: OkHiVerify, didStart locationId: String) {
        // verification started successfully
        print(locationId)
    }
    
    func verify(_ okVerify: OkHiVerify, didEnd locationId: String) {
        // verification stopped
    }
}

Next steps

Read the OkHi integration best practices document to better manage requesting background location permissions from your users

Last updated