Convert string to time xcode - ios

I have a json containing various times. Like below
{
"5:10"
"14:24"
}
I need to use this time to fire local notification everyday.
Can you please suggest me how to properly put this string in this line?
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setHour: 20] ;
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
In this code I am manually adding the time to 8 pm. I need to put that time from json into the code.

Related

Show local notification on specific day and specific time in iOS

i want to show local notification on every saturday like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=#"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
NSLog(#"day: %#", dayString);
if([dayString isEqualToString:#"Saturday"])
{
NSLog(#"Success");
[self PushNotification];
}
-(void)PushNotification
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = #"Test";
localNotification.alertBody =#"test of notification";
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
[componentsForFireDate setWeekday: 7]; //for fixing Saturday
[componentsForFireDate setHour: 17]; //for fixing 5PM hour
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;
}
but my local notification is display in every minit then how can i display notification on every Saturday of the week.
thanks.
Try to declare fireData property in localNotification and schedule your notification
localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Hope this help

iOS create local notification when app exit with 10 days

I am create a local notification with 10 days and at 15:30 when app exit. Here is a code:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
float day = 10;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*day];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: newDate];
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
When I try change date & time in the my phone to the next day (now + 1), It's still show notification, next day, still show. I want local notification show each 10 days when app exit. Any suggests to fix this problems ?
try this code: your app open after 10 day notification fire and other 1 day fire.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsForFireDate = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate: [NSDate date]];
componentsForFireDate.day = componentsForFireDate.day + 10;
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
NSLog(#"Date: %#",fireDateOfNotification);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.alertBody = #"This is local notification!";
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
hope this code help you.
After changing the time in your system, please check the simulator time also. initially it not updated.
In simulator settings, General->language & region-> Advanced-> Automatica switch will be on mode. select off and on. then you simulator time will also change

Send local notification at a certain time of the day

I want to get a random string from an array and then display this everyday at 7am.
Here is the code in my AppDelegate.m
-(void)applicationDidEnterBackground:(UIApplication *)application
{
QuoteTableViewController *quoteVC;
int randomlyGeneratedNumber;
randomlyGeneratedNumber =arc4random()%[quoteVC.arrayOfOurQuotes count];
NSString *stringOfTheDay = [[NSString alloc] init];
stringOfTheDay = [quoteVC.arrayOfOurQuotes objectAtIndex:randomlyGeneratedNumber];
NSLog(#"%#",stringOfTheDay);
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:9];
[componentsForReferenceDate setMonth:11];
[componentsForReferenceDate setYear:2012];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:7];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval = 0;
notification.alertBody = [NSString stringWithFormat:stringOfTheDay];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
There is a error at this line when i run the code
randomlyGeneratedNumber =arc4random()%[quoteVC.arrayOfOurQuotes count];
There is an array in my other View Controller. I did import the view controller.
EDIT :
Array has strings inside but when imported into app delegate it becomes 0. How to fix this?
You need to implement 'arrayOfOurQuotes' as a property of your App delegate instead of the view controller. The view controller and therefore your array will not be around anymore when the view is gone - which is obviously the case if you have already entered the background.

UILocalNotification set repeatCalendar

I am New to NSCalander, NSdates, and NSDateComponents
Basically I have a Local Notification and I want to repeat the fire date based on the selection of the user, let's say on Sunday and Monday only.
We should use repeatCalendar property for the UILocalNotification but I couldn't reach how to setup it.
So any One can help me with simple line of codes ?
Thanks
There is a code snippet to set the UILocalNotification to fire at every sunday 20:00.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
enjoy

Fire a notification at a specific day and time every week

I want to trigger a UILocalNotification every Sunday at 8PM, however, I'm having a fire date every day at 8PM.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);//it indicates that the notif will be triggered today at 8PM and not Sunday.
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
Thank you.
You missed the NSWeekCalendarUnit in the NSDateComponents init function. Add the NSWeekCalendarUnit to it and set the repeatInterval to NSWeekCalendarUnit, then output is
next fire date = Sunday, November 24, 2013 at 8:00:00 PM
The code is here:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
//...
notification.repeatInterval = NSWeekCalendarUnit;
//...
I have also searched about it. Below code work good for me. Pass the week day value 1 to 7 Sunday to Saturday and notification body with action which you want to fire and specify your date then notification will come on that specific day.
Time will automatically set when you set date.
Hope this help you.
-(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
[componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
// [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
// [componentsForFireDate setMinute:0] ;
// [componentsForFireDate setSecond:0] ;
notification.repeatInterval = NSWeekCalendarUnit;
notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

Resources