1

我正在开发必须使用区域监控的应用程序。
当用户的位置根据该位置发生变化时,即使应用程序处于后台,用户也会收到有关用户现在在新位置的位置的通知。

我从最近几天开始尝试并在谷歌中搜索,但我没有找到解决方案或任何想法。
有人可以帮助我吗?
提前致谢。

4

1 回答 1

3

在 的帮助下使用地理围栏CLLocationManager并调用startMonitoringForRegion

制作这样的功能并开始监视区域

-(void)notificationForRegion:(CLLocationDegrees) latitude withLongitude:(CLLocationDegrees) longitude andIdentifier : (NSString*) identifer {
    CLLocationManager * locationManager = [[CLLocationManager alloc] init];
    CLCircularRegion * regionForMonitoring = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(latitude, longitude) radius:50.0 identifier:identifer];
    regionForMonitoring.notifyOnExit = TRUE;
    [locationManager startMonitoringForRegion:regionForMonitoring];
}

实现in 的didEnterRegion委托方法, CLLocationManagerDelegate因为AppDelegate对象AppDelegate始终保留在内存中,并在此处安排本地通知。

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    UILocalNotification * localNotif = [[UILocalNotification alloc] init];
    localNotif.alertBody = @"You are now in the region";
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [manager stopMonitoringForRegion:region];  
}

调用上述notificationForRegion函数如下

拿到你的后location

[self notificationForRegion:location.coordinate.latitude withLongitude:location.coordinate.longitude andIdentifier:@"your identifier"];
于 2016-10-10T05:25:56.233 回答