My code works fine and exactly how I want it to to in emulator. But when I put it on the real device and go to the location the locationManager(..., didEnterRegion region: CLRegion)
does not get called. I don't think that it is a bug in the code because when I enter and leave the location in the simulator both the didExitRegion
and didEnterRegion
get called, but not on the real device. I'm testing with an iPhone 5S in Swift 2.0 Xcode 7 beta 5 with a good cell coverage in a large open area. I have also tried different radiuses. My simplified code is below:
import UIKit
import CoreLocation
import MapKit
class GeoLapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var manager: CLLocationManager?
var location: CLLocationCoordinate2D!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager?.delegate = self;
manager?.desiredAccuracy = kCLLocationAccuracyBest
self.mapView.delegate = self
mapView.mapType = MKMapType.Satellite
manager?.startUpdatingLocation()
let currRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude), radius: 20, identifier: "Laplocation")
manager?.startMonitoringForRegion(currRegion)
let newAnotation = MKPointAnnotation()
newAnotation.coordinate = location
newAnotation.title = "Start/Finish location"
mapView.addAnnotation(newAnotation)
let circle = MKCircle(centerCoordinate: location, radius: 150 as CLLocationDistance)
self.mapView.addOverlay(circle)
print(location.latitude)
print(location.longitude)
print(location)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.blueColor()
circle.fillColor = UIColor(red: 0, green: 0, blue: 255, alpha: 0.4)
circle.lineWidth = 1
return circle
} else {
return nil
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
let location = locations[0]
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location, completionHandler: { (data, error) -> Void in
self.mapView.centerCoordinate = location.coordinate
let reg = MKCoordinateRegionMakeWithDistance(location.coordinate, 100, 100)
self.mapView.setRegion(reg, animated: true)
self.mapView.showsUserLocation = true
})
}
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entering region")
}
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Exit region")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("\(error)")
}
}
Any one else having troubles like this or has the syntax changed in Xcode 7?