私は、ユーザーが多数の場所を保存できる場所ベースのアプリケーションを開発しています。ユーザーがその保存された場所の近くを通過すると、アプリケーションはローカル通知によってユーザーに通知します。
この機能を実現するために、次のコードを使用しました。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
[self showAlertWithTitle:@"" andMessage:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh."];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
[self showAlertWithTitle:@"" andMessage:@"The functions of this app are limited because the Background App Refresh is disable."];
} else {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
// This "afterResume" flag is just to show that he receiving location updates
self.shareModel.afterResume = YES;
self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; //kCLLocationAccuracyBestForNavigation
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
[self.shareModel.anotherLocationManager startUpdatingLocation];
self.shareModel.anotherLocationManager.pausesLocationUpdatesAutomatically = YES;
if(IS_OS_8_OR_LATER) {
if ([self.shareModel.anotherLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.shareModel.anotherLocationManager requestWhenInUseAuthorization];
}
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentLocation = [locations lastObject];
latitudeTemp = currentLocation.coordinate.latitude;
longitudeTemp = currentLocation.coordinate.longitude;
[self locationChangeLogic];
//If the timer still valid, return it (Will not run the code below)
if (self.shareModel.timer)
return;
self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];
//Restart the locationMaanger after 30 Seconds
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:30
target:self
selector:@selector (restartLocationUpdates)
userInfo:nil
repeats:YES];
//Will only stop the locationManager after 10 seconds, so that we can get some accurate locations
//The location manager will only operate for 10 seconds to save battery
NSTimer * delay10Seconds;
delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10
target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:YES];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//Remove the "afterResume" Flag after the app is active again.
self.shareModel.afterResume = NO;
if(self.shareModel.anotherLocationManager)
[self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];
self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; //kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
[self.shareModel.anotherLocationManager startUpdatingLocation];
if(IS_OS_8_OR_LATER) {
if ([self.shareModel.anotherLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.shareModel.anotherLocationManager requestWhenInUseAuthorization];
}
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}
上記のコードを使用することで、ユーザーがアプリケーションを強制終了した場合でも場所を取得できますが、正確ではありません。時々私は場所を取得しませんでした、そして時々私はその保存された場所を通過した後に場所を取得します。ユーザーがその特定の半径を入力したときに、正確に位置通知を表示する必要があります。
この問題を解決するために私を助けてください。
前もって感謝します。
リージョンモニタリング(ジオフェンシング)は私の要件を満たしています。
詳細については、次のリンクをいくつか使用できます。
皆さんありがとう..
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加