jjackson-esristaff

Getting Started with Swift and ArcGIS Runtime SDK for iOS

Blog Post created by jjackson-esristaff Employee on Jul 22, 2014

Those of us in Esri that build apps for iOS and OS X are pretty excited about Apple's new programming language. Like you, we are trying our best to stay on top of the moving target that is Swift. I figure I can share some of our experiences to the wider ArcGIS developer community and hopefully spark some good discussion along the way.

 

Let's start out with a very simple tutorial that will get you started with an iOS app that uses Swift and ArcGIS. I'll follow up with some other posts which dive into more technical depth.

 

Augment the SDK

The ArcGIS SDK is written in Objective-C, but it works pretty well with Swift as is. In order to use it, the simplest thing you can do is add a module definition file to your SDK installation. The file should look like this:

 

framework module ArcGIS {

  umbrella header "ArcGIS.h"

  export *

  module * { export * }

}

 

The file should be named module.modulemap and it needs to be placed in a folder called Modules inside the ArcGIS.framework folder. If you installed the SDK in the default location that would be ~/Library/SDKs/ArcGIS/iOS/ArcGIS.framework/Modules. Note that you'll need to create the Modules folder.

 

You'll only need to create that file once and it will work for any Swift projects you create going forward.

 

Create a Swift Project

Start Xcode (I'm currently using Beta-4) and create a new iOS Application project. Choose the Single View Application.

 

In the project settings, add these values so that Xcode can find the ArcGIS SDK:

 

Other Linker Flags: -framework ArcGIS -ObjC -l c++

Framework Search Paths: ~/Library/SDKs/ArcGIS/iOS

 

Add a Map View

In the Main.storyboard add a view.

 

Size this view to fill its superview, and then set then add four constraints to pin the new view to the superview.

 

Show the identity inspector and change the class of the new view to be AGSMapView. Now open ViewController.swift in the assistant editor and create an outlet for the map view (Control-drag from the storyboard to the ViewController class.) If you did everything right Xcode should create a var that looks like this:

 

@IBOutlet weak var mapView: AGSMapView!

 

To fix the warning, change the import statement from UIKit to ArcGIS.

 

Now, to display some data in the map view, make the following changes to viewDidLoad():

 

    override func viewDidLoad() {

        super.viewDidLoad()

      

        let layer = AGSTiledMapServiceLayer(URL: NSURL(string: "http://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer"))

        mapView.addMapLayer(layer)

 

        let center = AGSPoint.pointWithX(-7822565, y: 5413016, spatialReference: nil)

        mapView.zoomToScale(72112, withCenterPoint:center, animated: true)

    }

 

 

The last step is to build and run your app. That's it. You're living the Swift lifestyle now.

Outcomes