私はアプリが殺されたり終了したときに、緯度と経度を取得する必要がある機能を1つ持っています。歩行中に場所が更新されず、アプリが殺されたとき
車に乗っているときに、アプリが殺されたり終了したりすると、緯度と経度が表示されています。
しかし、私が数分間歩き始めたとき、私はアプリの殺された/終了した緯度と経度を取得していません。
以下は私の論理です。上記
+ (CLLocationManager *)sharedLocationManager {
static CLLocationManager *_locationManager;
@synchronized(self) {
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_locationManager.allowsBackgroundLocationUpdates = YES;
_locationManager.pausesLocationUpdatesAutomatically = NO;
_locationManager.activityType = CLActivityTypeOther;
_locationManager.distanceFilter = kCLDistanceFilterNone;
}
}
return _locationManager;
}
- (id)init {
if (self==[super init]) {
//Get the share model and also initialize myLocationArray
self.shareModel = [LocationShareModel sharedModel];
self.shareModel.myLocationArray = [[NSMutableArray alloc]init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
return self;
}
-(void)applicationEnterBackground{
CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
[locationManager stopMonitoringSignificantLocationChanges];
if(IS_OS_8_OR_LATER) {
[locationManager requestAlwaysAuthorization];
}
[locationManager startMonitoringSignificantLocationChanges];
//Use the BackgroundTaskManager to manage all the background Task
self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];
}
私はappdelegate
から呼び出す午前AppDelegate.mファイルで、私は
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
self.locationTracker = [[LocationTracker alloc]init];
[self.locationTracker startLocationTracking];
//Send the best location to server every 60 seconds
//You may adjust the time interval depends on the need of your app.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
NSTimeInterval time = 10;
self.locationUpdateTimer =
[NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:@selector(updateLocation)
userInfo:nil
repeats:YES];
}
}
return YES;
}
の下のような任意のヒントを書いた私のLocationmanagerクラスです??ありがとう...
背景モードで関連する変更を行いましたか? –
@ReshmiMajumder:はい、位置と背景を取得するためのバックグラウンドモードを有効にしました – Nirav
歩いている間に場所の更新にインターネットまたは3Gが必要ですか? @ReshmiMajumder – Nirav