mwoods
mwoods

Reputation: 317

Running CoreLocation in Swift Playground Instance

I was trying to prototype a basic class for handling getting Geo Coord for my app and wanted to run it in a Swift Playground instance, but the delegate doesnt seem to get called.

Below is the code. Any advice RE: debugging would be helpful.

Is it even possible to run in the IDE.

// Playground - noun: a place where people can play

import UIKit
import CoreLocation


var str = "Hello, playground"


class GeoCordDelegate: NSObject, CLLocationManagerDelegate {

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println("Updated Location")
        println(locationMgr.location.coordinate.latitude)
        println(locationMgr.location.coordinate.longitude)
    }


    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("Error while updating location " + error.localizedDescription)
    }

}


let locationMgr = CLLocationManager()

locationMgr.delegate = GeoCordDelegate()
locationMgr.desiredAccuracy = kCLLocationAccuracyBest
locationMgr.startUpdatingLocation()

Upvotes: 2

Views: 2030

Answers (3)

David Berry
David Berry

Reputation: 41226

Since CoreLocation runs asynchronously, you need to enable asynchronous operation in your playground.

Try:

import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

Upvotes: 1

Dipu Rajak
Dipu Rajak

Reputation: 693

For Xcode 9

import CoreLocation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true 

// Address to Geocode
let address = "Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France"

print("Starting Geocoding")
CLGeocoder().geocodeAddressString(address) { placemarks, error in
  let coordinate = placemarks!.first!.location!.coordinate
  print("Completed Geocoding, Latitude: \(coordinate.latitude), Longitude: \(coordinate.longitude)")
  PlaygroundPage.current.finishExecution() 
}

print("End of Playground")

Upvotes: 2

Nilloc
Nilloc

Reputation: 875

Incase anyone still wants to do this, here's an example that worked for me in Xcode 7.1 with up-to-date APIs:

//: Playground - noun: a place where people can play

import Cocoa
import XCPlayground
import CoreLocation

var currentPage = XCPlayground.XCPlaygroundPage.currentPage
currentPage.needsIndefiniteExecution = true // Sets up the constant running of the playground so that the location can update properly


let locationMgr = CLLocationManager()

class GeoCordDelegate: NSObject, CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
        print("Updated Location: " + newLocation.description)
    }
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error while updating location " + error.localizedDescription)
    }
}

let geoCoordDelegate = GeoCordDelegate()
locationMgr.delegate = geoCoordDelegate
locationMgr.desiredAccuracy = kCLLocationAccuracyBest
locationMgr.startUpdatingLocation()

The first time it runs you will be prompted to allow access to location for the Example-n-.app.

Upvotes: 1

Related Questions