Multiple UNUserNotifications not firing - ios

I'm setting multiple UNUsernotifications as below,
- (void)viewDidLoad {
[super viewDidLoad];
notifCount = 0;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request succeeded!");
[self set10Notif];
}
}];
}
In the set10Notif method, I'm setting multiple(8 for testing) notifications with time 10 seconds from current time.
-(void) set10Notif
{
notifCount = notifCount+1;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"10.0") && notifCount < 10)
{
// create actions
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];
NSString *fireStr = [self returnStringFromDate:fireD withFormat:#"hh/mm/ss dd/MM/yyyy"];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:fireD];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:#"Notif!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:#"Prayer"
content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
[userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Local Notification succeeded");
}
else {
NSLog(#"Local Notification failed");
}
[self set10Notif];
}];
#endif
}
}
All the local notifications are set. But only one notification got fired in the device, that's the last one.
Why can't the multiple notifications are not firing?
What did I do wrong?

Give a different "requestWithIdentifier" and "time delay" for each notification and try, may it works for you.

MULTIPLE NSUSERNOTIFICATION FIRE ON CUSTOM DATE AND TIME
UNUserNotificationCenter *center=[UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:( UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request authorization succeeded!");
// [self showAlert];
}
}];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:#"શ્રી હરિ જયંતિ!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:#"જય સ્વામિનારાયણ \nઆવતીકાલે એકાદશી છે!"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:#"nirnay" withExtension:#"jpg"];
NSError *error;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:#"nirnay" URL:fileURL options:nil error:&error];
objNotificationContent.attachments = #[attachment];
/// 4. update application icon badge number
// objNotificationContent.badge = #([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
for (int i=0; i<5; i++)
{
int hours=11;
int mint=46+i;
NSString *myDateAsAStringValue=#"2017-03-22";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
// or #"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSDate *myDate = [dateFormatter dateFromString: myDateAsAStringValue];
NSDate *pickerDate = myDate;
NSLog(#"pickerDate - %#",pickerDate);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
// [dateFormatter setDateFormat:DT_FORMATE_BIRTHDATE];
[dateFormatter2 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter2 setTimeZone:gmt];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *timeComponents = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute|NSCalendarUnitSecond)
fromDate:pickerDate];
[timeComponents setHour:hours];
[timeComponents setMinute:mint];
[timeComponents setSecond:0];
NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *fierDate = [formatter stringFromDate:dtFinal];
NSLog(#"fierDate - %#",fierDate);
UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:timeComponents
repeats:NO];
UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:#"ten%d",i]
content:objNotificationContent trigger:trigger3];
/// 3. schedule localNotification
[center addNotificationRequest:request3 withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Local Notification succeeded By calender");
}
else {
NSLog(#"Local Notification failed");
}
}];
}

Related

Getting date from web, returns nil

Doing a check to get the current time and date from Google. The first option works although not the best way to do this as it's using a depreciated method and waiting for everything to finish with the synchronous method is not good UX.
-(NSDate*)timeAndDateFromWeb{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:#"https://google.co.uk"]];
[request setHTTPMethod:#"GET"];
NSHTTPURLResponse *httpResponse = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&httpResponse error:nil];
NSString *dateString = [[httpResponse allHeaderFields] objectForKey:#"Date"];
DebugLog(#" *** GOOGLE DATE: %# ****",dateString);
if (httpResponse){
hasDataConnection = YES;
}
else{
hasDataConnection = NO;
}
// Convert string to date object
NSDateFormatter *dateformatted = [NSDateFormatter new];
[dateformatted setDateFormat:#"E, d MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
[dateformatted setLocale:locale];
return [dateformatted dateFromString:dateString];
}
Trying to adapt it is almost there although I'm returning nil for my date string: [dateformatted dateFromString:dateString];
NSURL *url = [NSURL URLWithString:#"https://google.co.uk"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSHTTPURLResponse *httpResponse = nil;
NSString *dateString = [[httpResponse allHeaderFields] objectForKey:#"Date"];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
hasDataConnection = NO;
//NSLog(#"\n\n ----> Not connected Error,%#", [error localizedDescription]);
}
else {
//NSLog(#"\n\n -----> connected: %#", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
hasDataConnection = YES;
}
}];
// Convert string to date object
NSDateFormatter *dateformatted = [NSDateFormatter new];
[dateformatted setDateFormat:#"E, d MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
[dateformatted setLocale:locale];
DebugLog(#" *** GOOGLE DATE: %# ****",[dateformatted dateFromString:dateString]);
return [dateformatted dateFromString:dateString];
When you're switching from sync to async, can't return value the same way as before.
When you call sendAsynchronousRequest method, it starts a background task, but your thread continues work instantly. That's why both httpResponse and dateString are null.
So instead of that you should change your return type to void, because you can't return result instantly, and add a callback, which will be run when the job is done. And process your formatting in the completion of the task too:
- (void)timeAndDateFromWeb:(void (^)(NSDate *))completion {
NSURL *url = [NSURL URLWithString:#"https://google.co.uk"];
NSURLSessionTask *task = [NSURLSession.sharedSession
dataTaskWithURL:url
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
// hasDataConnection = NO;
//NSLog(#"\n\n ----> Not connected Error,%#", [error localizedDescription]);
}
else if ([response isKindOfClass:NSHTTPURLResponse.class] ) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSString *dateString = [[httpResponse allHeaderFields] objectForKey:#"Date"];
// Convert string to date object
NSDateFormatter *dateformatted = [NSDateFormatter new];
[dateformatted setDateFormat:#"E, d MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
[dateformatted setLocale:locale];
// DebugLog(#" *** GOOGLE DATE: %# ****",[dateformatted dateFromString:dateString]);
completion([dateformatted dateFromString:dateString]);
//NSLog(#"\n\n -----> connected: %#", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
// hasDataConnection = YES;
}
}];
[task resume];
}
And so the result of this method will be returned to the block, same as result of the background task.
Don't forget, that it will be called on the background thread, same as the download task completion block. So if you wanna change some UI you need to move back to the main thread:
[self timeAndDateFromWeb:^(NSDate *date) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"%#", date);
// UI changes
});
}];
Alternatively you can move it to the main thread inside your function, right before returning the result:
NSDate *date = [dateformatted dateFromString:dateString];
dispatch_async(dispatch_get_main_queue(), ^{
completion(date);
});

