zoukankan      html  css  js  c++  java
  • Repeating an iOS local notification

    when I wrote about local notifications one thing that I left out was the ability to schedule a repeating notification. One of the reasons I did not bother to mention the ability to set a repeat interval is that the function is very limited. At least it is with iOS 4.1 at the time of writing. To show what can and can not be done I will modify the original sample app to include the ability to set a repeat interval.

    Setting the repeat interval

    The modified UI for the sample app now looks as below. A segmented control has been added to allow a repeat schedule to specified. The possible options are to repeat every minute, hourly, daily or weekly. If you think this is a limited set of options you will see that this not only down to my limited UI design skills but also because the possible options supported by local notifications are limited.

    Other than modifying the XIB file the code changes to implement the repeat interval are minor. The method that creates the local notification (scheduleNotification) has the following additional lines:

        NSInteger index = [scheduleControl selectedSegmentIndex];
        switch (index) {
            case 1:
                notif.repeatInterval = NSMinuteCalendarUnit;
                break;
            case 2:
                notif.repeatInterval = NSHourCalendarUnit;
                break;
            case 3:
                notif.repeatInterval = NSDayCalendarUnit;
                break;
            case 4:
                notif.repeatInterval = NSWeekCalendarUnit;
                break;
            default:
                notif.repeatInterval = 0;
                break;
        }

     

    The local notification object, which is of type UILocalNotification has a property named repeatInterval which determines the repeat interval for the delivery of the notification. The Apple documentation does not really provide much in the way of explanation or example but the repeatInterval is of type NSCalendarUnit which if you check the definition can take one of the following values:

    • NSEraCalendarUnit
    • NSYearCalendarUnit
    • NSMonthCalendarUnit
    • NSDayCalendarUnit
    • NSHourCalendarUnit
    • NSMinuteCalendarUnit
    • NSSecondCalendarUnit
    • NSWeekCalendarUnit
    • NSWeekdayCalendarUnit
    • NSWeekdayOrdinalCalendarUnit
    • NSQuarterCalendarUnit

    These are mostly self explanatory. If you set the repeatInterval to NSDayCalendarUnit then the notification will repeat every day at the same time as the initial notification. The definition of intervals such as week, month, quarter, etc. is actually dependent on the calendar used. You can specify a different calendar from the current calendar by setting the repeatCalendar property to an alternate NSCalendar object. In practise I am not sure you will ever find a practical reason to set an alternate calendar.

    Limitations

    The main limitation of notifications is that the user interface does not provide any way to cancel a repeating notification. So if you create a local notification with an hourly repeat interval it will repeat every hour until the user launches your app and gives you the chance to cancel it programatically. Note that the notification repeats even if the user selects the close/cancel button in the notification alert dialog.

    The other obvious limitation is that you can only use one of the NSCalendarUnit repeat intervals. So you can have a repeat interval of one minute or one hour or one day but not five minutes or three hours or two days.

    All things considered the ability to set a repeat interval for a local notification is of very limited use as currently implemented. In general I would say the handling of notifications is an area where Apple could make substantial improvements in a future release of iOS. Enhancing the UI to allow the user to view multiple notifications and snooze or cancel a repeating notification would make the feature much more useful. It is interesting to note that the Apple clock app has the ability to snooze an alarm but there is no obvious way to create a similar application with the current version of the public iOS frameworks.

    Example code

    Not much has changed but you can get the updated Xcode project for the example app here.

    http://useyourloaf.com/blog/2010/9/13/repeating-an-ios-local-notification.html

  • 相关阅读:
    docker出现如下错误:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    docker 常用的命令
    postman的使用大全
    plsql连接数据库出现乱码
    mybatis从mapper接口跳转到相应的xml文件的eclipse插件
    PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required
    解决java compiler level does not match the version of the installed java project facet【转载】
    使用maven时出现Failure to transfer 错误的解决方法
    QT5 网络通讯
    c/c++ 贪吃蛇控制台版
  • 原文地址:https://www.cnblogs.com/zhw511006/p/2081474.html
Copyright © 2011-2022 走看看