我有这个代码,每当本地JSON文件中发生的事件与其当前位置之间的距离<1时,就会发送用户通知。 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];
}
}
}
答案 0 :(得分:0)
根据您设置位置管理器的方式,通常会通过位置更新每秒调用一次委托方法locationManager:didUpdateToLocation:fromLocation:
。所以你的代码一遍又一遍地发布本地通知。您需要跟踪发布通知的时间,以避免发布重复项。