2012-06-04 76 views
2

我看着代碼的許多作品,但沒有得到解決呢,Xcode中:更新圖標徽章每天

我只是需要讓我的應用程序圖標徽章一天的某些日曆(公曆沒有)號碼每日更新。

我該怎麼做?

+0

有什麼想法嗎? – hafedh

回答

0

我不知道如何編寫代碼,但如果您要將這樣的應用程序提交到應用程序商店,蘋果將不會批准它。蘋果嚴格的審查指南可能令人沮喪,就像在這種情況下,它們限制了你的應用程序的功能。對不起:(

+0

這是我的應用程序的一小部分,不用擔心,已準備好所有類似的應用程序批准。 – hafedh

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]; 
}