IOS swift geofence not working on real device

My code works fine and exactly how I want it to be in the emulator. But when I put it on a real device and moved into place, it locationManager(..., didEnterRegion region: CLRegion)won’t be called. I do not think that this is a mistake in the code, because when I enter and leave a place in the simulator, they are called both didExitRegion, and didEnterRegion, but not on a real device. I am testing iPhone 5S in Swift 2.0 Xcode 7 beta 5 with good cell coverage in a large open area. I also tried different radii. 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)")
    }
}

Anyone else having problems like this or the syntax changed in Xcode 7?

+4

All Articles