Beta
Maps SDK for iOS v10

Installation

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. In order 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 <INSERT SECRET API TOKEN>

Configure your public token

To configure your public access token, open your project's Info.plist file and add a MGLMapboxAccessToken key whose value is your public access token.

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

guide
Access token best practices

Learn how to keep access tokens private in mobile apps.

Add the dependency

Mapbox provides the Maps SDK via Swift Package Manager, CocoaPods and direct download. You can choose whichever you prefer.

Mapbox Maps can be consumed via Swift Package Manager (SPM). To add the Mapbox Maps SDK with SPM, you will need to configure your environment to download it from Mapbox. This requires a Mapbox access token with the Downloads:Read scope. In a previous step, you added this token to your .netrc file.

  1. Go to the project file and select swift packages. Then press the "+" button to add a new package.
  2. Insert https://github.com/mapbox/mapbox-maps-ios.git as the URL and pull in the package.
  3. Select the exact version option and insert the desired version.
  4. At this point, everything should be fetched and loaded up. Select the "MapboxMaps" library and then press finish.
  5. In your code, you can now import MapboxMaps as well as any of the other packages that were downloaded as dependencies.

Notes

  • If you need to update your packages, you can click on File > Swift Packages > Update To Latest Package Versions
  • Sometimes, artifacts cannot be resolved or errors can occur, in this case select File > Swift Packages > Reset Package Cache
  • If your Xcode crashes, delete your derived data folder

Configure location permissions

The Mapbox Maps SDK for iOS requests permissions for user location on your behalf when mapOptions.location.showUserLocation is set to true.

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 mapOptions.location.showUserLocation is set to true 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:

mapView.locationManager.requestTemporaryFullAccuracyPermissions(withPurposeKey: "customKey")

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 LocationAccuracyAuthorizationDescription 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>LocationAccuracyAuthorizationDescription</key>
    <string>Please enable precise location. Turn-by-turn directions only work when precise location data is available.</string>
</dict>

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's LocationPermissionsDelegate protocol has a set of delegate methods that will be called when the user makes changes to location permissions.

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 a map

Insert the following code snippets into your ViewController.

import UIKit
import MapboxMaps
class ViewController: UIViewController {
internal var mapView: MapView!
override public func viewDidLoad() {
super.viewDidLoad()
let myResourceOptions = ResourceOptions(accessToken: "your_public_access_token")
mapView = MapView(with: view.bounds, resourceOptions: myResourceOptions)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.update { (mapOptions) in
mapOptions.ornaments.showsScale = true
}
self.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.