Show nearby places on a map in a flow
Follow this recipe to include an interactive map in a flow. Your app renders the map as a native MapKit view in a custom element. Your app requests location access on the flow screen.
This recipe only covers iOS apps. Apps built with other frameworks only display the custom elements’ fallback content. Support for Android and cross-platform frameworks is upcoming.
Before you start
- Update your app to Adapty iOS SDK 4.4 or later. Earlier SDK versions show the fallback content for every custom element.
- Set up your app to fetch and display flows.
- To ask users for location access, add the
NSLocationWhenInUseUsageDescriptionkey to theInfo.plistfile of your app. Set its value to the text of the prompt, such as “Find fitness centers near you.”
1. Add the custom element to the screen
In the Flow & Paywall Builder, add a custom element to a flow screen, and set it up for the map:
- Type:
map. - ID:
fitness_centers. - Height: Keep the default Fixed height. The map fills the custom element.
Keep the custom element out of containers that respond to touch, such as a carousel. Otherwise, a drag on the map also moves the carousel.
Inside the custom element, add an Image with a picture of a map. Users see it as the fallback when:
- Their version of your app doesn’t display the map.
- They denied location access before the screen opened.
- Their device runs iOS 16.
If the user hasn’t answered the location access prompt yet, iOS shows it when the map appears. To tell users why your app needs their location, add a text element above the custom element, such as “Find fitness centers near you.”
2. Create the map view
Create a model for location access, and a SwiftUI view with a map. The view requests location access, finds fitness centers within 2 km of the user, and displays a marker for each one:
The map view needs iOS 17 or later. On iOS 16, users see the fallback. If the user denies location access on the screen, the view replaces the map with a message about how to allow access in Settings.
3. Match the map to the custom element
In your app-wide resolver, add a property for the LocationAccess model. Then add a case that returns the map view for the map Type and the fitness_centers ID. The highlighted lines show what to add. If your app has no app-wide resolver yet, copy the whole code:
The SDK calls the resolver when the screen opens, and again while it’s open, such as when products load. Location access determines what the resolver returns on each call:
- Not answered yet: The map view, so that the view can request access.
- Allowed: The map view, which shows fitness centers near the user.
- Denied before the screen opened:
nil, so users see the fallback instead of the message about Settings. - Denied on the screen: The map view, because the view sets
isMapOnScreenwhile it’s on the screen. If the resolver returnednilhere, the SDK would replace the message about Settings with the fallback at the next call.
If your app doesn’t set the app-wide resolver yet, call AdaptyUI.setCustomElementsResolver(_:) once, such as at app launch:
4. Test on a device
The Flow & Paywall Builder can’t run your app code, so its canvas only shows the fallback. To preview the map:
- Publish the flow. To see the latest published version instead of a cached one, register the device as a test device.
- Run a build of your app that includes the resolver, on iOS 17 or later. Open the screen, and allow location access. Verify that the map shows your location and markers for fitness centers near you.
To test the fallback, deny location access: in the Settings app, go to Privacy & Security > Location Services > your app, and select Never. Then open the screen again.
To find out why the fallback appears instead of the map, follow the instructions in the Test on a device section of the iOS guide.