2012-02-16 9 views
2

iOS 5のCoreLocation地域デリゲートメソッドに問題があります。シミュレータとデバイスの両方に問題があります。監視のための領域を追加しようとしていて、didStartMonitoring代理コールバックを待っています。まれに、うまく動作します。しかし、通常didStartMonitoringでもmonitoringDidFailも呼ばれません。地域はmonitoredRegionsに追加されます。デリゲートオブジェクトは正しく設定され、通常はdidEnterRegiondidExitRegionが呼び出されます。ロケーションマネージャは決してリリースされません。これは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"); 
} 

誰もが考えていますか?私はこれを処理することができますが、didEnterRegiondidExitRegionデリゲートメソッドも時々矛盾しているようで、それは私にとって大きな問題です。

編集:私はこの機能を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 implementationMac implementation若干異なることに気づいた - 特に、マックはdidStartMonitoringRegionを持っていません。 iOSライブラリの代わりにMacライブラリを誤って使用しているようなことがありますか?

答えて

0

地域の半径が非常に小さいため、正確なピックアップを得る機会はありません。半メートルから10または20のようなものに拡大してそこから作業してみてください。

精度を設定してみることもできますが、半径は私がほとんど確実な主な問題です。

アップデートは

私はちょうどあなたが-startMonitoringRegionではなく-startUpdatingLocatuon呼んでいる気づきました。 CLRegionを構築して監視するためにコードを追加してみてください。

+0

申し訳ありませんが、私はそれを実現し、投稿後に修正しました。それは助けになりませんでした。 – jab

+0

とにかく、私が質問している問題は、領域のエントリ/終了を検出しないということです。なぜなら、 'didStartMonitoringForRegion'デリゲートメソッドは決して呼び出されません(もう1度は呼び出されません。 – jab

+0

更新しました:私はいくつかの方法を行ってきましたが、この実装では、オブジェクトが初期化された直後( 'init'メソッドが完了した直後)に' startMonitoringRegion'が呼び出されます。遅れを加えても助けにならない。 – jab

3

は、Appleが言うことを参照してください:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

iOSシミュレータやデバイス上で、お住まいの地域の監視コードをテストし、その領域のイベントを実現し、アプリケーションの地域モニタリングのサポートをテストするかもしれません領域境界を越えた直後には起こらない。偽の通知を防止するため、iOSは特定のしきい値条件が満たされるまで地域通知を配信しません。具体的には、ユーザの位置は、領域境界を越えて、その境界から最小距離だけ移動し、通知が報告される前に少なくとも20秒間その最小距離にとどまらなければならない。

特定のしきい値の距離は、現在利用可能なハードウェアと位置付け技術によって決まります。たとえば、Wi-Fiが無効になっている場合、地域の監視はかなり正確ではありません。ただし、テスト目的では、最小距離は約200メートルと仮定できます。

+0

これは良いメモですが、問題ではありません。 'didEnterRegion'と' didExitRegion'デリゲートメソッドが呼び出されます(あなたの言うとおりです)。しかし、 'didStartMonitoringForRegion'は、ロケーションマネージャがそのリージョンを明確に監視しているにもかかわらず呼び出されません。 – jab

+0

上記のコメントで説明したのと同じ問題は、ny解決策を見つけましたか? – nidIOS

関連する問題