iOS アプリで地域監視などの位置情報サービスを使用しています。これが私のコードです
//this for creating region
-(void)createRegion
{
[dictionary setValue:@"23 St, New York" forKey:@"title"];
[dictionary setValue:[NSNumber numberWithDouble:40.742878] forKey:@"latitude"];
[dictionary setValue:[NSNumber numberWithDouble:-73.992821] forKey:@"longitude"];
[dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"];
[regionArray addObject:[self mapDictionaryToRegion:dictionary]];
[self initializeRegionMonitoring:regionArray];
}
- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary {
NSString *title = [dictionary valueForKey:@"title"];
CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];
return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
radius:regionRadius
identifier:title];
}
- (void) initializeRegionMonitoring:(NSArray*)geofences {
if(![CLLocationManager regionMonitoringAvailable]) {
// [self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
return;
}
for(CLRegion *geofence in geofences) {
[_locationManager startMonitoringForRegion:geofence];
}
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"entered region %@",region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"exited region %@",region.identifier);
}
アプリがフォアグラウンドの場合は正常に動作します。ログが表示されます:「領域に入りました..」と「領域を出ました..」ですが、アプリがバックグラウンドに移行すると、同じログがほんの数秒で2回出力されます。つまり、デリゲートメソッドが2回呼び出されます。 2回電話をかけないようにする方法はありますか? または、領域の作成または監視中に間違いを犯していますか? 助けてください..よろしくお願いします..