I had posted how to add OneMap into your iOS application in previous post. In this tutorial we will try one of the OneMap REST APIs in Swift Playgrounds and see the return results. Xcode Playgrounds is perfect for experimenting with new APIs interactively. We are going to use Address Search API from OneMap.
This allows searching of address data for a given search value. This also allows support of commands such as AND, OR, including word (+), excluding word (-), and exact phrase search using quotation marks. It will return search results with X,Y coordinates of the searched location. http://www.onemap.sg/api/help/
First create a new Playgrounds file in xcode. Then import XCPlayground module. After that call XCPSetExecutionShouldContinueIndefinitely and set it true so that execution will continue and we can see the return result from asynchronous REST API calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCPlayground | |
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) |
Next, we are going to use NSURLSession to make the REST API call to OneMap service. I use the example URL available in their api help page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) | |
let urlString = "http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&wc=SEARCHVAL%20LIKE%20%27CITY$%27&otptFlds=CATEGORY&returnGeom=0&nohaxr=10" | |
After that we pass the url string to dataTaskWithURL function of the session object and implement a completion handler function to tackle the return result from async call.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
session.dataTaskWithURL(NSURL(string: urlString)!, completionHandler: { | |
(taskData, taskResponse, taskError) -> Void in | |
var jsonReadError: NSError? | |
var jsonResult = NSJSONSerialization.JSONObjectWithData(taskData, options: NSJSONReadingOptions.MutableContainers, error: &jsonReadError) as NSDictionary | |
for (key, value) in jsonResult{ | |
println("\(key) : \(value)") | |
} | |
let arr = jsonResult["SearchResults"] as NSArray | |
println("\(arr[0])") | |
println("\(arr[1])") | |
println("\(arr[2])") | |
println("\(arr[3])") | |
println("\(arr[4])") | |
let categ = arr[1]["CATEGORY"] | |
let searchval = arr[1]["SEARCHVAL"] | |
let xval = arr[1]["X"] | |
let yval = arr[1]["Y"] | |
println("\(jsonReadError)") | |
println("\(taskData)") | |
println("\(taskResponse)") | |
}).resume() |
We can convert the result JSON data using NSJSONSerialization.JSONObjectWithData method and cast it as NSDationary. And then we can write println statement on the elements of NSDationary result. The values of the result will be shown interactively at the right column of Playground. We can change the parameter values in the url string and see the result changes interactively.
We will use this API call in the next post, adding OneMap Search Function in iOS App.