2012-10-16 25 views
3
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region 

仅在与实现CLLocationManagerDelegate的类对应的UIView处于活动状态时才有效。核心位置didEnterRegion没有工作

如果我改变了视图,它不会触发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]; 
} 
+0

你在看什么?因为如果你想引发委托事件,它必须要为它设置委托。 –

+0

已设置代理人 [locationManager setDelegate:self]; 我想在进入地区时触发通知。 – Harikrishnan

+0

请帮助我..我真的卡在它 – Harikrishnan

回答

1

从看你的代码,我猜你正在使用ARC,这取决于你的控制器/视图层次,当您切换到不同的视图您的视图和控制器可能会解除分配,当发生这种情况时,locationManager也将被释放。

只需将整个CLLocationManager代码移动到您的AppDelegate并让AppDelegate成为CLLocationManager代理。你现在调用“enableRegionMonitoring”的地方,你可以在你的AppDelegate上调用它。即使ViewController不再可见,这将保持活动状态。