私は時間ベースのリマインダーアプリに取り組んでいます。ユーザーがリマインダーとリマインダーの時間を入力する場所。問題は、現在の時刻をユーザー定義の時刻と継続的に比較する方法です。サンプルコードは非常に役立ちます。私はこの点で立ち往生しているからです。
9566 次
2 に答える
15
現在の時刻とユーザー定義の時刻を比較することは、正しいデザインパターンではありません。
UIKitは、タスクのより高レベルの抽象化であるNSLocalNotificationオブジェクトを提供します。
以下は、選択した時間にローカル通知を作成してスケジュールするコードの一部です。
UILocalNotification *aNotification = [[UILocalNotification alloc] init];
aNotification.fireDate = [NSDate date];
aNotification.timeZone = [NSTimeZone defaultTimeZone];
aNotification.alertBody = @"Notification triggered";
aNotification.alertAction = @"Details";
/* if you wish to pass additional parameters and arguments, you can fill an info dictionary and set it as userInfo property */
//NSDictionary *infoDict = //fill it with a reference to an istance of NSDictionary;
//aNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:aNotification];
[aNotification release];
また、起動時とアプリの通常の実行時の両方で、ローカル通知に応答するようにAppDelegateを設定してください(アプリケーションがフォアグラウンドにある場合でも通知を受け取りたい場合)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *aNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
if (aNotification) {
//if we're here, than we have a local notification. Add the code to display it to the user
}
//...
//your applicationDidFinishLaunchingWithOptions code goes here
//...
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//if we're here, than we have a local notification. Add the code to display it to the user
}
詳細については、AppleDeveloperDocumentationを参照してください。
于 2012-01-17T10:13:35.573 に答える