6

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回電話をかけないようにする方法はありますか? または、領域の作成または監視中に間違いを犯していますか? 助けてください..よろしくお願いします..

4

1 に答える 1

9

この質問は私自身の重複だと思います。Apple の地域監視 API にバグがあると思います。私はすでにこの問題について Apple にレーダーを提出しましたが、まだ何も連絡がありません。

これを回避する 1 つの方法は、didEnter メソッドと didExit メソッドにタイムスタンプを保存することです。メソッドが保存されたタイムスタンプの 10 秒以内に起動する場合は、そのメソッドをだまされてスキップします。

誰かが興味を持っている場合は、github にこの問題を示すプロジェクトがあります。

https://github.com/billburgess/GeofenceBug

Apple が問題に気づき、行動を起こす唯一の方法であるため、別のレーダーを提出してください。レーダー番号は 12452255 です - 重複したデリゲートが地域監視を呼び出しています。

このレーダーをごまかしたい場合は、情報を含むオープン レーダー リンクを次に示します。 http://openradar.appspot.com/radar?id=2484401

于 2012-12-28T12:16:25.747 に答える