SDK for iOS Developer's Guide

Create a Simple HERE SDK App Using Swift

This tutorial provides instructions on how to create a simple application using Swift programming language. It is equivalent to the Objective-C tutorial, which is located at Create a Simple App Using HERE SDK.

Development tasks for this basic application include:
  • Create a new Xcode project
  • Add necessary resources and a map view to the project
  • Acquire credentials from HERE for accessing map services
  • Initialize the map view so that a map instance is created for rendering on the client device
The contents of this guide apply to Xcode 13.0 and the iOS 16 SDK.

Sample Project in SDK for iOS

A copy of the Xcode project described in this tutorial is available in sample-apps folder in your SDK for iOS package. To run the project, double-click on SwiftHelloMap.xcodeproj and follow the instructions in README.txt file.

Create a New Single View Application

  1. From Xcode menu, select File > New > Project to open the New project dialog (or press Shift + Command + N).
  2. Select iOS > Application > Single View Application as the application type you want to create. Press Next.
  3. In the next dialog, enter your Product Name (such as HelloMap) and Organization Identifier (such as edu.self).
  4. Choose "Swift" under Language, then click Next.
  5. Navigate to the directory where you want your project to be stored and then select Create.
  6. The next step is to configure this project to use HERE SDK.

Configure the Application

  1. Extract the SDK for iOS archive to somewhere in your local file system.
  2. Add the NMAKit.xcframework to your Xcode project. Click on your app target and choose the General tab. Find the section called Frameworks, Libraries, and Embedded Content, click the plus (+) sign, click the "Add Other" and then click Add Files. From the file dialog box select NMAKit.xcframework folder and then click Finish.
    Figure 1. Adding NMAKit.xcframework
  3. Ensure that NMAKit.xcframework appears in the Frameworks, Libraries, and Embedded Content section.
    Figure 2. Added Frameworks
  4. Run the application. From the Xcode menu bar, select Product > Run. Ensure that the project runs in the iOS Simulator without errors.
  5. SDK for iOS is now ready for use in your Xcode project. Now that you have your project configured to work with HERE SDK, try extending the sample application to render a map.

Create the Map View

In this section, we utilize NMAMapView and NMAGeoCoordinates classes to render a Map.

  1. Create an NMAMapView.

    1. Select Main.storyboard in the navigator, then open the Utilities view by pressing the key combination Command + Option + Control + 3. Drag and drop a View object from the Object Library onto the View Controller. If necessary, resize the View so it takes up the entire viewable area.
    2. In the Interface Builder click on the created View and then open the Identity Inspector in the Utilities view by pressing the key combination Command + Option + 3. Change the class value from UIView to NMAMapView and press return. In the Document Outline you should see that the name of the View has changed from View to Map View.
    Figure 3. MapView
  2. Create an outlet for NMAMapView in ViewController.

    1. Select Main.storyboard in the navigator.
    2. Press Command + Option + Return to open the Assistant Editor. It should show ViewController.swift.
    3. Add the following import statement to the top of this file:
      import NMAKit
    4. Hold the Control key on the keyboard and click to drag from the Map View to the interface block in ViewController.swift. You should see a blue line and tooltip which says "Insert Outlet or Outlet Connection". Release the mouse button and a dialog appears allowing you to create an outlet.
    5. Name the outlet mapView, keep the other default options and then select Connect.

    Figure 4. Create an Outlet
  3. Now an outlet to NMAMapView is set. The modified file should be as follows:

    
    import UIKit
    import NMAKit
    
    class ViewController: UIViewController {
    
      @IBOutlet weak var mapView: NMAMapView!
    
      override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      }
    
      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
      }
    
    }
    
  4. Implement NMAMapView setup and lifecycle code by replacing viewDidLoad() function with viewWillAppear(animated) and addMapCircle():
    
    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      mapView.useHighResolutionMap = true
      mapView.zoomLevel = 13.2
      mapView.set(geoCenter: NMAGeoCoordinates(latitude: 49.258867, longitude: -123.008046),
        animation: .linear)
      // Note, logo is not shown when its size is greater than 1/8 of NMAMapView's height or width.
      mapView.copyrightLogoPosition = NMALayoutPosition.bottomCenter
      addMapCircle()
    }
    
    func addMapCircle() {
      if mapCircle == nil {
        let coordinates: NMAGeoCoordinates =
          NMAGeoCoordinates(latitude: 49.258867, longitude: -123.008046)
        mapCircle = NMAMapCircle(coordinates: coordinates, radius: 50)
        mapView.add(mapCircle!)
      }
    }
    
  5. Add your HERE application credentials.
    1. Open AppDelegate.swift file and import NMAKit by adding the following import statement to the top of the file.
      import NMAKit
    2. Add the following in didFinishLaunchingWithOptions function replacing YOUR_APP_ID, YOUR_APP_CODE and YOUR_LICENSE_KEY with the credentials that you received from your https://developer.here.com.
      NMAApplicationContext.setAppId("YOUR_APP_ID",
          appCode: "YOUR_APP_CODE",
          licenseKey: "YOUR_LICENSE_KEY")
    Note: didFinishLaunchingWithOptions might not be the best place to setup your HERE SDK credentials. For example, if your initial view controller is in a Storyboard and instantiates its HERE SDK properties on declaration. In this case, instantiation will occur before didFinishLaunchingWithOptions, resulting in undefined HERE SDK behavior. To prevent this, assign a credentials setup to a property of your AppDelegate.swift.
  6. Build and run the application. If the build is successful, you now have an application that displays a map similar to the following screenshot and allows you to manipulate it using gestures.

    Figure 5. Running the App Running the App
Note: For the available authentication options, see the Identity & Access Management Developer Guide.