如何模拟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 :(得分:1)
为了在手表模拟器中进行位置检测,您还需要设置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)")
}
}