HERE iOS SDK Developer's Guide

Creating a Simple HERE SDK Application Using Swift

This tutorial provides instructions on how to create a simple application using the Swift programming language. It is equivalent to the Objective-C tutorial, which is located at Creating a Simple Application Using the 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 such that a map instance is created for rendering on the client device
The contents of this guide apply to Xcode 9 and the iOS 11 SDK.

Sample Project in the HERE iOS SDK

A copy of the Xcode project described in this tutorial is available in the sample-apps folder in your HERE iOS SDK package. To run the project, double-click on SwiftHelloMap.xcodeproj and follow the instructions in the 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 the HERE SDK.

Configure the Application

  1. Extract the HERE iOS SDK archive to somewhere in your local file system.
  2. Add the NMAKit framework to your Xcode project. To add the NMAKit framework to your Xcode project, click on your app target and choose the "General" tab. Find the section called "Embedded Binaries", click the plus (+) sign, and then click the "Add Other" button. From the file dialog box, select the "NMAKit.framework" folder. Ensure that the "Copy items if needed" and "Create folder reference" options are selected, then click Finish.
    Figure 1. Add File to Target
  3. Ensure that NMAKit.framework appears in the "Embedded Binaries" and the "Linked Frameworks and Libraries" sections.
    Figure 2. Embedded Binaries
  4. Run the application. From the Xcode menu bar, select Product > Run. Ensure that the project runs in the iOS Simulator without errors.
  5. The HERE iOS SDK is now ready for use in your Xcode project. Now that you have your project configured to work with the HERE SDK, try extending the sample application to render a map.

Create the Map View

In this section, we utilize the 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 to View to Map View.
    Figure 3. MapView
  2. Create an outlet to 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 the 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)
      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 the 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 the application(_:didFinishLaunchingWithOptions) function, replacing YOUR_APP_ID and YOUR_APP_CODE with the credentials that you received from your http://developer.here.com.
      NMAApplicationContext.set(appId: "YOUR_APP_ID",
          appCode: "YOUR_APP_CODE")
  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