2014-02-17 37 views
0

我有位置經理。用戶可以創建任務並選擇區別位置。當用戶來到位置 - 我顯示本地通知。它適用於一項任務。當我有更多的一項任務出現一些奇怪的現象時:我收到很多通知。我收到所有預定的通知。位置經理+本地通知

問題:如何管理位置管理器併爲不同地區推送本地通知?例如:我有三個地區 - 一個在莫斯科,一個在倫敦,一個在紐約。所以,當用戶來到莫斯科時,我只想看到一個通知(@「Notification 1」)。當倫敦 - 通知2.當紐約 - 通知3.

這裏我的代碼:

// 
// 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 
+0

我認爲當我收到下一個本地通知時,我必須解除以前的通知。因爲我隨機收到很多通知 - 現在我有2個通知,但收到5 :( –

回答

1

每個CLRegion您添加到監控都有一個標識符,你可以用它來確定進入該區域。

+0

是的,我明白這一點。但我不知道,如何做到這一點。請,你可以發送示例代碼,不明白,爲什麼我的應用程序發送多個通知時,用戶輸入區域:( –