using UILocalNotification with core location

谁都会走 提交于 2019-12-24 17:13:00

问题


I have this code which send user notifications every time the distance between an event that occur in a local JSON file and his current location is < 100 meter asking him whether he is at that event or not , when he presses on yes then that event will be marked as attended. the thing is I tried to do that by using some code i found online but I'm not sure if it is the right way to do it, anyway i tested it on my iPhone and what happened is when i arrived to an event location it kept sending unstoppable notifications and when i try to press yes or no nothing actually happen it keeps sending these notifications. Can anyone plz explain for me what is going wrong, I'm not very familiar with Xcode and objective-C language. The code i used is shown below.

in AppDelegate.m

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
           {
             // load Core Data
              NSManagedObjectContext *context = [self managedObjectContext];
              if (!context) {
                  NSLog(@"No NSManagedObjectContext generated");
               }
               NSLog(@"DelegateApp Managed Object Context = %@", context);
              [[DataManager sharedInstance] setManagedObjectContext:context];
              [[DataManager sharedInstance] initDataBase];
              return YES;

          UILocalNotification *notification = [launchOptions   objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

       if (notification) {
           [self showAlarm:notification.alertBody];
           NSLog(@"AppDelegate didFinishLaunchingWithOptions");
           application.applicationIconBadgeNumber = 0;
       }

      [self.window makeKeyAndVisible];
       return YES;
    }

     - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
         [self showAlarm:notification.alertBody];
         application.applicationIconBadgeNumber = 0;
         NSLog(@"AppDelegate didReceiveLocalNotification %@", notification.userInfo);
      }


    - (void)showAlarm:(NSString *)text {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"SPOT IT"
                                                    message:text delegate:self
                                          cancelButtonTitle:@"YES"
                                          otherButtonTitles:@"NO",nil];

             [alertView show];
     }


    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
       {
           NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

           if([title isEqualToString:@"NO"])
           {
             NSLog(@"Button 2 was selected.");
           }
           else if([title isEqualToString:@"YES"])
           {
            NSLog(@"Button 1 was selected.");

            // attended
           [_eachEvent setHasATTENDED:[NSNumber numberWithBool:TRUE]];
         // save
          NSError *error = nil;
          if (![_managedObjectContext save:&error])
          {
           NSLog(@"Error in saving");
          }

      }
  }

in my DataManager class:

  - (void) locationManager:(CLLocationManager *)manager
 didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation {

//NSLog(@"MV_EventsDataManager new location: latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude,  newLocation.coordinate.longitude);
for (Event *musicevent in [self loadTodaysEvents]) {

    // distance
    CLLocationDegrees lat = [musicevent.lat doubleValue];
    CLLocationDegrees lon = [musicevent.longi doubleValue];
    CLLocation *evLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
    double distance = [evLocation distanceFromLocation:newLocation];
    //NSLog(@"\t Calculated KM %@ to %@", [NSString stringWithFormat:@"%.1f",(distance/1000.0)], musicevent.title);

    // CLOSE !
    if (distance <= 100) {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];

        UILocalNotification *localNotification = [[UILocalNotification alloc] init];

        localNotification.alertBody = @"Are u there!";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.applicationIconBadgeNumber = 1; // increment

        //  NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];
        // localNotification.userInfo = infoDict;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     }

   }

}

回答1:


Depending on how you setup the location manager, the delegate method locationManager:didUpdateToLocation:fromLocation: will typically be called once per second with location updates. So your code is posting local notifications over and over. You need to keep track of when you've posted a notification so you can avoid posting duplicates.



来源:https://stackoverflow.com/questions/23367112/using-uilocalnotification-with-core-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!