iOS swift geofence无法在真实设备上运行

时间:2015-09-02 16:49:34

标签: ios iphone xcode swift2 xcode7-beta5

我的代码工作正常,我想要它在模拟器中的确切方式。但是,当我把它放在真实设备上并转到该位置时,locationManager(..., didEnterRegion region: CLRegion)不会被调用。我不认为这是代码中的错误,因为当我在模拟器中输入和离开位置时,didExitRegiondidEnterRegion都会被调用,但不会在真实设备上调用。我在 Swift 2.0 Xcode 7 beta 5 中使用iPhone 5S进行测试,在大型开放区域内具有良好的小区覆盖率。我也尝试过不同的半径。我的简化代码如下:

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)")
    }
}

是否有其他人遇到过这样的麻烦或者Xcode 7中的语法有变化?

0 个答案:

没有答案