3
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

CLLocationManagerDelegate を実装するクラスに対応する UIView がアクティブな場合にのみ機能します。

ビューを変更した場合、didEnterRegion はトリガーされません。誰でも私を助けることができますか?

私のコードは次のようになります

- (void)enableRegionMonitoring {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    CLLocationCoordinate2D myMonLocation = CLLocationCoordinate2DMake(10.766699790955, 76.650101525879);
    CLRegion *myRegion = [[CLRegion alloc]
                         initCircularRegionWithCenter:myMonLocation
                                               radius:100
                                           identifier:@"MyLoc"];
    //NSLog(@"reg=%@",myRegion); 
    // Start monitoring for our CLRegion using best accuracy
    [locationManager startMonitoringForRegion:myRegion
                              desiredAccuracy:kCLLocationAccuracyBest];
}



- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"Entered Region");

    NSDate *nowx=[NSDate date];


    UILocalNotification *localNotification=[[UILocalNotification alloc]init];
    if (!localNotification)
        return;
    NSDictionary *data = [NSDictionary dictionaryWithObject:@"qw" forKey:@"mykey"];
    [localNotification setUserInfo:data];

    [localNotification setFireDate:nowx];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    NSMutableString *message=[[NSMutableString alloc]init];
    message = @"Local Not Triggered By didEnterRegion";
    [localNotification setAlertBody:[nowx description]];
    [localNotification setAlertAction:@"Open App"];
    [localNotification setHasAction:YES];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
4

1 に答える 1

1

あなたのコードを見ると、ARCを使用していると思います。コントローラー/ビュー階層によっては、別のビューに切り替えるとビューとコントローラーの割り当てが解除される場合があります。これが発生すると、locationManagerも割り当てが解除されます。

CLLocationManager コード全体を AppDelegate に移動し、AppDelegate を CLLocationManager デリゲートにするだけです。現在「enableRegionMonitoring」を呼び出している場所では、代わりに AppDelegate で呼び出します。ViewController が表示されなくなっても、これはアクティブなままです。

于 2013-02-13T19:57:31.370 に答える