2012-04-05 81 views
6

即时通讯目前与“地区”的示例代码工作: https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.h TML#// apple_ref/DOC/UID/DTS40010726-介绍,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]; 
} 

可能有人建议我是否该是正确的,还是有任何建议? (道歉为格式不佳)

回答

1

你是正确的,没有比从locationManager发出本地通知更好的方法:didExitRegion: 此外,即使您的应用程序在后台运行或关闭,这也可以工作。