2

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

我只需要每天用一些日历(不是公历)数字更新我的应用程序图标徽章。

我怎样才能做到这一点?

4

2 回答 2

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];
}
于 2012-12-11T13:17:19.947 回答
0

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

于 2012-07-11T16:47:19.320 回答