Iphone xcode:每天更新图标徽章
我查看了很多代码,但还没有找到解决方案 我只需要让我的应用图标徽章每天更新一些日历(不是公历)的日期Iphone xcode:每天更新图标徽章,iphone,xcode,calendar,uilocalnotification,Iphone,Xcode,Calendar,Uilocalnotification,我查看了很多代码,但还没有找到解决方案 我只需要让我的应用图标徽章每天更新一些日历(不是公历)的日期 我该怎么做呢?我不知道你会如何编码,但如果你打算向应用商店提交这样的应用程序,苹果不会批准。苹果严格的审查准则可能会令人沮丧,就像在本例中一样,它们限制了应用程序的功能。抱歉:(我不知道你会如何编码,但如果你打算向应用商店提交这样的应用程序,苹果不会批准。苹果的严格审查准则可能会令人沮丧,就像在这种情况下,它们限制了你的应用程序的功能。抱歉:(显然,您不能使用重复的本地通知,因为您要指定一个应用
我该怎么做呢?我不知道你会如何编码,但如果你打算向应用商店提交这样的应用程序,苹果不会批准。苹果严格的审查准则可能会令人沮丧,就像在本例中一样,它们限制了应用程序的功能。抱歉:(我不知道你会如何编码,但如果你打算向应用商店提交这样的应用程序,苹果不会批准。苹果的严格审查准则可能会令人沮丧,就像在这种情况下,它们限制了你的应用程序的功能。抱歉:(显然,您不能使用重复的本地通知,因为您要指定一个应用程序标识号。因此,您必须在每天午夜使用一个具有相应标识号的本地通知 因为您最多只能安排64个本地通知,所以必须在每次应用程序启动时对通知进行排队 此代码未经测试,可能存在夏令时等问题(适用于iOS 4.2或更高版本,使用ARC)
-(无效)应用程序IDBECOMEACTIVE:(UIApplication*)应用程序{
NSInteger startingDayAfterToday=[application.ScheduledLocalNotification计数];
NSArray*localNotifications=[自localNotificationsStartingOnDayAfterToday:startingDayAfterToday];
NSArray*newScheduledNotifications=[application.scheduledLocalNotifications arrayByAddingObjectsFromArray:localNotifications];
[应用程序集ScheduledLocalNotifications:NewsScheduledNotifications];
}
-(NSArray*)本地通知从今天开始到今天之后:(NSInteger)从今天之后开始{
NSMutableArray*localNotifications=[[NSMutableArray alloc]initWithCapacity:64-startingDayAfterToday];
对于(i=startingDayAfterToday;i<64;i++){
//创建新的本地通知
UILocalNotification*通知=[[UILocalNotification alloc]init];
notification.hasAction=否;
//创建今天的午夜日期
NSCalendar*calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];//也可以是其他日历
NSDateComponents*todayDateComponents=[日历组件:(NSRacalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)fromDate:[NSDate日期]];
NSDate*todayMidnight=[calendar dateFromComponents:todayDateComponents];
//创建火灾日期
NSDateComponents*AddedDateySComponents=[[NSDateComponents alloc]init];
addedDaysComponents.day=i;
NSDate*fireDate=[日历日期由AddingComponents:addedDaysComponents toDate:todayMidnight选项:0];
//设置火灾日期和时区
notification.fireDate=fireDate;
notification.timeZone=[NSTimeZone systemTimeZone];
//设置徽章号码
NSDateComponents*fireDateComponents=[日历组件:NSDayCalendarUnit fromDate:fireDate];
notification.applicationBadgeNumber=fireDateComponents.day;
//完成后,将通知添加到数组中
[LocalNotificationsAddObject:notification];
}
返回[本地通知副本];
}
显然,您不能使用重复的本地通知,因为您要指定一个应用程序标识号。因此,您必须在每天午夜使用一个具有相应标识号的本地通知
因为您最多只能安排64个本地通知,所以必须在每次应用程序启动时对通知进行排队
此代码未经测试,可能存在夏令时等问题(适用于iOS 4.2或更高版本,使用ARC)
-(无效)应用程序IDBECOMEACTIVE:(UIApplication*)应用程序{
NSInteger startingDayAfterToday=[application.ScheduledLocalNotification计数];
NSArray*localNotifications=[自localNotificationsStartingOnDayAfterToday:startingDayAfterToday];
NSArray*newScheduledNotifications=[application.scheduledLocalNotifications arrayByAddingObjectsFromArray:localNotifications];
[应用程序集ScheduledLocalNotifications:NewsScheduledNotifications];
}
-(NSArray*)本地通知从今天开始到今天之后:(NSInteger)从今天之后开始{
NSMutableArray*localNotifications=[[NSMutableArray alloc]initWithCapacity:64-startingDayAfterToday];
对于(i=startingDayAfterToday;i<64;i++){
//创建新的本地通知
UILocalNotification*通知=[[UILocalNotification alloc]init];
notification.hasAction=否;
//创建今天的午夜日期
NSCalendar*calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];//也可以是其他日历
NSDateComponents*todayDateComponents=[日历组件:(NSRacalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)fromDate:[NSDate日期]];
NSDate*todayMidnight=[calendar dateFromComponents:todayDateComponents];
//创建火灾日期
NSDateComponents*AddedDateySComponents=[[NSDateComponents alloc]init];
addedDaysComponents.day=i;
NSDate*fireDate=[日历日期由AddingComponents:addedDaysComponents toDate:todayMidnight选项:0];
//设置火灾日期和时区
notification.fireDate=fireDate;
notification.timeZone=[NSTimeZone systemTimeZone];
//设置徽章号码
NSDateComponents*fireDateComponents=[日历组件:NSDayCalendarUnit fromDate:fireDate];
notification.applicationBadgeNumber=fireDateComponents.day;
//完成后,将通知添加到数组中
[LocalNotificationsAddObject:notification];
}
返回[本地通知副本];
}
- (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];
}