1

如何模擬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

+0

http://stackoverflow.com/questions/1409141/location-manager-error-kclerrordomain-error-0 –

+0

對不起,這是行不通的。此外,沒有關於watchOS模擬器的信息。 – Vyacheslav

回答

1

爲了使手錶模擬器位置檢測工作,您需要以設置iPhone模擬器的位置。我建議你遵循的步驟,在iPhone模擬器

  1. 設置位置,(調試 - >位置 - >自定義位置)手錶simlautor
  2. 設置位置,(調試 - >位置 - >自定義位置)
  3. 有時,當您運行watchkit應用程序時,iPhone模擬器中的位置重置爲無。因此在您訪問該位置之前在觀看擴展代碼之前放置了斷點。檢查位置在兩個模擬器中設置。

希望這會有所幫助。

示例代碼迅速,

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

沒有。我測試了很多次。它不起作用 – Vyacheslav

+0

iPhone/iPad模擬器的作品。但是watchOS不會。 – Vyacheslav

+0

通過使用上述步驟,我可以成功地模擬手錶應用中的位置。 – vkhemnar