Installation
This page uses v6.4.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v11.9.0, in the Maps SDK documentation.
Before starting to develop your application with the Maps SDK, you'll need to configure your credentials and add the SDK as a dependency.
Configure credentials
If you plan to install the SDK via direct download, you do not need to configure a secret token. You will still need to configure a public token.
To download and install the SDK, follow the steps below. Start by logging into your Mapbox account, then get your default public token and add it to your project's Info.plist
.
Step 1: Log in/Sign up for a Mapbox account
Login to your Mapbox account at https://account.mapbox.com. If you don't have an account, sign up for free here.
Your account includes a default public access token, and allows you to create a secret access token for use in the installation of the SDK.
Step 2: Configure your public token
Your public access token is used in your application code when requesting Mapbox resources.
To configure your public access token, follow these steps:
- Open your project's
Info.plist
file - Hover over a key and click the plus button
- Type
MBXAccessToken
into the key field - Click the value field and paste in your public access token.
When you need to rotate your access token, you will need to update the token value in your Info.plist
file.
Learn how to keep access tokens private in mobile apps.
Configure location permissions
The Mapbox Maps SDK for iOS requests permissions for user location on your behalf when MGLMapView.showsUserLocation
is set to YES
.
Prior to iOS 14, the device could only send the user’s exact location. With iOS 14, users can opt into Approximate Location. Since users may toggle precise location off when initial permission for their location is requested by the app or in the System settings, developers are strongly encouraged to support Approximate Location.
Handle the User Interface
The Maps SDK provides an approximate user location indicator that mirrors the approximate user location indicator previewed by Apple. This indicator will appear by default when MGLMapView.showsUserLocation
is set to YES
and precise location has been opted out. You may further configure or hide this component.
Request temporary access to precise location
Certain application features may require precise location. The Mapbox Maps SDK for iOS provides a wrapper of Apple’s CoreLocation APIs that requests temporary access to precise location when the user has opted out at the application settings level:
[MGLLocationManager.requestTemporaryFullAccuracyAuthorizationWithPurposeKey:]
Make the following adjustments to your Info.plist file to provide explanations for system prompts that may appear during location prompts:
Provide users a brief explanation of how the app will use their location data for temporary access:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your precise location is used to calculate turn-by-turn directions, show your location on the map, and help improve the map.</string>
Add MGLAccuracyAuthorizationDescription
as an element of the NSLocationTemporaryUsageDescriptionDictionary
dictionary to give users a brief explanation of why a feature in your app requires their exact location:
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict>
<key>MGLAccuracyAuthorizationDescription</key>
<string>Please enable precise location. Turn-by-turn directions only work when precise location data is available.</string>
</dict>
As a convenience, if your application uses a MGLMapView.userTrackingMode
to track user location, the Maps SDK will check for full accuracy authorization and request access on your behalf.
Handle changes in location authorization
At any point, a user may grant or revoke access to precise location in System settings. The Maps SDK for iOS provides a delegate method to handle these changes:
[MGLMapViewDelegate mapView:didChangeLocationManagerAuthorization:]
For more detail, see the example implementation of this delegate method.
After the current session elapses, your users will be prompted to give location permissions the next time they open your app. Your users must enable precise location during this prompt or in the app’s System settings to avoid being asked repeatedly for location accuracy permissions.
Add the dependency
Mapbox provides the Maps SDK via Carthage, CocoaPods , and direct download. You can choose whichever you prefer.
To add the Mapbox Maps SDK dependency with CocoaPods, you will need to configure your build to download the Maps SDK from Mapbox directly. This requires a valid username and an access token with the Downloads: Read
scope. In a previous step, you added these items to your .netrc
file.
- Add the following to your
Podfile
:
use_frameworks!
target 'TargetNameForYourApp' do
pod 'Mapbox-iOS-SDK', '~> 6.4.1'
end
- Run
pod install
to install the dependency.
Add a map
You can add a map to your application in one of three ways: using Swift, Objective-C, or a storyboard. Select one of the following options to learn how to create a map using your preferred method.
The Maps SDK can be used with SwiftUI by wrapping it in a View
conforming to UIViewRepresentable
. Refer to the SwiftUI
documentation
for more details on how to use UIViewRepresentable
.
Insert the following code snippets into your ViewController
.
Run your application and you will see a map on the screen.
Running into problems installing? Read more about common installation issues in our troubleshooting guide.