iOS 5のCoreLocation地域デリゲートメソッドに問題があります。シミュレータとデバイスの両方に問題があります。監視のための領域を追加しようとしていて、didStartMonitoring
代理コールバックを待っています。まれに、うまく動作します。しかし、通常didStartMonitoring
でもmonitoringDidFail
も呼ばれません。地域はmonitoredRegions
に追加されます。デリゲートオブジェクトは正しく設定され、通常はdidEnterRegion
とdidExitRegion
が呼び出されます。ロケーションマネージャは決してリリースされません。これはmain threadにあります。私が考えることができるすべての条件をチェックしました。CoreLocation地域の代理人が呼び出されない
-(id) init
{
self = [super init];
if(self) {
NSLog(@"initializing location manager");
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
return self;
}
-(void) startMonitoringRegion
{
BOOL monitoring = NO;
if ([CLLocationManager regionMonitoringAvailable]) {
if ([CLLocationManager regionMonitoringEnabled]) {
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
monitoring = YES;
} else {
NSLog(@"app is not authorized for location monitoring");
}
} else {
NSLog(@"region monitoring is not enabled");
}
} else {
NSLog(@"region monitoring is not available");
}
if(!monitoring) return;
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:locationManager.location.coordinate
radius:50
identifier:@"majorRegion"];
NSLog(@"trying to start monitoring for region %@", region);
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
}
-(void) locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
NSLog(@"region monitoring started");
}
- (void) locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error
{
NSLog(@"region monitoring failed");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"location manager failed");
}
誰もが考えていますか?私はこれを処理することができますが、didEnterRegion
とdidExitRegion
デリゲートメソッドも時々矛盾しているようで、それは私にとって大きな問題です。
編集:私はこの機能を1つのアプリケーションデリゲート内に複製できます。カスタムオブジェクトなどはありません。以下の実装を参照してください。領域は追加され(印刷時に見えます)、didStartMonitoringRegion
は呼び出されません。
@implementation AppDelegate
@synthesize window = _window;
@synthesize locationManager;
-(void) startMonitoringRegion
{
BOOL monitoring = NO;
if ([CLLocationManager regionMonitoringAvailable]) {
if ([CLLocationManager regionMonitoringEnabled]) {
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
monitoring = YES;
} else {
NSLog(@"app is not authorized for location monitoring");
}
} else {
NSLog(@"region monitoring is not enabled");
}
} else {
NSLog(@"region monitoring is not available");
}
if(!monitoring) return;
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:locationManager.location.coordinate
radius:50.
identifier:@"majorRegion"];
NSLog(@"trying to start monitoring for region %@", region);
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
}
-(void) printMonitoredRegions
{
NSLog(@"printing regions:");
for(CLRegion* region in locationManager.monitoredRegions)
NSLog(@"%@", region);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"initializing location manager");
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[self startMonitoringRegion];
[self performSelector:@selector(printMonitoredRegions) withObject:nil afterDelay:2.];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//NSLog(@"location updated");
}
-(void) locationManager:(CLLocationManager*)manager
didStartMonitoringForRegion:(CLRegion*)region
{
NSLog(@"region monitoring started");
}
-(void) locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
NSLog(@"did enter region");
}
-(void) locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
NSLog(@"did exit region");
}
- (void) locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error
{
NSLog(@"region monitoring failed");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"location manager failed");
}
@end
ログイン:
2012-02-21 10:53:50.397 locationtest[64440:f803] initializing location manager
2012-02-21 10:53:50.412 locationtest[64440:f803] trying to start monitoring for region (identifier majorRegion) <LAT,LONG> radius 50.00m
2012-02-21 10:53:52.414 locationtest[64440:f803] printing regions:
2012-02-21 10:53:52.416 locationtest[64440:f803] (identifier majorRegion <LAT,LONG> radius 50.00m
編集2:私はちょうどCLLocationManagerDelegate
プロトコルのiOS implementationがMac implementation若干異なることに気づいた - 特に、マックはdidStartMonitoringRegion
を持っていません。 iOSライブラリの代わりにMacライブラリを誤って使用しているようなことがありますか?
申し訳ありませんが、私はそれを実現し、投稿後に修正しました。それは助けになりませんでした。 – jab
とにかく、私が質問している問題は、領域のエントリ/終了を検出しないということです。なぜなら、 'didStartMonitoringForRegion'デリゲートメソッドは決して呼び出されません(もう1度は呼び出されません。 – jab
更新しました:私はいくつかの方法を行ってきましたが、この実装では、オブジェクトが初期化された直後( 'init'メソッドが完了した直後)に' startMonitoringRegion'が呼び出されます。遅れを加えても助けにならない。 – jab