结合使用UILocalNotification和核心位置 [英] using UILocalNotification with core location

查看:72
本文介绍了结合使用UILocalNotification和核心位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,每次在本地JSON文件中发生的事件与其当前位置之间的距离为<时,它都会向用户发送通知. 100米询问他是否参加该活动,当他按是"时,该活动将被标记为已参加.事情是我试图通过使用一些我在网上找到的代码来做到这一点,但是我不确定这是否是正确的方法,无论如何,我在iPhone上对其进行了测试,结果是到达活动地点后发生了什么不断发送不可阻挡的通知,当我尝试按是或否时,实际上没有任何反应,它会继续发送这些通知.谁能为我解释出什么问题了,我对Xcode和Objective-C语言不是很熟悉.我使用的代码如下所示.

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.

在AppDelegate.m

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

      }
  }

在我的DataManager类中:

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

   }

}

推荐答案

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

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.

这篇关于结合使用UILocalNotification和核心位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