Skip to main content

Listening or Results

You can leverage MetricSDKs internal notification manager or use your own notification handler. Add a NotificationCenter observer to the viewDidLoad() of the view controller that launched your SDK. And pass in your @objc marked function as the selector. Eg. In this case we use handleVerificationOutcome(). You can call deinit but that's not really necessary if you're using Swift 4.2+.

...
import MetricSDK
...
class ViewController: UIViewController {
...
override func viewDidLoad(){
super.viewDidLoad()
...
MetricNotificationManager.shared.addObserver(observer: self,
selector: #selector(handleVerificationOutcome),
name: NotificationKeys.VERIFICATION_COMPLETE)
}
...

@objc func handleVerificationOutcome(_ notification: Notification) {
if let outcome = notification.object as? VerificationOutcome {
switch outcome {
case .success:
//update your UI, etc..
print("Verification successful")
case .failed(let reason):
//update your UI, etc..
print("Session Failed: \(reason)")
default:
break
}
}
}
}

In modern Swift, specifically from Swift 4.2 onwards, you generally do not need to manually remove observers for notifications, as they are automatically deregistered when the object is deallocated.

deinit{
NotificationCenter.default.removeObserver(self, name: NotificationKeys.VERIFICATION_COMPLETE, object: nil)
}