1

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

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194

1 Answers1

1

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)")
}
 }
vkhemnar
  • 338
  • 3
  • 9
  • I am successfully able to simulate location in my watch app by using above mentioned steps. – vkhemnar Aug 31 '16 at 11:52
  • Prior to this, i have added NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription keys in Info.plist. In the code, i dont call requestLocation right away after creating locationManager because creating this object it automatically calls didChangeAuthorizationStatus. In this function i check status and if it is AuthorizedAlways, or AuthorizedWhenInUse then i call requestLocation – vkhemnar Aug 31 '16 at 12:00
  • I brief, I would say the same code worked in iPhone simulator with the same preferences doesn't work in watchOS. – Vyacheslav Aug 31 '16 at 12:27