この質問がStackOverflowで何度も聞かれたことは知っていますが、iOSを初めて使用するため、アプリでアラームを設定できませんでしたか?私はこのチュートリアルに従ってアラームを設定しています:
iOSでUILocalNotificationを使用してリマインダーを設定します。
しかし、それは私にとってはうまくいかないようです。
毎日アラームを設定する必要があります5.00 PM
。時刻の選択に日付ピッカーを使用できません。
この質問がStackOverflowで何度も聞かれたことは知っていますが、iOSを初めて使用するため、アプリでアラームを設定できませんでしたか?私はこのチュートリアルに従ってアラームを設定しています:
iOSでUILocalNotificationを使用してリマインダーを設定します。
しかし、それは私にとってはうまくいかないようです。
毎日アラームを設定する必要があります5.00 PM
。時刻の選択に日付ピッカーを使用できません。
まずxib
、(またはコード)で日付ピッカーモードを設定します:時間(デフォルトは日付と時刻)
システムは、発火日が現在の日付であり、時刻がユーザーが選択した時刻であると想定します。繰り返し間隔を設定して機能するため、これは問題ではありません。私はそれをテストしました。
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
PS:希望する分の最初の秒にアラームが鳴るように、クラスを0
使用する秒数を設定することをお勧めします。NSDateComponents
あなたはチェックすることができます:
これを行う方法について投稿したチュートリアル。
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
パラメータを使用してこのメソッドを呼び出し、これを使用します
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hours];
[dateComps setMinute:minutes];
[dateComps setSecond:seconds];
NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;
localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
日付だけでなく時刻も変更できるように、日付ピッカーのスタイルを変更する必要がある場合があります。
あなたはこれを試すことができます
UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:@"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];