IOS10.3 localnotification objective-c

I wanted to create a local Notification in IOS 10.3 using objective -c for a certain date of date picker, but it is not working for some reason.
here is my code
-(IBAction)addReminder:(id)sender
{
NSDate *date =self.datePicker.date;
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
content.title = [NSString localizedUserNotificationStringForKey:#"wake up" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:#"it is time" arguments:nil];
NSDateComponents *dateComponent = [[NSDateComponents alloc]init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *Components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
dateComponent.hour = [Components hour];
dateComponent.minute = [Components minute];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponent repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:#"Alarm" content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// Ask user for the permission
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted)
{
// Add your notification to center
[center addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil)
{
NSLog(#"%#", [error localizedDescription]);
}
NSLog(#"finished scheduling");
}];
}
else
{
NSLog(#"%i",granted);
}
}];
}
After I run my code and I choose a certain date. The notification does not work and I get 0 for "granted variable" in the console.

What is the limit of setting number of UserNotifications at a single time for iOS 10? [duplicate]

I'm setting multiple UNUsernotifications as below,
- (void)viewDidLoad {
[super viewDidLoad];
notifCount = 0;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request succeeded!");
[self set10Notif];
}
}];
}
In the set10Notif method, I'm setting multiple(8 for testing) notifications with time 10 seconds from current time.
-(void) set10Notif
{
notifCount = notifCount+1;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"10.0") && notifCount < 10)
{
// create actions
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];
NSString *fireStr = [self returnStringFromDate:fireD withFormat:#"hh/mm/ss dd/MM/yyyy"];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:fireD];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:#"Notif!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:#"Prayer"
content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
[userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Local Notification succeeded");
}
else {
NSLog(#"Local Notification failed");
}
[self set10Notif];
}];
#endif
}
}
All the local notifications are set. But only one notification got fired in the device, that's the last one.
Why can't the multiple notifications are not firing?
What did I do wrong?
Give a different "requestWithIdentifier" and "time delay" for each notification and try, may it works for you.
MULTIPLE NSUSERNOTIFICATION FIRE ON CUSTOM DATE AND TIME
UNUserNotificationCenter *center=[UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:( UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request authorization succeeded!");
// [self showAlert];
}
}];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:#"શ્રી હરિ જયંતિ!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:#"જય સ્વામિનારાયણ \nઆવતીકાલે એકાદશી છે!"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:#"nirnay" withExtension:#"jpg"];
NSError *error;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:#"nirnay" URL:fileURL options:nil error:&error];
objNotificationContent.attachments = #[attachment];
/// 4. update application icon badge number
// objNotificationContent.badge = #([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
for (int i=0; i<5; i++)
{
int hours=11;
int mint=46+i;
NSString *myDateAsAStringValue=#"2017-03-22";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
// or #"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSDate *myDate = [dateFormatter dateFromString: myDateAsAStringValue];
NSDate *pickerDate = myDate;
NSLog(#"pickerDate - %#",pickerDate);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
// [dateFormatter setDateFormat:DT_FORMATE_BIRTHDATE];
[dateFormatter2 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter2 setTimeZone:gmt];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *timeComponents = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute|NSCalendarUnitSecond)
fromDate:pickerDate];
[timeComponents setHour:hours];
[timeComponents setMinute:mint];
[timeComponents setSecond:0];
NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *fierDate = [formatter stringFromDate:dtFinal];
NSLog(#"fierDate - %#",fierDate);
UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:timeComponents
repeats:NO];
UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:#"ten%d",i]
content:objNotificationContent trigger:trigger3];
/// 3. schedule localNotification
[center addNotificationRequest:request3 withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Local Notification succeeded By calender");
}
else {
NSLog(#"Local Notification failed");
}
}];
}

