【问题标题】:CLLocationManager not receiving background updates in ios7CLLocationManager 未在 ios7 中接收后台更新
【发布时间】:2013-11-14 02:17:03
【问题描述】:

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

这就是我所做的。我在app plist中添加了Required Background Modes 'location',并在下面的initMethod中设置了location manager。

-(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 ];
            }
        }
    }
}

【问题讨论】:

  • 从哪里开始位置管理器?
  • 我在我的旅行视图控制器中启动它,在 insertNewObject 方法中。
  • 这是您唯一开始的地方吗?你在哪里停下来?当应用程序进入后台时它是否可能被停止?
  • 当用户在指定半径内时,我将在 locationUpdate 中停止它。当视图消失并且当我回来更新恢复时,我不会在任何地方停止它。

标签: ios cllocationmanager


【解决方案1】:

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

【讨论】:

  • 它们是同一个东西,所以这应该没有什么区别。 (您可以通过右键单击 plist 中的条目并(取消)选择“显示原始键/值”在它们之间切换)
  • 它们不是一回事。位置不是有效的替代品。在ios7中需要声明,否则手机后台应用刷新设置下不会声​​明应用。
  • 它最终在 plist 中是一样的
猜你喜欢
  • 2016-07-23
  • 2017-05-30
  • 2016-05-18
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多