使用UILocalNotification具有核心位置(using UILocalNotificat

2019-10-20 01:18发布

我有这个代码发送的每发生在一个本地JSON文件,他现在的位置是一个事件之间的距离<百米问他是否是在该事件或没有时间用户通知,当他按下的是,那么该事件将作为出席标记。 事情是我试图通过使用一些代码,我在网上发现这样做,但我不知道这是否是这样做的,反正我测试了它在我的iPhone,并发生了什么事情的正确方法是,当我抵达活动目的地它不停的发送势不可挡的通知,当我尝试按Yes或No没有实际发生但持续发送这些通知。 任何人都可以PLZ给我什么错误解释,我不是很熟悉,Xcode和Objective-C语言。 我使用的代码如下所示。

在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");
          }

      }
  }

在我的DataManager类:

  - (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];
     }

   }

}

Answer 1:

视您设置的位置管理,委托方法locationManager:didUpdateToLocation:fromLocation:通常与位置更新每秒一次调用。 所以,你的代码是遍地张贴本地通知。 你需要跟踪,当你发布一个通知,这样你可以避免重复发帖。



文章来源: using UILocalNotification with core location