電話がロックされているか、バックグラウンドで実行されている間に位置情報の更新を受信しようとしていますが、どちらも機能しません。
これが私がやったことです。アプリの plistに追加Required Background Modes 'location'
し、次の 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 ];
}
}
}
}