【问题标题】:using UILocalNotification with core location将 UILocalNotification 与核心位置一起使用
【发布时间】:2014-04-29 14:10:04
【问题描述】:

我有这段代码,每次本地 JSON 文件中发生的事件与他当前位置之间的距离小于 100 米时,我都会发送用户通知,询问他是否在该事件中,当他按下是时该活动将被标记为已参加。问题是我试图通过使用我在网上找到的一些代码来做到这一点,但我不确定这是否是正确的方法,无论如何我在我的 iPhone 上测试了它,当我到达一个活动地点时发生了什么不断发送不可阻挡的通知,当我尝试按是或否时,实际上什么也没发生,它会继续发送这些通知。谁能帮我解释一下出了什么问题,我对 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];
     }

   }

}

【问题讨论】:

  • @rdelmar 基于你的简历 我想你可能知道上面的代码出了什么问题??你能帮忙吗?很紧急..

标签: ios core-location nsnotifications


【解决方案1】:

根据您设置位置管理器的方式,委托方法 locationManager:didUpdateToLocation:fromLocation: 通常会每秒调用一次并进行位置更新。因此,您的代码一遍又一遍地发布本地通知。您需要跟踪发布通知的时间,以避免重复发布。

【讨论】:

  • 我没有完全理解你[你需要跟踪你何时发布通知]所以你是说上面的代码是错误的吗?那么正确的方法是什么?
  • 他的意思是 locationManager:didUpdateToLocation 可能会在同一个位置被多次调用(这就是 iPhone iOS 的行为方式),因此 scheduleLocalNotification: 也会为同一个位置被多次调用,所以你的代码需要记录它是否已经发布了通知,如果是,则不要再发布一个又一个又一个
  • P.S. locationManager:didUpdateToLocation:fromLocation: 已弃用(弃用意味着它已被其他东西取代),除非您正在为旧版本的 iOS 开发
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多