2012-04-05 27 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: 此外,即使您的應用程序在後臺運行或關閉,這也可以工作。