Определение местоположения в симуляторе WatchOS не удалось

Как смоделировать локацию для симулятора 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 ответ

Чтобы функция определения местоположения работала в симуляторе часов, вам также необходимо установить симулятор местоположения в iPhone. Я предлагаю вам следовать шагам как

  1. Установить местоположение в симуляторе iPhone, (Отладка -> Местоположение -> Пользовательское местоположение)
  2. Установить местоположение в часовом симлауторе, (Отладка -> Местоположение -> Пользовательское местоположение)
  3. Иногда местоположение в симуляторе iPhone сбрасывается на "Нет" при запуске приложения watchkit. поэтому, прежде чем получить доступ к местоположению, поставьте точку останова в коде расширения часов. Проверьте местоположение установлено в обоих симуляторах.

Надеюсь это поможет.

Пример кода в 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)")
}
 }
Другие вопросы по тегам