Tutorial: Pre-create inquiries for iOS
Tutorial: Pre-create inquiries for iOS
Tutorial: Pre-create inquiries for iOS
The Persona iOS SDK lets you integrate identity verification directly into your native iOS app.
There are two ways to use the iOS SDK:
This guide walks you through the second method: pre-creating inquiries via API. This is the method we recommend you use in production.
You will:
The sample code in this guide illustrates an approach that we recommend in production.
However, for demonstration purposes, the code itself is simplified and not production-ready. For example, it does not include:
Pre-creating inquiries (the method shown in this guide) is recommended for production use. However, if you’re looking for the fastest way to test the iOS SDK, see the inquiry template approach.
You’ll need:
Before you start, you should:
A user named Alexander Sample just joined your dog walking app as a dog walker. You want to verify his identity to ensure the safety of users on your service.
Alexander’s user ID in your app is “usr_ABC123”. During account signup in your app, he stated his birthdate is August 31, 1977.
Every inquiry is created from an inquiry template, which defines details like the specific verification logic and UI text and branding of that inquiry. You can think of inquiry templates as a mold that lets you create many inquiries.
Persona offers a suite of solutions that include preconfigured inquiry templates. In this tutorial, use the “KYC” solution to verify your dog walkers.
Follow these instructions to add the “KYC” solution to your Sandbox environment.
Find the ID of the newly-created inquiry template.
In the Persona dashboard, navigate to Inquiries > Templates. Find the “KYC” template in the list of inquiry templates, and note the value in the ID field. The value should begin with itmpl_.
Create a backend server with one endpoint that returns an inquiry for the current logged-in user. If the user has an in-progress inquiry, the backend will return that inquiry’s ID. Otherwise, the backend will create a new inquiry and return its ID.
This is the same backend from the Embedded Flow Pre-create guide.
server.py with the following code:In this code, replace itmpl_XXXXXXXXXXXXX with your inquiry template ID from Step 2.
requirements.txt file:You should see:
Keep this terminal window open.
The backend is identical to the Embedded Flow Pre-create guide. See that guide’s explanation for details on:
In the next steps, you will construct the iOS app. Here’s how the pieces fit together:
View Model:
Views:
UX flow:
Next, you’ll create the app and build the view model first, then the views.
Create a new iOS app that will display the Persona verification flow.
Open Xcode and create a new iOS App project:
PawsonaInstall the Persona iOS SDK using Swift Package Manager:
https://github.com/persona-id/inquiry-ios-2Create a new Swift file called OnboardingViewModel.swift:
This view model handles the following tasks:
Fetching inquiry from backend: Your iOS app fetches an inquiry from your backend. This is the key part of the “Pre-create” approach:
Handling callbacks: The view model handles callbacks from the Persona SDK:
handleCompletion: Called when verification finishes successfullyhandleCancellation: Called when user cancels the flowhandleEvent: Called when the flow starts or progresses to a new pagehandleError: Called when an error occursThese callbacks let you coordinate your app’s UI with changes in the Persona UI. Do not rely on them for up-to-date data about the state of the inquiry. Use webhooks (Step 11) for logic that depends on inquiry state.
Storing and updating state: The view model updates its state based on the Persona callbacks. This state is consumed by the views in our app:
Now, create the onboarding screen that Alexander sees before he starts verification:
When you created the iOS app, a ContentView was generated. Replace the contents of ContentView.swift with the following code:
Then create OnboardingView.swift:
Note the following features of OnboardingView:
“Start Verifying” button: OnboardingView contains a “Start Verifying” button. When tapped, it triggers the view model to initiate the Persona verification flow:
Persona flow presentation: After the view model gets an inquiry from the backend, it sets showPersonaFlow to true. The Persona flow is then presented:
The Persona SDK provides the Inquiry class, which has a start method that starts the verification flow as a modal:
As you can see from the method above, the Inquiry needs to be presented from a UIKit UIViewController. In SwiftUI, we must wrap a UIViewController within a UIViewControllerRepresentable. You’ll create this now.
Create PersonaFlowPresenter.swift with the following code:
This wrapper integrates the Persona iOS SDK with SwiftUI. Note the following features:
Uses a pre-created inquiry: The SDK is initialized with inquiryId instead of a template ID. The session token is non-nil only when resuming pending inquiries:
Note that the code here doesn’t prefill user data or set a reference ID on the inquiry, because the backend already did that.
Implements an InquiryDelegate: The Persona SDK requires a delegate object that conforms to InquiryDelegate to receive completion, cancellation, event, and error callbacks. In PersonaFlowPresenter, the internal Coordinator class is this delegate:
The final piece of iOS app code is the simple screen that’s shown after Alexander finishes the Persona verification:
Create a new Swift file called FinishedView.swift:
The Persona SDK requires a few permissions you must prompt the user for.
In your app settings, go to Targets > Pawsona > Info.
Edit “Custom iOS Target Properties” to include the following settings:
There are a few settings needed in this demo app. These settings are NOT needed for a production app.
Allow your iOS app to talk to your local backend. In the “Custom iOS Target Properties” (which you edited in the previous step), add the following setting:
If you’re testing on a physical device (e.g. a real iPhone), you need to update the backend URL:
Find your computer’s IP address:
192.168.1.100)In OnboardingViewModel.swift, update the backendURL:
Make sure your iPhone and computer are on the same Wi-Fi network.
You can receive notifications when any inquiry’s state changes. For example, you can be alerted when any inquiry is started by a user, or when any inquiry is completed. See the full list of inquiry events you can be alerted about.
To receive automatic notifications:
inquiry.started, inquiry.completed, inquiry.approved, inquiry.declined, and inquiry.failedFor this tutorial, you can skip webhooks and view results in the dashboard.
Build and run the app in Xcode. Then test the inquiry creation and resumption logic with different scenarios. When doing these manual tests:
Scenario 1: Create new inquiry
No incomplete inquiry found, creating new oneScenario 2: Resume created inquiry
Found existing inquiry inq_XXXXXXXXXXXXX with status: createdInquiry is created, resuming without session tokenScenario 3: Resume pending inquiry
Found existing inquiry inq_XXXXXXXXXXXXX with status: pendingInquiry is pending, generating session token to resumeScenario 4: Create new inquiry after completion
No incomplete inquiry found, creating new oneIf you set up the webhook in Step 11, check your server logs. You should see events from inquiry.started, inquiry.completed, and inquiry.approved.
Note: If you want to receive the inquiry.failed event, open the verification flow again and select “Fail verification” in the debug menu (the pink config menu on screen). Then click through the verification flow.
In the Persona dashboard:
usr_ABC123)You should see two inquiries if you tested all four scenarios above. Click on each to see their status and details. Note that because this inquiry was created in Sandbox, some of the data shown will be demo data.
You can also retrieve inquiry details via API. You’ll need the inquiry ID you see printed in the “Debug info” section of the UI. See Retrieve an Inquiry.
You have built a backend API that:
You also:
This is a complete example of how you can pre-create inquiries for a verification flow created with the Persona iOS SDK.
Enhance this integration:
Explore further: