WatchOS模拟器中的位置检测失败 [英] Location detection in WatchOS simulator has been failed
本文介绍了WatchOS模拟器中的位置检测失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为watchOS模拟器模拟位置?
How to simulate a location for watchOS simulator?
与请求一起使用
- (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 *域:@ kCLErrorDomain-代码:0 0x7a867970
推荐答案
为使手表模拟器中的位置检测有效,您还需要设置位置iPhone模拟器。我建议您按照以下步骤操作
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
- 在iPhone模拟器中设置位置,(调试->位置->自定义位置)
- 在手表模拟器中设置位置,(调试->位置->自定义位置)
- 有时,当您运行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)")
}
}
这篇关于WatchOS模拟器中的位置检测失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文