Adding particular date in default calender as an event - iphone

I am new in iPhone development.
There is a requirement in my application in which, there is a web service link which is below:
http://01s.in/webservices/sikhcalendar/getData.php?db_table=cal
so i want that the particular date which are shown in link, that date should be added in iCal which are add in my end in app. and it should generate an alert view on that particular day.
So, I am not getting how to add an event in iCal. Please give me some answer for this.
Thanks in advance.

adding a date in ical first you have to add the two framework in your code i.e.EventKit/EventKit.h, EventKitUI/EventKitUI.h and conforms the class to EKEventEditViewDelegate delegate and use the below method to add date in iCal
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
and i recommend you to go through this url and learn about these framework

Adding Event on the default calendar can be done using the following function
-(void)createEvent :(NSString *)eventTitle: (NSURL *)eventURL: (NSString *)eventNotes: (NSDate *)eventStartDate: (NSDate *)eventEndDate{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = eventTitle;
event.URL = eventURL;
event.notes = eventNotes;
event.startDate = eventStartDate;
event.endDate = eventEndDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
EKAlarm *myAlarm = [EKAlarm alarmWithRelativeOffset:0];
[event addAlarm:myAlarm];
NSError *err;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"event created success if value = 1 : %d", success);}
Here eventStartDate would be the time when the alarm which you set gets executed, and you get a notification

Please take a look into the EventKit framwork and the Apple Documentation.
Everything is there :)
Apple Documentation Calendar/Reminder

Related

How i can access Calendar Event and Schedular Event in our app? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to add events in iPhone using Event Kit framework
Adding particular date in default calender as an event
I am making an application in which i wanna use existing calendar events and schedular event in my app in iPhone.
and i also want to edit events through my app..
Thanks in advance...
Import EventKitUI/EventKitUI.h, EventKit/EventKit.h frameworks in your header file. This is the code to add an event to Default iPhone calendar
-(IBAction) addEvent:(id)sender
{
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *events = [EKEvent eventWithEventStore:eventStore];
events.title = #"Title";
events.notes = #"Description";
events.location = #"Location";
events.startDate = [NSDate date];
events.endDate = [NSDate date];
events.availability = EKEventAvailabilityFree;
[events setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:events span:EKSpanThisEvent error:&err];
NSLog(#"Error From iCal : %#", [err description]);
}
Then to view the Events you can use EKEventEditViewController
EKEventEditViewController *editViewController = [[EKEventEditViewController alloc] init];
editViewController.editViewDelegate = self;
editViewController.event = event3;
editViewController.eventStore = eventStore1;
[self presentModalViewController:editViewController animated:YES];
Hope this helps
Refer EventKit framework to accomplish these.
The apple documentation is here.

how to set time in alarm using Eventkit framework in ios 6

I want to add events to calendar with alarm. If any event time is at 9.00 then alarm must be set at 8.45 ,How to add alarm time using EKAlarm.
Thanks in advance.
i just found a description at techotopia For set time in alarm using Eventkit framework in ios 6 like:-
-(void)createReminder
{
EKReminder *reminder = [EKReminder
reminderWithEventStore:self.eventStore];
reminder.title = _reminderText.text;
reminder.calendar = [_eventStore defaultCalendarForNewReminders];
NSDate *date = [_myDatePicker date];
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
[reminder addAlarm:alarm];
NSError *error = nil;
[_eventStore saveReminder:reminder commit:YES error:&error];
if (error)
NSLog(#"error = %#", error);
}
Hope its helps you :)

EventKit - App freezes when adding an EKEvent with 2 alarms (iOS 5)

I have an app that programmatically adds reminders to your iOS device's calendar.
Previous to iOS 5, I could add a calendar item with two alarms thusly:
EKEventStore* eventStore = [[EKEventStore alloc] init];
EKEvent* event = [EKEvent eventWithEventStore:eventStore];
// set startDate, endDate, title, location, etc.
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]]; // 5 min
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]]; // 15 min
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError* error = nil;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&error];
On iOS 5 this freezes the application. It does not return with an error - it just never returns.
If I only call addAlarm once, it works as expected.
On iOS 4.2, calling addAlarm twice works just fine.
Am I doing something wrong?
Its a bug with Apple. If you set 2 alarms it causes the app to freeze. If you only set 1 it works just fine. This is fixed in iOS 5.1 .
If you take a look at the EventKit section in the iOS 5 changes from iOS 4.3 document, it mentions that some items are deprecated for EKEvent. The hierarchy has changed and a new abstract superclass has been added: EKCalendarItem.
have you tried calling addAlarm using a variable?
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]]; // 5 min
[event addAlarm:alarm];
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]]; // 15 min
[event addAlarm:alarm2];
I had the same error.
The problem seems that startDate shoudln't be the same as endDate... really silly iOS change!

How to add Multiple events to iphone calendar from iphone/ipad app?

