How to set an alarm in iOS?

I know this question has been asked about StackOverflow many times, but I couldn’t set the alarm in my application because I am very new to iOS? I follow this guide to set an alarm:

Set reminders using UILocalNotification in iOS.

However, this does not seem to work for me.

I need to set an alarm daily, say 5.00 PM daily. I cannot use date picker to select time.

+6
source share
4 answers
  • First, on your xib , (or code) set the date selection mode: Time (by default - date and time)

  • The system assumes that the start date is the current date, and the time is the time that the user selected. This is not a problem because you set the repeat interval to make it work. I tested it.

     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; [localNotif setFireDate:datePicker.date]; [localNotif setRepeatInterval:NSDayCalendarUnit]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

PS: It would be nice to set the seconds to 0 using the NSDateComponents class to set the alarm for the first second of the minute you want. You can check:

Local notifications in iOS.

The tutorial you posted on how to do this.

+9
source
 + (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID 

Call this method with parameters and use this

  + (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]; } 
+1
source

You may need to change the date picker style to change the time in addition to the date.

0
source

You can try this

 UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init]; [todolistLocalNotification setFireDate:[lodatepicker date]]; [todolistLocalNotification setAlertAction:@"Note list"]; [todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]]; [todolistLocalNotification setAlertBody:text_todolist]; [todolistLocalNotification setHasAction:YES]; 
0
source

Source: https://habr.com/ru/post/922298/


All Articles