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
importOkCoreimportOkCollect 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!returntrue}
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
importUIKitimportOkCoreimportOkCollectclassViewController:UIViewController {privatelet okCollect =OkHiCollect()// initialise the OkCollect class private let okhiLocationService = OkHiLocationService() // optionally use the OkHiLocationService to manage location permissions
privatevar 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
overridefuncviewDidLoad() { super.viewDidLoad() 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 }funcshowMessage(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) }@IBActionfunconButtonPress(_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 permissionlaunchOkCollect() } else { okhiLocationService.requestLocationPermission(withBackgroundLocationPermission: true) // requests for background location permission if you're going to use OkVerify, set false if not
} }funclaunchOkCollect() { let okHiTheme = OkHiTheme().with(logoUrl: "https://cdn.okhi.co/icon.png").with(appBarColor: "#ba0c2f").with(appName: "OkHi")
let okHiConfig =OkHiConfig().enableStreetView().enableAppBar()guardlet vc = okCollect.viewController(with: okHiUser, okHiTheme: okHiTheme, okHiConfig: okHiConfig)else {return } self.present(vc, animated:true, completion:nil) }funcstartAddressVerification(location: OkHiLocation) { okVerify?.start(location: location)// start the address verification process }funcstopAddressVerification (locationId: string) { okVerify?.stop(locationId: locationId)// okVerify?.stopAll() this will stop all addresses from being verified }}extensionViewController:OkCollectWebviewDelegate {funccollect(didEncounterErrorerror: OkHiError) {showMessage(title:"Error: \(error.code)", message: error.message) }funccollect(didSelectAddressuser: OkHiUser, location: OkHiLocation) {startAddressVerification(location: location) }}extensionViewController:OkHiLocationServiceDelegate { func okHiLocationService(locationService: OkHiLocationService, didChangeLocationPermissionStatus locationPermissionType: LocationPermissionType, result: Bool) {
if locationPermissionType == .always && result { // check whether location permission was grantedlaunchOkCollect() } }}extensionViewController:OkVerifyDelegate {funcverify(_okVerify: OkHiVerify, didEncounterErrorerror: OkHiError) {showMessage(title:"Error: \(error.code)", message: error.message) }funcverify(_okVerify: OkHiVerify, didStartlocationId: String) {showMessage(title:"Verification started", message:"Address ID: \(locationId)") }funcverify(_okVerify: OkHiVerify, didEndlocationId: String) {showMessage(title:"Verification stopped", message:"Address ID: \(locationId)") }}
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
importUIKitimportOkCoreimportOkVerifyclassViewController: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
privatevar 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
overridefuncviewDidLoad() { super.viewDidLoad() okVerify =OkHiVerify(user: okHiUser) okVerify?.delegate = self }@IBActionfunconButtonPress(_sender: UIButton) {if okhiLocationService.isLocationPermissionGranted()&& okhiLocationService.isLocationServicesAvailable() {startAddressVerification() } }funcstartAddressVerification() { okVerify?.start(location: okhiLocation) }}extensionViewController:OkVerifyDelegate {funcverify(_okVerify: OkHiVerify, didEncounterErrorerror: OkHiError) {// handle errors }funcverify(_okVerify: OkHiVerify, didStartlocationId: String) {// verification started successfullyprint(locationId) }funcverify(_okVerify: OkHiVerify, didEndlocationId: String) {// verification stopped }}
Next steps
Read the OkHi integration best practices document to better manage requesting background location permissions from your users