xcode:每日更新图标徽章

时间:2012-06-04 11:09:13

标签: iphone xcode calendar uilocalnotification

我查看了很多代码,但还没有得到解决方案,

我只需要每天更新我的应用图标徽章,并使用当天的某些日历(非格里高利)编号。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我不知道你会如何编码,但如果你要将这样的应用程序提交到应用程序商店,苹果就不会批准它。 Apple严格的审核指南可能令人沮丧,就像在这种情况下,它们会限制您应用的功能。对不起:(

答案 1 :(得分:0)

您显然无法使用重复的本地通知,因为您要指定应用程序徽章编号。因此,您必须在午夜安排的每一天使用一个本地通知,并使用相应的徽章编号。

由于您最多只能安排64个本地通知,因此您必须在每次启动应用程序时对通知进行排队。

此代码未经过测试,夏令时可能存在问题等。(适用于iOS 4.2或更高版本,使用ARC)

- (void) applicationDidBecomeActive:(UIApplication *)application {
    NSUInteger startingDayAfterToday = [application.scheduledLocalNotifications count];
    NSArray *localNotifications = [self localNotificationsStartingOnDayAfterToday:startingDayAfterToday];
    NSArray *newScheduledNotifications = [application.scheduledLocalNotifications arrayByAddingObjectsFromArray:localNotifications];
    [application setScheduledLocalNotifications:newScheduledNotifications];
}

- (NSArray *) localNotificationsStartingOnDayAfterToday:(NSUInteger)startingDayAfterToday {
    NSMutableArray *localNotifications = [[NSMutableArray alloc] initWithCapacity:64 - startingDayAfterToday];
    for (NSUInteger i = startingDayAfterToday; i < 64; i++) {
        // Create a new local notification
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.hasAction = NO;

        // Create today's midnight date
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // Could be other calendar, too
        NSDateComponents *todayDateComponents = [calendar components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
        NSDate *todayMidnight = [calendar dateFromComponents:todayDateComponents];

        // Create the fire date
        NSDateComponents *addedDaysComponents = [[NSDateComponents alloc] init];
        addedDaysComponents.day = i;
        NSDate *fireDate = [calendar dateByAddingComponents:addedDaysComponents toDate:todayMidnight options:0];

        // Set the fire date and time zone
        notification.fireDate = fireDate;
        notification.timeZone = [NSTimeZone systemTimeZone];

        // Set the badge number
        NSDateComponents *fireDateComponents = [calendar components:NSDayCalendarUnit fromDate:fireDate];
        notification.applicationIconBadgeNumber = fireDateComponents.day;

        // We're done, add the notification to the array
        [localNotifications addObject:notification];
    }

    return [localNotifications copy];
}