Ios 位置管理器&x2B;本地通知
我有位置经理。用户可以创建任务并选择位置。当用户到达位置时,我显示本地通知。它只适用于一项任务。当我有多个任务时,会出现一些奇怪的情况:我收到许多通知。我收到所有预定的通知 问题:如何管理location Manager并为不同地区推送本地通知?我有三个地区——一个在莫斯科,一个在伦敦,一个在纽约。所以,当用户来到莫斯科时,我只希望看到一个通知(“通知1”)。在伦敦时-通知2。在纽约时-通知3 这是我的代码:Ios 位置管理器&x2B;本地通知,ios,objective-c,navigation,notifications,cllocationmanager,Ios,Objective C,Navigation,Notifications,Cllocationmanager,我有位置经理。用户可以创建任务并选择位置。当用户到达位置时,我显示本地通知。它只适用于一项任务。当我有多个任务时,会出现一些奇怪的情况:我收到许多通知。我收到所有预定的通知 问题:如何管理location Manager并为不同地区推送本地通知?我有三个地区——一个在莫斯科,一个在伦敦,一个在纽约。所以,当用户来到莫斯科时,我只希望看到一个通知(“通知1”)。在伦敦时-通知2。在纽约时-通知3 这是我的代码: // // AddTaskViewController.m // TaskMana
//
// AddTaskViewController.m
// TaskManager
//
// Created by Vladyslav Semenchenko on 2/10/14.
// Copyright (c) 2014 Vladyslav Semenchenko. All rights reserved.
//
#import "AddTaskViewController.h"
@interface AddTaskViewController ()
@end
@implementation AddTaskViewController
#pragma mark - Defaul View life cycle methods
- (void)viewDidLoad
{
[super viewDidLoad];
// Positioning user location to center of mapView
userRegionPosition = MKCoordinateRegionMakeWithDistance([self.mapView userLocation].coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:userRegionPosition] animated:YES];
// Set up delegates
self.textField.delegate = self;
self.mapView.delegate = self;
[self addGestureRecogniserToMapView];
// Add Location Manager
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager stopMonitoringSignificantLocationChanges];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
NSLog(@"Monitored regions: %@", self.locationManager.monitoredRegions);
}
-(void)viewDidAppear:(BOOL)animated
{
// Positioning user location to center of mapView
userRegionPosition = MKCoordinateRegionMakeWithDistance([self.mapView userLocation].coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:userRegionPosition] animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"createTask"])
{
NSDictionary *newTask = [[NSDictionary alloc] initWithObjectsAndKeys:[self.textField text], @"Task text", nil];
[TasksIO addTasksToFile:newTask];
// Add Location Manager
if (self.enableLocationMonitoring.isOn){
taskRegion = [[CLCircularRegion alloc] initWithCenter:taskPlace.coordinate radius:1 identifier:[self.textField text]];
[self.locationManager startMonitoringForRegion:taskRegion];
}
}
}
#pragma mark - MapView methods
- (void)addGestureRecogniserToMapView{
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(addPinToMap:)];
lpgr.minimumPressDuration = 0.5; //
[self.mapView addGestureRecognizer:lpgr];
}
- (void)addPinToMap:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
// Add annotation
NSArray *existingAnnotations = self.mapView.annotations;
[self.mapView removeAnnotations:existingAnnotations];
taskPlace = [[MKPointAnnotation alloc]init];
taskPlace.coordinate = touchMapCoordinate;
taskPlace.title = [self.textField text];
longitude = taskPlace.coordinate.longitude;
latitude = taskPlace.coordinate.latitude;
[self.mapView addAnnotation:taskPlace];
}
#pragma mark - TextField delegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.textField) {
[textField resignFirstResponder];
}
return NO;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if(state == CLRegionStateInside)
{
UILocalNotification* localNotificationInside = [[UILocalNotification alloc] init];
localNotificationInside.fireDate = [NSDate date];
localNotificationInside.alertBody = @"It's time to do some work here!";
localNotificationInside.alertAction = @"Open App to view tasks..";
localNotificationInside.soundName = UILocalNotificationDefaultSoundName;
localNotificationInside.timeZone = [NSTimeZone defaultTimeZone];
localNotificationInside.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotificationInside];
}
else if(state == CLRegionStateOutside)
{
}
else{
}
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
/*UILocalNotification* localNotificationInside = [[UILocalNotification alloc] init];
localNotificationInside.fireDate = [NSDate date];
//localNotificationInside.alertBody = @"It's time to do some work here!";
localNotificationInside.alertBody = self.textField.text;
localNotificationInside.alertAction = @"Open App to view tasks..";
localNotificationInside.soundName = UILocalNotificationDefaultSoundName;
localNotificationInside.timeZone = [NSTimeZone defaultTimeZone];
localNotificationInside.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotificationInside];*/
}
#pragma mark - Helpers
-(void) showMessage:(NSString *) message
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Debug info"
message:message
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:Nil, nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show];
}
- (IBAction)clearMonitoredLocations:(id)sender {
for (CLRegion *monitored in [self.locationManager monitoredRegions])
[self.locationManager stopMonitoringForRegion:monitored];
}
@end
您添加到监视器中的每个区域都有一个标识符,您可以使用它来标识输入的区域。是的,我理解这一点。但我不知道该怎么做。请发送示例代码。我不明白,为什么我的应用程序在用户进入区域时发送多个通知:(我认为当我收到下一个本地通知时,我必须取消上一个通知。因为我随机收到许多通知-目前我有2个通知,但收到5个:(