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 回答