不久前,我为 iOS 10 编写了一个 GPS 应用程序。我已经让它稳定并按预期工作,现在我想追求省电技术。该应用程序现在每 5 分钟发送一次 GPS 位置,但我可以灵活地选择发送 GPS 位置的频率,只要它们发送的频率不是太低。我的应用程序还需要“最佳”或“10m”GPS 定位设置,我知道这会消耗大量电量,但我不能根据我的要求使用 3km 设置(我知道这会节省更多电量)。
从我收集并阅读文档并使用代码使其工作的内容来看,似乎有两种技术:
- 结合 UNLocationNotificationTrigger 设置 pausesLocationUpdatesAutomatically = YES
- allowDeferredLocationUpdatesUntilTraveled:timeout:
根据iOS 10 和 11 中的这篇文章, 2 号显然根本不起作用。
当我将它登录到我的服务器时,数字 1 给了我零星的结果。有时它可以工作,有时我会在不知名的地方获得一个 GPS 点(没有看到暂停或恢复日志消息),然后它开始工作,其他时候它似乎根本不起作用。
有时我会在同一秒内看到 2 次连续停顿。
其他时候,我看到它背靠背(相隔几分钟)恢复位置,而从未看到暂停日志消息。其他时候,我看到暂停日志消息,几分钟后(或一段时间后)恢复 GPS 发送而没有看到“恢复”日志消息。
- (void) setGPSBatterySavingTrigger
{
if (IS_IOS_10_OR_LATER && ([g_prefs getGPSAuthStatus] == kCLAuthorizationStatusAuthorizedAlways))
{
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(g_locMgr.getLastLocation.latitude, g_locMgr.getLastLocation.longitude);
CLRegion *region = nil;
float radius_m = 150.0;
region = [[CLCircularRegion alloc] initWithCenter:center radius:radius_m identifier:@"pauseLocationsCenter"];
if (!region) {
[logger log:TSERVLOG :@"pause radius failed at radius <%f>", radius_m];
return;
}
region.notifyOnEntry = NO;
region.notifyOnExit = YES;
UNLocationNotificationTrigger *trigger = [UNLocationNotificationTrigger
triggerWithRegion:region repeats:NO];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = PROG_NAME;
content.body = @"GPS Restarted";
//content.sound = [UNNotificationSound soundNamed:nil];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"GPSTextNotify" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError *error)
{
if (error.code)
{
[logger log:TSERVLOG :@"UNUserNotificationCenter addRequest FAILED <%@>", error.localizedDescription];
}
}];
}
}
- (void) locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager
{
[logger log:TSERVLOG :@"===== JME GPS PauseLocationUpdates at <%@> <%f, %f>",
[mydate getCurrentDateTimeStr], self.getLastLocation.latitude, self.getLastLocation.longitude];
[self setGPSBatterySavingTrigger];
}
- (void) locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager
{
[logger log:TSERVLOG :@"===== GPS ResumeLocationUpdates at <%@> <%f, %f>",
[mydate getCurrentDateTimeStr], self.getLastLocation.latitude, self.getLastLocation.longitude];
}
底线问题:
- 有没有人能够使用其中任何一种来节省电池 + GPS 在 iOS 10+ 中可靠地工作?
- 如果没有,您做了哪些组合措施来让它 100% 地工作,同时节省电池寿命?
- 我是否错过了另一种有效的技术?(注意我的要求不能使用 3km 设置)
谢谢您的意见!