This is the code I have and I it puts the date in a application badge. I was wondering how to reload this number everyday without entering the app to reload it. Thanks in advance!
NSDate *today = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comp = [cal components:NSDayCalendarUnit fromDate:today];
NSInteger day = [comp day];
[UIApplication sharedApplication].applicationIconBadgeNumber = day;
A relatively easy way would be to put the code that sets the badge in the background. You can use [UIApplication beginBackgroundTaskWithExpirationHandler] or a related call for this purpose. The only problem is that there's a 10 minute limit for such execution. Having said that, there are tricks that can be used to circumvent that limit. Here is an excellent thread that discusses this topic: Run app for more than 10 minutes in background
I have following code :
NSDate *fireTime = [[NSDate date] addTimeInterval:3600*24];
notification.fireDate = fireTime;
notification.alertBody = #"Your Message!";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
If you are testing on simulator then probably that is the reason why you are not getting the output. Test it on device and see whether is it working .
Related
I wish to know if there is any way to find next minute UTC time without using internet connection. I have to perform an action (play song at a time on multiple devices without lag) at a given point of time.
Since NSDate is always in UTC, you could:
NSDateComponents *minuteComponent = [[NSDateComponents alloc] init];
minuteComponent.minute = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextMinuteDate = [theCalendar dateByAddingComponents:minuteComponent toDate:[NSDate date] options:0];
NSLog(#"%#",nextMinuteDate);
I looked at many pieces of codes but didn't get solution yet,
I simply need to get my app icon badge updated daily with some calendar (not gregorian) number of the day.
How can I do that?
I don't know how you would code it, but if you were going to submit such an app to the app store, apple wouldn't approve it. Apple's strict review guidelines can be frustrating, and like in this case, they limit functionality of your apps. Sorry :(
You obviously can't use repeating local notifications, because you want to specify an application badge number. Therefore you have to use one local notification for each day scheduled at midnight and with the appropriate badge number.
Because you can only schedule a maximum of 64 local notifications, you have to queue the notifications at each application launch.
This code isn't tested, there might be problems with daylight saving times, etc. (Works on iOS 4.2 or later, using 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];
}
I am trying to show Today's date with the application badge number, this date should be updated when the application fires a Local Notification, but the problem is the local notification does not update the date ! and shows only date of the day on which my project was created ! here is my code :
- (void) viewDidLoad {
[self notification];
}
- (void) notification {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: now];
[componentsForFireDate year];
[componentsForFireDate month];
[componentsForFireDate day];
[componentsForFireDate setHour:1];
[componentsForFireDate setMinute:2];
[componentsForFireDate setSecond:1];
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone]; notification.repeatInterval= NSDayCalendarUnit;
NSString *date = [self showGregorianFullDate];
notification.alertAction = #"View";
notification.soundName = UILocalNotificationDefaultSoundName;
//updating badge number :
NSCalendar* Calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *Components = [Calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )fromDate:[NSDate date]];
int a = [Components day];
notification.applicationIconBadgeNumber = a;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Do you only schedule this one notification? When notifications fire they don't get to run any code. If your app is open you can handle the notification, and if the user takes action on your notification you are again given a chance to handle it.
This notification is programmed to set the badge number to a certain day number. That number is a fixed number. It won't change automatically when the notification has been fired.
The application badge is designed to show a number of unhandled notifications (as per the Human Interface Guidelines) and so may not be the best place to show a date. Also if you look at the app store review guidelines any app which uses system provided items in a way not described in the Human interface Guidelines could be rejected from the app store.
If you continue down this path then you may want to look at the Local Notification Programming Guide. It shows each app can have 64 local notifications scheduled, and that you would need to schedule one every day to update the badge number to the next day. This means that if the user doesn't open your app for 65 days the badge number will be wrong, and you would also have no local notifications left for user alerts.
Just get the day from componentsForFireDate for your notification.applicationIconBadgeNumber, just like this...
//updating badge number :
int a = [componentsForFireDate day];
notification.applicationIconBadgeNumber = a;
The thing is that applicationIconBadgeNumber has to be pre-determined at the time you create the notification. If you get the day from [NSDate date], it will show the day you create the notification not the day that the notification fires.
I can't get my local notifications to show. I am testing on the iphone. They do not show up at all and the date generated seems to be an hour before the one entered. 19 becomes 18 and so on.
How do I enable these local notifications at 7 while taking into account the user's time zone?
NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour: 19];
[components setMinute:00];
NSDate *todayAt7 = [myCalendar dateFromComponents:components];
UILocalNotification *dailyNotification = [[UILocalNotification alloc] init];
dailyNotification.fireDate = todayAt7; // set this to 7pm
dailyNotification.timeZone = [NSTimeZone defaultTimeZone];
dailyNotification.repeatInterval = NSDayCalendarUnit;
dailyNotification.soundName = UILocalNotificationDefaultSoundName;
dailyNotification.alertBody = #"You need to enter data for today.";
The first issue is that when you are creating he components instance, you are not asking for the hour and minutes components. Also, you should use NSCalendar *myCalendar = [NSCalendar autoupdatingCurrentCalendar]; instead of currentCalendar.
Second issue, at least your not showing the code for it, but you're not actually scheduling the notification object. See [UIApplication scheduleLocalNotification:].
This should be really simple!
I have a shop, it opens at 8:30 and closes at 17:00. I want my app to say the shops current open or currently closed.
Whats the best way to store my open_time and close_time? Store them as seconds since the start of the day, i.e. 30600 and 63000?
This make sense, but how do I get the current time right now, in seconds since the begining of today, so I can check if current_time is between open_time and close_time, i.e. open!!
Thanks in advance!
This problem isn't quite as trivial as you may think. You have to work with dates very carefully. The best solution is to store all of your open and close times as dates. Here is some code for creating your open/close times and comparing them:
NSDate * now = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * comps = [calendar components:~(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
[comps setHour:8];
[comps setMinute:30];
NSDate * open = [calendar dateFromComponents:comps];
[comps setHour:17];
[comps setMinute:0];
NSDate * close = [calendar dateFromComponents:comps];
if ([now compare:open] == NSOrderedDescending && [now compare:close] == NSOrderedAscending) {
// The date is within the shop's hours.
}
else {
// The date is not within the shop's hours.
}
Here's what I did:
Grab the current date.
Get the components of the date, except hours, minutes, and seconds.
Set the hour and minutes.
Create an open time.
Repeat steps 3-4 for close time.
Compare open and close times to now.
If you ever need to do any modification of dates, you should always use NSCalendar and NSDateComponents. Check out this answer for why it's so important.
I think a clearer solution would be to use NSDate objects with only hour/minute components present.
Basically, somewhere in your app you need to store the shop's open/close times as such:
NSCalendar *calendar = [[NSCalendar alloc]
initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *openTime = [[NSDateComponents alloc] init];
[openTime setHour: 12];
[openTime setMinute: 30];
NSDate *openDate = [calendar dateFromComponents: openTime];
[calendar release];
And if you need to see whether the current time is between two such NSDate objects you could have a method like this:
- (BOOL)currentTimeIsInBetween: (NSDate *)date1 andDate: (NSDate *)date2 {
NSCalendar *calendar = [[NSCalendar alloc]
initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *currentComponents = [calendar components:
(NSMinuteCalendarUnit | NSHourCalendarUnit)
fromDate: [NSDate date]];
NSDate *currentAdjusted = [calendar dateFromComponents: currentComponents];
[calendar release];
if ([currentAdjusted compare: date1] == NSOrderedAscending)
return NO;
if ([currentAdjusted compare: date2] == NSOrderedDescending)
return NO;
return YES;
}
EDIT: Seems like user rbrown was a bit faster than me, we are suggesting the same approach.
You can do something like this.
NSDate *today = // code for getting today date at 0 oclock
NSDate *now = [NSDate date];
double second = [now timeIntervalSinceDate:today];
Now you got time in second since the start of the day for compare.