Use UILocalNotification to implement an alarm (Swift) and uilocalnotification

Source: Internet
Author: User

Use UILocalNotification to implement an alarm (Swift) and uilocalnotification

Previously, the project needed to implement an alert clock. I found it was old code on github for a long time, so I prepared to write one myself, just recently learning Swift, I used Swift to write a demo here: https://github.com/Phelthas/Demo_AlarmClock effect to sum up, the memo interface if a DatePicker plus 7 UIButton will be done, a little problem is how to indicate which days selected, days are not selected. Here we can use arrays or collections, but the simplest is to use a hexadecimal number (which is also shown on github, I used to write a large lump of data using arrays ...) 0x111 1111 indicates all selected items, 0x000 0000 indicates all selected items, and 1 indicates a day selected. Then, the shift operator can be used to calculate the selected of the button to indicate whether the button has been selected or not. Then, it can be implemented using UILocalNotification. It should be noted that the repeat interval of UILocalNotification can only be the value defined by Apple (NSCalendarUnit), and the interval cannot be too small (seconds are not used ), the number of uilocalnotifications that can be set for each app is limited (it seems to be 128). Therefore, a notification here can only make the same time of the X clock every week. If you want an alarm to ring on Monday and Tuesday, we need to define two uilocalnotifications. If the alarm is triggered on a 7-day basis, we need seven notifications... At present, I really don't know any other method. If you know something, I hope you can tell me how many things you want to know in the week, you need to calculate it based on NSCalendar, using dateComponents, note that Apple defines weekday as 1 (that is, Sunday is the first day of a week) on Sunday, and we generally use Monday as the first day of a week, therefore, you need to convert the calculation. For the conversion method, see "userInfo" for the notification when adding notifications in demo. Set the notification identifier as the unique identifier of the notification, otherwise, it cannot be canceled. Here, the time stamp of the notification time is used as a string to serve as the identifier of the notification.

 

 

I plan to write at least one blog article every month in 2016. The first blog article will end at the end of the month... Try again later!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.