OkCollect iOS

The OkCollect iOS framework enables you to launch OkHi from your app and collect accurate addresses from your users.

Getting Started

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

Installation

Add Swift Package dependency

https://github.com/OkHi/okcollect-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)
    return true
}

Start creating addresses

import UIKit
import OkCore
import OkCollect

class ViewController: UIViewController {

    private let okCollect = OkHiCollect() // initialise the OkCollect class
    private let okhiLocationService = OkHiLocationService() // optionally use the OkHiLocationService to manage location permissions
    
    override func viewDidLoad() {
        super.viewDidLoad()
        okCollect.delegate = self // set your view controller as an OkCollect delegate
        okhiLocationService.delegate = self // set your view controller as an OkHiLocationService delegate
    }
    
    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)
    }
    
    @IBAction func onButtonPress(_ sender: UIButton) {
        if !okhiLocationService.isLocationServicesAvailable() { // check whether location services is turned on
            showMessage(title: "Turn on location services", message: "Location services are required to create and verify an address.")
            return
        }
        if okhiLocationService.isLocationPermissionGranted() { // check for location permission
            launchOkCollect()
        } else {
            okhiLocationService.requestLocationPermission(withBackgroundLocationPermission: true) // requests for background location permission if you're going to use OkVerify, set false if not
        }       
    }
    
    func launchOkCollect() {
        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)
    }
    
}

extension ViewController: OkCollectWebviewDelegate {
    func collect(didEncounterError error: OkHiError) {
        showMessage(title: "Error: \(error.code)", message: error.message)
    }
    
    func collect(didSelectAddress user: OkHiUser, location: OkHiLocation) {
        // your magic goes here, user has created an address ✨
    }
}

extension ViewController: OkHiLocationServiceDelegate {
    func okHiLocationService(locationService: OkHiLocationService, didChangeLocationPermissionStatus locationPermissionType: LocationPermissionType, result: Bool) {
        if locationPermissionType == .always && result { // check whether location permission was granted
            launchOkCollect()
        }
    }
}

Check out our full integration example

Whenever a user's OkHi address is created or used an SMS is sent to them notifying them of this usage

Whereas OkCollect does provide ways to customise the look and feel of the address creation process as well as alter some of the in built functionality. Due to accessibility concerns, the product will work with select brand colours that provide enough contrast to the text being rendered.

Last updated