如何在后台使用地理围栏方法

时间:2012-09-09 11:29:00

标签: ios core-location geofencing

我正在尝试制作一个应用程序,当用户进入某个区域时会通知用户。我在应用程序处于活动状态时执行了该操作,但我不知道如何在后台运行它。任何人都可以帮助我吗? 这是我用过的代码:

self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;

locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;


[locationManager startUpdatingLocation];

if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled])
{ 

     loc = CLLocationCoordinate2DMake(30.794686, 31.012309);
     alslamMosque =[[CLRegion alloc]initCircularRegionWithCenter:loc radius:10 identifier:@"alslam"];

     CLLocationAccuracy acc = kCLLocationAccuracyNearestTenMeters;

    [locationManager startMonitoringForRegion:alslamMosque desiredAccuracy:acc];
     [locationManager startMonitoringSignificantLocationChanges];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{ 
    UIAlertView *alr=[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat: @"you've enterd region and you are %f meters from",dd]  
                                            message:region.identifier delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];

    [alr show];

    [alr release];
    NSLog(@"enter region");

}

1 个答案:

答案 0 :(得分:1)

这是堆栈上的直接链接,正是您正在寻找的内容:

Geolocation with local notification like reminder

剪断>

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
notification.fireDate = [NSDate date]; 
NSTimeZone* timezone = [NSTimeZone defaultTimeZone]; 
notification.timeZone = timezone; 
notification.alertBody = @"Notification message"; 
notification.alertAction = @"Show"; 
notification.soundName = UILocalNotificationDefaultSoundName; 
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release]; // release if not using ARC

之后继续并确保您在App Delegate中处理通知