Location detection in WatchOS simulator has been failed

733 views Asked by At

How to simulate a location for watchOS simulator?

using with request

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

    locationManager.delegate = self;

    [locationManager requestWhenInUseAuthorization];
    [locationManager requestLocation];
}

I always catch an error:

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

The error is NSError * domain: @"kCLErrorDomain" - code: 0 0x7a867970

1

There are 1 answers

3
vkhemnar On

In order to make location detection work in watch simulator, You are required to set the location iPhone simulator as well. i Suggest you to follow steps as

  1. Set location in iPhone simulator, (Debug ->Location ->Custom location)
  2. Set location in watch simlautor, (Debug ->Location ->Custom location)
  3. Sometimes, location in iPhone simulator is reset to None when you run watchkit app. hence put break point before in watch extension code before you access the location. Check location is set in both simulators.

Hope this helps.

Sample code in 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)")
}
 }