Overview
This guide shows you how to integrate the MetaMap SDK into your SwiftUI-based iOS application using theMetaMapDelegateObserver 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 theMetaMapDelegateObserver pattern:
Understanding MetaMapDelegateObserver
TheMetaMapDelegateObserver is a custom UIViewControllerRepresentable wrapper that bridges UIKit’s delegate pattern with SwiftUI’s declarative approach.
Key Components
1. UIViewControllerRepresentable Implementation
The observer conforms toUIViewControllerRepresentable 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 theMetaMapDelegateObserver to your view hierarchy using a ZStack:
Handling Results
The observer provides two closure parameters:Success Closure
Cancelled Closure
Launching Verification Flow
CallshowMetaMapFlow from any button or action in your SwiftUI view:
Parameters
showMetaMapFlow Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
clientId | String | Yes | Your MetaMap client ID |
flowId | String | Yes | The verification flow ID |
metadata | Dictionary | No | Additional metadata and customization options |
Best Practices
- Single Observer Instance: Place the
MetaMapDelegateObserveronce in your view hierarchy, typically at the top level - ZStack Layout: Use a
ZStackto ensure the observer doesn’t affect your UI layout - State Management: Store verification results in
@Stateor@StateObjectfor reactive UI updates - Error Handling: Always implement both success and cancelled closures
Example with State Management
Next Steps
- Learn about UI Customization options
- Explore UIKit Integration
- See Objective-C Integration