【问题标题】:Location detection in WatchOS simulator has been failedWatchOS 模拟器中的位置检测失败
【发布时间】:2016-12-25 14:01:21
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

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

  1. 在 iPhone 模拟器中设置位置,(调试 -> 位置 -> 自定义位置)
  2. 在手表模拟器中设置位置,(调试 -> 位置 -> 自定义位置)
  3. 有时,当您运行 watchkit 应用程序时,iPhone 模拟器中的位置会重置为无。因此,在您访问该位置之前,在手表扩展代码中放置断点。在两个模拟器中都设置了检查位置。

希望这会有所帮助。

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

【讨论】:

  • 通过使用上述步骤,我成功地在手表应用中模拟了位置。
  • 在此之前,我在 Info.plist 中添加了 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription 键。在代码中,我不会在创建 locationManager 后立即调用 requestLocation,因为创建此对象会自动调用 didChangeAuthorizationStatus。在这个函数中,我检查状态,如果它是 AuthorizedAlways 或 AuthorizedWhenInUse 然后我调用 requestLocation
  • 我简短地说,我想说在具有相同偏好的 iPhone 模拟器中工作的相同代码在 watchOS 中不起作用。
猜你喜欢
  • 2015-05-27
  • 1970-01-01
  • 2022-07-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
相关资源
最近更新 更多