iOS7 CLLocationManager 暂停更新并且不会恢复
Posted
技术标签:
【中文标题】iOS7 CLLocationManager 暂停更新并且不会恢复【英文标题】:iOS7 CLLocationManager pauses updates and won't resume 【发布时间】:2014-03-08 20:44:10 【问题描述】:我有一段代码可以在 ios 模拟中运行并获得连续的位置更新,但是当我在 iPad 上运行它时,它似乎在第三次更新后停止。我试图将 activityType 更改为 other or 并设置可能导致它暂停更新的内容,但这些似乎没有帮助。我也不清楚为什么它在现实生活中而不是在模拟中停止。
我确实尝试在每次更新后停止更新,然后执行 startUpdates,这似乎确实有效,但这让我觉得这是一种不好的行为方式。是否有人对此类问题有其他建议或经验。
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *myLocationManager;
@end
@implementation ViewController
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
for (int locIndex = 0; locIndex < locations.count; locIndex++)
CLLocation *newLocation = [locations objectAtIndex:locIndex];
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
CLLocationDistance distance = 0;
NSTimeInterval timeout = 1;
[manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:timeout];
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
CLLocationDistance distance = 0;
NSTimeInterval timeout = 1;
[manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:timeout];
/* We received the new location */
NSLog(@"Latitude = %f", newLocation.coordinate.latitude);
NSLog(@"Longitude = %f", newLocation.coordinate.longitude);
//[self.myLocationManager stopUpdatingLocation];
//[self.myLocationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
/* Failed to receive user's location */
NSLog(@"******************************************************");
NSLog(@"************** FAILED TO RECEIVE LOCATION ************");
NSLog(@"******************************************************");
- (void)viewDidLoad
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled])
self.myLocationManager = [[CLLocationManager alloc] init];
self.myLocationManager.delegate = self;
//self.myLocationManager.pausesLocationUpdatesAutomatically = NO;
//self.myLocationManager.activityType = CLActivityTypeOther;
[self.myLocationManager startUpdatingLocation];
//self.myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
else
/* Location services are not enabled.
Take appropriate action: for instance, prompt the
user to enable the location services */
NSLog(@"Location services are not enabled");
【问题讨论】:
iPad 是否有 SIM 卡,即是否有移动数据? 【参考方案1】:当用户忘记 GPS 处于活动状态时使用暂停。为防止 GPS 耗尽电池电量,在重新启动应用程序之前,GPS 会停止并重新打开。
您应该接听暂停呼叫并询问用户位置更新是否应该继续。
【讨论】:
以上是关于iOS7 CLLocationManager 暂停更新并且不会恢复的主要内容,如果未能解决你的问题,请参考以下文章
CLLocationManager requestWhenInUseAuthorization() 不能在 ios7 上使用 swift 2.0
Objective C,iOS 7 - CLLocationManager:didUpdateLocations 只被调用一次