【发布时间】:2015-09-18 01:20:15
【问题描述】:
我正在尝试在我的应用处于后台时获取位置。当我使用我的应用程序时,一切都很好。我也可以切换到任何其他应用程序(甚至是主屏幕)并且位置仍然可以进入。如果我正在使用我的手机但我的应用程序在后台,只要我的手机处于活动状态,位置更新似乎就可以了(未锁定)。
但是,如果我锁定手机并在大约 10 分钟后回来,状态栏中的位置图标和我的应用不再获取位置更新。
我检查了应用程序崩溃,但没有崩溃报告。我的应用仍然出现在应用列表中,所以我认为它没有崩溃。
如果我请求“始终”许可,GPS 是否应该永远保持开启状态。还是手机休眠一段时间后操作系统会关闭它?
这是我的 plist 中的条目:
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
下面是后台定位服务的截图:
这是我的自定义类,即位置管理器委托:
@interface LocationDelegate ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation LocationDelegate
+ (instancetype) singleton {
static LocationDelegate *delegate = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
delegate = [[self alloc] init];
});
return delegate;
}
- (id) init {
if (self = [super init]) {
// for now filter and accuracy are based on the same preference
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
// Check for iOS 8
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
return self;
}
注意它是一个单例。这有关系吗?在我的应用程序在后台运行一段时间或暂停后,此类是否会被清理?从我读过的内容来看,我不认为像这样的单例会被清理,因为它们是强引用,所以我认为这不是问题......但我真的不知道为什么我在一定数量后停止获取位置时间。
编辑:
我的位置管理器似乎在一段时间后暂停位置更新。最大的问题是我没有接到简历电话。
我将activityType设置为默认值
CLActivityTypeOther
我可以尝试其他活动类型,看看是否会有所作为。但是,我依靠我的应用程序来进行步行和驾驶。
我想我遇到了同样的问题:
【问题讨论】:
-
嗨@lostintranslation,我有同样的问题。如果你有,请给我答案。
标签: ios objective-c iphone cllocationmanager