アプリにジオフェンシングを実装しようとしています。Kevin McMahonによる素晴らしいチュートリアルに取り組んでいます。
私の問題は、ジオフェンスを作成しようとするたびに "EXC_BAD_ACCESS (code=1, address= ...) が発生することです。
これが私のコードです:
NSMutableArray *placeID = [[NSMutableArray alloc] init];
NSMutableArray *lats = [[NSMutableArray alloc] init];
NSMutableArray *longs = [[NSMutableArray alloc] init];
for (int i = 0; i < [responseObject count]; i++) {
[placeID addObject:[[responseObject objectAtIndex:i]objectForKey:@"id"]];
[longs addObject:[[responseObject objectAtIndex:i]objectForKey:@"long"]];
[lats addObject:[[responseObject objectAtIndex:i]objectForKey:@"lat"]];
}
NSMutableDictionary *latDict = [[NSMutableDictionary alloc] initWithObjects:lats forKeys:placeID];
NSMutableDictionary *longDict = [[NSMutableDictionary alloc] initWithObjects:longs forKeys:placeID];
NSMutableArray *geofences = [[NSMutableArray array] init];
for(int k = 0; k < [responseObject count]; k++) {
CLRegion *geofence = [self mapDictionaryToRegionWithLat:latDict withLong:longDict usingID:[placeID objectAtIndex:k]];
[geofences addObject:geofence];
}
[self initializeRegionMonitoring:geofences];
CLRegion の作成
- (CLRegion*)mapDictionaryToRegionWithLat:(NSDictionary *)latDict withLong:(NSDictionary *)longDict usingID: (NSString *)placeStringID {
NSString *title = [NSString stringWithFormat:@"title %@", placeStringID];
CLLocationDegrees latitude = [[latDict objectForKey:placeStringID] doubleValue];
CLLocationDegrees longitude = [[longDict objectForKey:placeStringID] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance regionRadius = 10.0;
return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
radius:regionRadius
identifier:title];
}
領域モニターの初期化
- (void) initializeRegionMonitoring:(NSArray*)geofences {
if (locationManager == nil) {
//[NSException raise:@"Location Manager Not Initialized" format:@"You must initialize location manager first."];
}
if(![CLLocationManager regionMonitoringAvailable]) {
//[self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
return;
}
for(CLRegion *geofence in geofences) { //ERROR HAPPENS HERE AFTER 7 ITERATION
[locationManager startMonitoringForRegion:geofence];
NSLog(@"geofence made here:\n\n%@\n\n",geofences);
}
}
"locationManager: manage didStartMonitoringForRegion: region" の NSLog が表示されないため、"for(CLRegion *geofence in geofences)" の 7 回目の繰り返し (b/c 7 つのジオフェンスがあります) の後にエラーが発生するようです。