【发布时间】: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