我目前正在使用“区域”示例代码: https ://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.h tml#//apple_ref/doc/uid/DTS40010726-Intro-DontLinkElementID_2
我想更进一步,并在用户退出该区域时生成或触发通知(可以用于进入和退出,我不介意,对于初始实现来说是最简单的)。
我一直在查看 CLLocation 类参考、位置感知编程指南和本地和推送通知编程指南。我正遭受信息过载的困扰。
非常感谢 :)
编辑:我想我可能有一个解决问题的想法:在 RegionsViewController 实现文件中有这个:
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];
[self updateWithEvent:event];
}
因为我想在用户退出指定区域边界时实现本地通知,所以我输入了这个:
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];
[self updateWithEvent:event];
//implement local notification:
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (notification == nil)
return;
notification.alertBody = [NSString stringWithFormat:@"Did You Lock Your House?"];
notification.alertAction = @"Lock House";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[app presentLocalNotificationNow:notification];
[notification release];
}
有人可以建议我这是否正确,或者是否有任何建议?(为糟糕的格式道歉)