I want your help my friends. Am developing iphone/ipad universal application. I want to add multiple events that is selected by user(it may 1-50 events). This app will add the events to iphone calendar. The events and event dates may differ. How to add multiple events to calendar without user interaction? I know well to add single event to iphone/ipad calendar but, i dont know to add multiple events? Please, help me friends.. I searched in google but didnt get the answer? Please.. Thanks in advance.
Thanks to read my poor english.
Yuva.M
Probably you have to store all your event objects in an array, then loop through it and add one-by-one to iPhone calendar.
.h file
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
// EKEventStore instance associated with the current Calendar application
#property (nonatomic, strong) EKEventStore *eventStore;
// Default calendar associated with the above event store
#property (nonatomic, strong) EKCalendar *defaultCalendar;
.m file
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Calendar event has called
self.eventStore = [[EKEventStore alloc] init];
// Check access right for user's calendar
[self checkEventStoreAccessForCalendar];
}// end viewDidLoad
- (BOOL) addAppointmentDateToCalender:(NSDate *) appointment_date {
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
// Doctor Name
NSString *doctorName = [objSpineCustomProtocol getUserDefaults:#"doctorName"];
// Title for the appointment on calender
NSString *appointment_title = [NSString stringWithFormat:#"Appointment with Dr.%#", doctorName];
event.title = appointment_title;
//[NSDate dateWithString:#"YYYY-MM-DD HH:MM:SS ±HHMM"], where -/+HHMM is the timezone offset.
event.startDate = appointment_date;
NSLog(#"Start date of appointment %#",event.startDate);
NSDate *end_date_appointment = [[NSDate alloc] initWithTimeInterval:1800 sinceDate:appointment_date];
event.endDate = end_date_appointment;
NSLog(#"End date of appointment %#",event.endDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
return true;
}// end method add_appointment_date_to_calender
-(void)checkEventStoreAccessForCalendar {
NSLog(#"Method: checkEventStoreAccessForCalendar");
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (status) {
// Update our UI if the user has granted access to their Calendar
case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar];
break;
// Prompt the user for access to Calendar if there is no definitive answer
case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess];
break;
// Display a message if the user has denied or restricted access to Calendar
case EKAuthorizationStatusDenied:
case EKAuthorizationStatusRestricted:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Privacy Warning" message:#"Permission was not granted for Calendar"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
break;
default:
break;
}
}// end of emethod checkEventStoreAccessForCalendar
// Prompt the user for access to their Calendar
-(void)requestCalendarAccess
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted) {
AppointmentViewController* __weak weakSelf = self;
// Let's ensure that our code will be executed from the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// The user has granted access to their Calendar; let's populate our UI with all events occuring in the next 24 hours.
[weakSelf accessGrantedForCalendar];
});
}
}];
}
// This method is called when the user has granted permission to Calendar
-(void)accessGrantedForCalendar
{
NSLog(#"Method: accessGrantedForCalendar");
// Let's get the default calendar associated with our event store
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
}// end of method accessGrantedForCalendar

UILocalNotification : updating BadgeNumber during repeatInterval

After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !
I need the:
Alert.applicationIconBadgeNumber = x
to be updated in background each time the notification fires, I am repeating the notify by:
notif.repeatInterval = NSMinuteCalendarUnit
Repeating is working fine every 1 m. when the app goes in background, but the BadgeNumber dosent get updated, it takes the 1st updated date value only.
I am calling the scheduleNotification method by viewDidLoad
Here is my full code:
- (void)scheduleNotification {
UILocalNotification *notif;
notif = [[[UILocalNotification alloc] init] autorelease];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.fireDate = [[NSDate date] dateByAddingTimeInterval:5];
notif.repeatInterval = NSMinuteCalendarUnit;
NSInteger BadgeNumber = [self BadgeNumber];
NSInteger *BadgeNumberPointer = &BadgeNumber;
NSString *BadgeNumberString = [NSString stringWithFormat:#"%i", BadgeNumber];
notif.applicationIconBadgeNumber = *BadgeNumberPointer;
notif.alertBody = BadgeNumberString;
notif.alertAction = #"Hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
-(int)BadgeNumber{
NSDate *currentDateUpdate = [[NSDate alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"dd"];
NSString *dateCheckUpdate = [formatter2 stringFromDate:currentDateUpdate];
NSInteger dateCheckUpdateInt = [[dateCheckUpdate substringWithRange:NSMakeRange(0, 2)] integerValue];
int BadgeNumber = dateCheckUpdateInt;
return BadgeNumber;
}
Kindly advice how to fix it,, thanking all of you.
Because the operating system copies the notification when scheduled, the data in the notification is not updated, therefore the application badge number doesn't change.
Maybe if you don't repeat the notification but generate your own new notification for each time interval it will give you the behavior you need. The only way I can think of generating notifications like that is to post a bunch of notifications in your scheduleNotification method, and then remember to delete the notifications when the user responds in the proper way. Since the OS only remembers the next chronologically scheduled 64 notifications, you could only schedule about an hour's worth. Since your badge number seems to be the current date, you could check the time and only bother with setting so many notifications if you're within an hour of midnight.
I don't understand what you are trying to accomplish by nagging the user so often, nor telling them the date in the badge number. Any app that bothered me so much or misused the badge number so would quickly get deleted from my iOS devices. Maybe rethinking what you are trying to accomplish may direct you to a better solution.
I know this is already answered, but you could use NSUserDefaults as a means of caching the badge count. Then in applicationIconBadgeNumber you can just use something like this:
notif.applicationIconBadgeNumber = ([NSUserDefaults standardUserDefaults] integerForKey:#"badgeCount"] + 1);
and then you could just reset it when the user responds accordingly.