【问题标题】:Using freeway drive/ city run in iOS simulator in background mode在后台模式下在 iOS 模拟器中使用高速公路/城市运行
【发布时间】:2015-01-02 19:52:42
【问题描述】:

我正在尝试使用 iOS 模拟器中的选项:debug->freeway drive/city run 来模拟位置更新。

在我的代码中,我使用CLLocationManager 通过以下代码获取位置更新:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDistanceFilter:20];
}

-(void)viewWillAppear:(BOOL)animated {
[locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)lm didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
NSLog(@"Location returned: %f, %f Accuracy: %f", location.coordinate.latitude,  location.coordinate.longitude, location.horizontalAccuracy);
}

当我的应用程序在后台并且我在模拟器中选择选项时,我从来没有收到关于位置更新的委托回调。

我为我的应用提供了位置更新的后台模式。请让我知道如何准确使用这些功能,或者我是否在这里遗漏了什么。

【问题讨论】:

    标签: ios ios-simulator core-location cllocationmanager


    【解决方案1】:

    我终于解决了这个问题。模拟器的选项工作得很好,但问题是 CLLocation 的实现。

    在 iOS 8 上,位置更新代码将不起作用,除非:

    1. 您将 NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription 添加到 plist 中,其中包含一些将提示用户的字符串值。

    2. 您需要添加询问用户的权限才能使位置代码起作用:

       [self.locationManager requestWhenInUseAuthorization]
       [self.locationManager requestAlwaysAuthorization]
      

    取自 this post

    【讨论】:

    • 谢谢!我知道我需要的是与此类似的东西,因此在将这些条目添加到 plist 并在开始更新位置之前询问用户之后,我的问题就解决了。 :)
    猜你喜欢
    • 1970-01-01
    • 2019-11-26
    • 2013-05-19
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    相关资源
    最近更新 更多