Skip to main content

Overview

This guide shows you how to integrate the MetaMap SDK into your SwiftUI-based iOS application using the MetaMapDelegateObserver wrapper pattern.

Prerequisites

  • MetaMap SDK installed via CocoaPods
  • iOS 13.0 or later
  • Swift 5.7 or later
  • SwiftUI framework

Implementation

Complete SwiftUI Example

Here’s a complete implementation using the MetaMapDelegateObserver pattern:

Understanding MetaMapDelegateObserver

The MetaMapDelegateObserver is a custom UIViewControllerRepresentable wrapper that bridges UIKit’s delegate pattern with SwiftUI’s declarative approach.

Key Components

1. UIViewControllerRepresentable Implementation

The observer conforms to UIViewControllerRepresentable to integrate with SwiftUI:

2. Closure-Based Callbacks

Instead of implementing delegate methods, use closures for handling results:

3. Coordinator Pattern

The Coordinator bridges delegate callbacks to SwiftUI closures:

Using MetaMapDelegateObserver

Basic Usage

Add the MetaMapDelegateObserver to your view hierarchy using a ZStack:

Handling Results

The observer provides two closure parameters:

Success Closure

Cancelled Closure

Launching Verification Flow

Call showMetaMapFlow from any button or action in your SwiftUI view:

Parameters

showMetaMapFlow Parameters

Best Practices

  1. Single Observer Instance: Place the MetaMapDelegateObserver once in your view hierarchy, typically at the top level
  2. ZStack Layout: Use a ZStack to ensure the observer doesn’t affect your UI layout
  3. State Management: Store verification results in @State or @StateObject for reactive UI updates
  4. Error Handling: Always implement both success and cancelled closures

Example with State Management

Next Steps