5

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?

Ryan Westcott
  • 199
  • 3
  • 14
  • Where is `location` set? – Paulw11 Sep 02 '15 at 20:18
  • It gets sent through the segue from another VC into the location variable. – Ryan Westcott Sep 02 '15 at 20:19
  • @RyanWestcott did you find the solution?? Even I am stuck with the same problem now. – SaiPavanParanam Dec 20 '16 at 14:46
  • @SaiPavanParanam i have same problem. did you find any solution? – Yogendra Patel Jan 31 '20 at 12:47
  • @YogendraPatel yes i did find the solution. But it does not concern the criteria mentioned above. I had more than 1000 locations and was activating the nearest 20 locations as that was the max limit when there is a change in 0.5 km for every second. In simulator that speed was possible, but not in real time as no once can go that fast on the road. That condition was put there to make sure I refresh the nearest location based on the location updates. As i was not moving at that speed, the geofence is not activated as per my code. – SaiPavanParanam Feb 03 '20 at 10:12

0 Answers0