문제

I have location Manager. User can create tasks and pick distinction location. When user come to location - I show local notification. It works for one task. When I have more that one task some strange appear: I receive many notifications. I receive all scheduled notifications.

Question: how can I manage location Manager and push local notifications for different regions? Example: I have three regions - one in Moscow, one in London and one in New York. So, when user come to Moscow I want see only one notification (@"Notification 1"). When in London - notification 2. When in New York - notification 3.

Here my code:

//
//  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
도움이 되었습니까?

해결책

Each CLRegion you add to monitor has an identifier, you can use this to identify which region is entered.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top