Skip to main content

Installation

A newer version of the Maps SDK is available

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.2.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

Note

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.

Before installing the SDK, you will need to gather the appropriate credentials. The SDK requires two pieces of sensitive information from your Mapbox account. If you don't have a Mapbox account: sign up and navigate to your Account page. You'll need:

  • A public access token: From your account's tokens page, you can either copy your default public token or click the Create a token button to create a new public token.
  • A secret access token with the Downloads:Read scope.
    1. From your account's tokens page, click the Create a token button.
    2. From the token creation page, give your token a name and make sure the box next to the Downloads:Read scope is checked.
    3. Click the Create token button at the bottom of the page to create your token.
    4. The token you've created is a secret token, which means you will only have one opportunity to copy it somewhere secure.

You should not expose these access tokens in publicly-accessible source code where unauthorized users might find them. Instead, you should store them somewhere safe on your computer and make sure they're only added when your app is compiled. Once this configuration step has been completed, you will be able to reference your credentials in other parts of your app.

Configure your secret token

Your secret token enables you to download the SDK directly from Mapbox. 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. Depending on your environment, you may have this file already, so check first before creating a new one.

The .netrc file is a plain text file that is used in certain development environments to store credentials used to access remote servers. The login should always be mapbox. It should not be your personal username used to create the secret token. 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 YOUR_SECRET_MAPBOX_ACCESS_TOKEN

Configure your public token

To configure your public access token, you will need to add a MGLMapboxAccessToken key to Info.plist and set the value to your public access token.

For projects created in Xcode 13 and higher, Info.plist is automatically generated. Configure your public access token by clicking on the project name in the project navigator to open the project editor, selecting a target, switching to the Info pane, and adding MGLMapboxAccessToken and your public access token as a new key-value pair in Custom iOS Target Properties.

If you ever need to rotate your access token, you will need to update the token value in your Info.plist or Info pane file.

guide
Access token best practices

Learn how to keep access tokens private in mobile apps.

chevron-right

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.

  1. Add the following to your Podfile:

    use_frameworks!
    
    target 'TargetNameForYourApp' do
      pod 'Mapbox-iOS-SDK', '~> 6.4.1'
    end
    
  2. 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.

Note

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.

 
import Mapbox
 

 
class ViewController: UIViewController {
 
override func viewDidLoad() {
 
super.viewDidLoad()
 

 
let url = URL(string: "mapbox://styles/mapbox/streets-v12")
 
let mapView = MGLMapView(frame: view.bounds, styleURL: url)
 
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
 
mapView.setCenter(CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06), zoomLevel: 9, animated: false)
 
view.addSubview(mapView)
 
}
 
}

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.