WatchOS模拟器中的位置检测失败

时间:2016-08-18 15:03:02

标签: objective-c cllocationmanager watch-os-2 watchos-simulator

如何模拟watchOS模拟器的位置?

使用请求

- (void) requestLocation {
    locationManager = [CLLocationManager new];

    locationManager.delegate = self;

    [locationManager requestWhenInUseAuthorization];
    [locationManager requestLocation];
}

我总是遇到错误:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        // Error here if no location can be found
}

错误是 NSError * domain: @"kCLErrorDomain" - code: 0 0x7a867970

1 个答案:

答案 0 :(得分:1)

为了在手表模拟器中进行位置检测,您还需要设置iPhone模拟器的位置。我建议你按照步骤

  1. 在iPhone模拟器中设置位置,(调试 - >位置 - >自定义位置)
  2. 在watch simlautor中设置位置,(Debug - > Location - > Custom location)
  3. 有时,当您运行watchkit应用时,iPhone模拟器中的位置会重置为无。因此,在访问该位置之前,请在监视扩展代码中添加断点。检查位置在两个模拟器中都已设置。
  4. 希望这有帮助。

    swift中的示例代码,

    class LocationManager: NSObject, CLLocationManagerDelegate
    {
    static let sharedInstance = VCLocationManager()
    private var locationManager : CLLocationManager?
    
    private override init()
    {
    
    }
    
    func initLocationMonitoring()
    {
        //didChangeAuthorizationStatus is called as soon as CLLocationManager instance is created.
        //Thus dont check authorization status here because it will be always handled.
    
        if locationManager == nil
        {
            locationManager = CLLocationManager()
            locationManager?.desiredAccuracy = kCLLocationAccuracyBest
            locationManager?.delegate = self
        }
        else
        {
            getCurrentLocation()
        }
    }
    
    func getCurrentLocation()
    {
        let authorizationStatus = CLLocationManager.authorizationStatus()
        handleLocationServicesAuthorizationStatus(authorizationStatus)
    }
    
     func handleLocationServicesAuthorizationStatus(status: CLAuthorizationStatus)
    {
        switch status
        {
        case .NotDetermined:
            handleLocationServicesStateNotDetermined()
        case .Restricted, .Denied:
            handleLocationServicesStateUnavailable()
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            handleLocationServicesStateAvailable()
        }
    }
    
    func handleLocationServicesStateNotDetermined()
    {
        locationManager?.requestWhenInUseAuthorization()
    }
    
    func handleLocationServicesStateUnavailable()
    {
        //Ask user to change the settings through a pop up.
    }
    
    func handleLocationServicesStateAvailable()
    {
        locationManager?.requestLocation()
    }
    
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
    {
        handleLocationServicesAuthorizationStatus(status)
    }
    
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        guard let mostRecentLocation = locations.last else { return }
        print(mostRecentLocation)
    }
    
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print("CL failed: \(error)")
    }
     }