2012-10-16 27 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不再可見,這將保持活動狀態。