我正在尝试接收位置更新,而手机已锁定或在后台运行,我无法工作。
这就是我所做的。我将Required Background Modes 'location'
添加到了app plist,并在以下initMethod中设置了位置管理器。
-(id)init {
if ( self = [ super init ] ) {
self.locationManager = [ CLLocationManager new ];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
//self.locationManager.distanceFilter = 75;
//self.locationManager.desiredAccuracy = 20;
self.currentSegment = [ [ BestRouteSegment alloc ] init ];
self.segmentKeys =
[ [ NSArray alloc ] init ];
}
return self;
}
我已经实现了委托方法,当应用程序处于前台时一切正常但是只要我触摸主屏幕或锁定手机更新就停止了。我已经阅读了几个关于这个问题的帖子,我已经添加了文档中提到的所有内容,但仍然没有运气。有什么建议吗?
这是我开始请求更新的地方
/* Initiates a new trip by allocating a trip on the heap and begins
requesting location updates
*/
- (void)insertNewObject:(id)sender {
self.deepSleepPreventer = [[SleepPreventer alloc] init];
[self.deepSleepPreventer startPreventSleep];
if ( !_itemsToDisplay ) {
_itemsToDisplay = [ [ NSMutableArray alloc ] init ];
}
self.brain.currentTrip = [ [ BestRouteTrip alloc ] init ];
self.brain.currentRoute.allTripsFromRoute =
[ self.brain.currentRoute addTrip:self.brain.currentTrip ];
[ self.brain.locationManager startUpdatingLocation ];
#warning Bad UI technique
// Hide back and add button from user
self.navigationItem.hidesBackButton = YES;
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.title = @"Trip Active ...";
}
以下是处理地点的位置。
- (void)locationUpdate:(CLLocation *) newLocation {
CLLocationCoordinate2D location;
location.latitude = newLocation.coordinate.latitude;
location.longitude = newLocation.coordinate.longitude;
if ( self.brain.currentTrip ) {
if ( !self.brain.currentTrip.timer.start &&
self.brain.currentSegment.startCoord.latitude ) {
if ( [ self.brain isCoordinate:location WithinDistance:200
OfCoordinate:self.brain.currentSegment.startCoord ]) {
// Don't start timing until destination is selected
if ( self.brain.currentSegment.endCoord.latitude )
[ self.brain.currentTrip.timer startTiming ];
}
}
if ( !self.brain.currentTrip.timer.end && self.brain.currentSegment.endCoord.latitude ) {
// Has the user reached their location once ending coord has been selected
if ( [ self.brain isCoordinate:location WithinDistance:200
OfCoordinate:self.brain.currentSegment.endCoord ] ){
[ self.brain.currentTrip .timer stopTiming ];
self.brain.currentTrip.tripTime =
[ self.brain.currentTrip.timer elapsedTime ] / 60.0; // Convert to minutes
[ self.brain.locationManager stopUpdatingLocation ];
NSIndexPath *indexPath =
[ NSIndexPath indexPathForRow:_itemsToDisplay.count inSection:0 ];
NSString *itemToDisplay =
[ @"Trip" stringByAppendingString:
[ NSString stringWithFormat:@"%d", _itemsToDisplay.count + 1 ] ];
itemToDisplay =
[ itemToDisplay stringByAppendingString:[ NSString stringWithFormat:@" ( %f )", self.brain.currentTrip.tripTime ] ];
[ _itemsToDisplay insertObject:itemToDisplay atIndex:
_itemsToDisplay.count ];
[ self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic ];
self.brain.currentRoute.avgRouteTime =
[ self.brain.currentRoute determineAverageTime ];
// Put buttons back on navigation bar
self.navigationItem.hidesBackButton = NO;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.title = @"Trips";
[ self.deepSleepPreventer stopPreventSleep ];
[ self.brain writeData ];
}
}
}
}
答案 0 :(得分:0)
为了解决此问题,我将Required Background Modes 'location'
替换为Required Background modes 'App registers for location updates'