6

現在、「リージョン」サンプルコードを使用しています: https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.h tml#//apple_ref/doc/uid/DTS40010726-Intro-DontLinkElementID_2

私はそれをさらに一歩進めて、ユーザーが領域を出るときに通知を生成または起動したいと思います(最初の実装で最も簡単なものは何でも構いません)。

CLLocation Class reference、Location Awareness Programming Guide、および Local and Push Notification Programming guide を見てきました。そして、私は情報過多に苦しんでいます。

どうもありがとう :)

編集:私は問題を解決するアイデアを持っていると思います: 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];
}

これが正しいかどうか、または推奨事項があるかどうかについて、誰かにアドバイスしてもらえますか? (書式が悪くてすみません)

4

1 に答える 1

1

その通りです。locationManager:didExitRegion: からローカル通知を送信するよりも良い方法はありません。さらに、アプリがバックグラウンドで実行されている場合や閉じている場合でも、これは機能します。

于 2012-11-19T09:21:17.563 に答える