0

最初のアプリケーションとして、アラーム アプリケーションを作成することにしました。これまでのところ、2 つの NSDate オブジェクトを作成し、通知のために isEqualToDate および UILocalNotifications と比較することを理解しています。設定された日付を現在の日付と継続的に比較するにはどうすればよいですか。ループを作成しますか、または Objective C でより効率的な方法はありますか? バックグラウンドで継続的にチェックするにはどうすればよいですか?

[Objective c を初めて使用します。申し訳ありませんが、よろしくお願いします]

これが私が始めたものです:

NSDate * now = [NSDate date];
NSDate * later = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"];
NSComparisonResult result = [later compare:mile];

NSLog(@"%@", later);
NSLog(@"%@", mile);
4

1 に答える 1

1

ループを作成したり、日付を自分で比較したりする必要はありません。アラームを鳴らす方法の 1 つは、ローカル通知を使用することです。自分でローカル通知をスケジュールするための簡単なサンプル コード:

// Initialize your notification
NSUserNotification *notification = [[NSUserNotification alloc] init];

// Set the title of your notification
[notification setTitle:@"Alarm finished!"];

// Set the text of your notification
[notification setInformativeText:@"My Text"];

// Set the time and date on which the notification will be delivered (in this case 60 seconds after the current time)
[notification setDeliveryDate:[NSDate dateWithTimeInterval:60 sinceDate:[NSDate date]]];

// Set the sound, this can be either nil for no sound, NSUserNotificationDefaultSoundName for the default sound)
[notification setSoundName:NSUserNotificationDefaultSoundName];

// Schedule the notification
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];

ローカル通知を使用したくないが、アラームが終了したら別のことを実行したい場合は、 を使用NSTimerして、アラームが発生したときにアクションを実行できます。

于 2013-10-24T21:46:09.720 に答える