0

我试图在手机被锁定或在后台运行时接收位置更新,但我无法工作。

这是我所做的。我添加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 ];
            }
        }
    }
}
4

1 回答 1

0

为了解决这个问题,我替换Required Background Modes 'location'Required Background modes 'App registers for location updates'

于 2013-11-14T16:57:39.533 回答