How to add multiple Local Notification in iOS 10? [duplicate]

I'm setting multiple UNUsernotifications as below,
- (void)viewDidLoad {
[super viewDidLoad];
notifCount = 0;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request succeeded!");
[self set10Notif];
}
}];
}
In the set10Notif method, I'm setting multiple(8 for testing) notifications with time 10 seconds from current time.
-(void) set10Notif
{
notifCount = notifCount+1;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"10.0") && notifCount < 10)
{
// create actions
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];
NSString *fireStr = [self returnStringFromDate:fireD withFormat:#"hh/mm/ss dd/MM/yyyy"];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:fireD];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:#"Notif!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:#"Prayer"
content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
[userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Local Notification succeeded");
}
else {
NSLog(#"Local Notification failed");
}
[self set10Notif];
}];
#endif
}
}
All the local notifications are set. But only one notification got fired in the device, that's the last one.
Why can't the multiple notifications are not firing?
What did I do wrong?
Give a different "requestWithIdentifier" and "time delay" for each notification and try, may it works for you.
MULTIPLE NSUSERNOTIFICATION FIRE ON CUSTOM DATE AND TIME
UNUserNotificationCenter *center=[UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:( UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(#"request authorization succeeded!");
// [self showAlert];
}
}];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:#"શ્રી હરિ જયંતિ!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:#"જય સ્વામિનારાયણ \nઆવતીકાલે એકાદશી છે!"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:#"nirnay" withExtension:#"jpg"];
NSError *error;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:#"nirnay" URL:fileURL options:nil error:&error];
objNotificationContent.attachments = #[attachment];
/// 4. update application icon badge number
// objNotificationContent.badge = #([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
for (int i=0; i<5; i++)
{
int hours=11;
int mint=46+i;
NSString *myDateAsAStringValue=#"2017-03-22";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
// or #"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSDate *myDate = [dateFormatter dateFromString: myDateAsAStringValue];
NSDate *pickerDate = myDate;
NSLog(#"pickerDate - %#",pickerDate);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
// [dateFormatter setDateFormat:DT_FORMATE_BIRTHDATE];
[dateFormatter2 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter2 setTimeZone:gmt];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *timeComponents = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute|NSCalendarUnitSecond)
fromDate:pickerDate];
[timeComponents setHour:hours];
[timeComponents setMinute:mint];
[timeComponents setSecond:0];
NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *fierDate = [formatter stringFromDate:dtFinal];
NSLog(#"fierDate - %#",fierDate);
UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:timeComponents
repeats:NO];
UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:#"ten%d",i]
content:objNotificationContent trigger:trigger3];
/// 3. schedule localNotification
[center addNotificationRequest:request3 withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(#"Local Notification succeeded By calender");
}
else {
NSLog(#"Local Notification failed");
}
}];
}

What format to enter an NSDate in

This is how I am adding an event to Ical I just dont know how to make the start date something other than the current day, as well as set the duration as an all day event.
> EKEventStore *store = [[EKEventStore alloc] init];
> [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
> if (!granted) { return; }
> EKEvent *event = [EKEvent eventWithEventStore:store];
> event.title = #"Hits THermal DC I";
> event.startDate = [NSDate date]; //today
> event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
> [event setCalendar:[store defaultCalendarForNewEvents]];
> NSError *err = nil;
> [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
> NSString *savedEventId = event.eventIdentifier; //this is so you can access this event later
> }];
To create any date with your own values, do this:
NSCalendar* calendar=[NSCalendar currentCalendar];
NSDateComponents* dateComps=[[NSDateComponents alloc] init];
[dateComps setYear:1990];
[dateComps setMonth:11];
[dateComps setDay:10];
NSDate* starDate=[calendar dateFromComponents:dateComps];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Hits THermal DC I";
event.startDate = starDate
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier; //this is so you can access this event later
}];

Resources