我正在使用单例类来跟踪用户位置。如果我从正在运行的应用程序中实例化该类并开始跟踪一切都很好。
但是,如果应用程序关闭(在应用程序切换器中,因此不再后台运行)并重新启动,我会检查是否有活动事件。在这种情况下,我实例化类并开始位置跟踪。被locationManager
实例化并且位置箭头出现在状态栏中,但是
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
or
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
never get called
任何想法?
代码singelton类:
@interface AlarmMeTrackUserLocation () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation AlarmMeTrackUserLocation
-(void)toggleLocationUpdates:(bool)toggle {
if (!toggle) {
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
sharedInstance = nil;
NSLog(@"--- UserTrack Stopped ---");
return;
}
if (!self.locationManager && toggle) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
self.locationManager.distanceFilter = 200;
}
NSLog(@"--- UserTrack Started");
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
...
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
...
}
+ (AlarmMeTrackUserLocation *)sharedTrackUserLocation {
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
//NSLog(@"--- New Instance created ---");
return sharedInstance;
}
NSLog(@"--- Existing Instance Used ---");
return sharedInstance;
}
来自 AppDelegate 的代码:
@property (strong, nonatomic) AlarmMeTrackUserLocation *userTrack;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
self.userTrack = [AlarmMeTrackUserLocation sharedTrackUserLocation];
self.userTrack.event = currentEvent;
[self.userTrack toggleLocationUpdates:YES];
}