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.7.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 iOS SDK, follow the steps below. Start by logging into your Mapbox account, then create and configure a secret access token, and lastly add your public access token to your Info.plist
.
Step 1: Log in/Sign up for a Mapbox account
If you haven't done so already, sign up for a Mapbox account and log into it.
You can sign up or sign in by clicking the buttons in the top right corner of your browser.
This account will allow you to create tokens and will create a default public token for you upon creation.
Step 2: Create a secret token
A secret token can enable access to various products/services at Mapbox, including the ability to download an SDK. To allow download access to an SDK, follow these steps:
- From your account's tokens page, click the Create a token button.
- From the token creation page, give your token a name and make sure the box next to the
Downloads:Read
scope is checked. - Click the Create token button at the bottom of the page to create your token.
- The token you've created is a secret token, which means you will only have one opportunity to copy it somewhere secure.
Store secret tokens in secure a location, outside of your project folder to make sure unauthorized users cannot find it.
Step 3: Configure your secret token
To use your secret token, you will need to store it in a .netrc
file in your home directory (not your project folder). This approach helps avoid accidentally exposing your secret token by keeping it out of your application's source code.
To create a .netrc file follow these steps:
Step 3-1: Check if you have already created a .netrc
file on your computer.
- Go to your home directory.
- Type
file .netrc
into your terminal and see if anything returns. - If you have a
.netrc
file already, skip step 3-2.
Step 3-2: Create the .netrc file.
- Open up your terminal in your home directory.
- Type
touch .netrc
.
Step 3-3: Open the .netrc
file.
- Type
open .netrc
from the same terminal window from Step 3-2
Step 3-4: Add login instructions to the .netrc
To set up the credentials required to download the SDK, add the following entry to your .netrc
file:
machine api.mapbox.com
login mapbox
password <INSERT SECRET ACCESS TOKEN>
Step 3-5: Set .netrc
file permissions
- Go to your home directory:
/Users/[CurrentUser]
.- If you do not see the
.netrc
file, pressCommand
+Shift
+.
(the period key).
- If you do not see the
- Right click on the
.netrc
. - Click
Get Info
. - Scroll down to
Sharing & Permissions
. - Under your current username, make sure to set
Privilege
toRead & Write
.
Step 4: Configure your public token
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.