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
Related
I have created a simple alarm notification App through which I can get real time, set alarm on or off, and play a single tone audio. But I need to play a sound which should start with a class VOID.
Below is the code:
To get and start alarm notification:
- (void)viewDidLoad {
[super viewDidLoad];
dateTimerPicker.date = [NSDate date];
}
- (void)presentMessage:(NSString *)message {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Hello!"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate {
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDate;
notification.alertBody = #"Time to wake up!!";
notification.soundName = #"PhoneOld.mp3";
[self playPause];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)alarmSetOn:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate:dateTimerPicker.date];
NSLog(#"Alarm Set: %#", dateTimeString);
[self scheduleLocalNotificationWithDate:dateTimerPicker.date];
[self presentMessage:#"Alarm ON!"];
}
- (IBAction)alarmSetOff:(id)sender {
NSLog(#"Alarm Off");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self presentMessage:#"Alarm OFF!"];
}
This is my VOID:
- (void)playPause {
RADAppDelegate *appDelegate = (RADAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.radiosound == 0){
[appDelegate.radiosound play];
} else {
[appDelegate.radiosound pause];
}
}
How can I set the alarm to start playing the radiosound if is rated 0, like a:
notification.soundName = [self playPause];
But I know this is a NSString.
You don't need to assign a sound name to scheduled notification, just invoke the playPause method and get the name of sound file from notification, as shown below and just assign it to NSString and set property to it in appDelegate and access it to play that file.
AppDelegate.h
#property (nonatomic,strong) NSString *nsStr_soundFile;
AppDelegate.m
#synthesize nsStr_soundFile;
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
//Give call to play sound method.
self.nsStr_soundFile=notification.soundName;
VOID *obj=[VOID alloc]init];
[obj playPause];
}
You can make a trick with opting out of iOS multitasking by setting in your app .plist file this key UIApplicationExitsOnSuspend to YES as written here
When an app opts out, it cycles between the not-running, inactive, and
active states and never enters the background or suspended states.
An app that runs in the pre-multitasking compatibility mode keeps
running when the user locks the device while the app is in the
foreground. All the app has to do is wait for the alarm time and
execute its custom code.
I'm writing an alarm clock iOS app. It's my first time using UILocalNotification. I'm getting the date from a date picker. I've formatted the dates to check whether my function was being passed the proper date, it was. I checked all of the needed properties for a UILocalNotification and I have them all and my notification still won't fire. Any ideas as to why? Thanks for the help.
#import "BIDAlarmViewController.h"
#interface BIDAlarmViewController ()
#end
#implementation BIDAlarmViewController
#synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setReminderUsingDateFromDatePicker: (id)sender{
[self scheduleNotificationForDate: datePicker.date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];
NSLog(#"Button Pressed.. date: %#", formattedDateString);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alarm activated"
message:#"Alarm has been set"
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
-(void) scheduleNotificationForDate: (NSDate*)date {
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = date;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = #"alarmsound.caf";
alarm.alertBody = #"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}
#end
http://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:
Make sure you've implemented the referenced method in your app delegate like so:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"Notification fired"!);
}
Notes from Apple on implementing this method:
Local notifications are similar to remote push notifications, but
differ in that they are scheduled, displayed, and received entirely on
the same device. An application can create and schedule a local
notification, and the operating system then delivers it at the
schedule date and time. If it delivers it when the application is not
active in the foreground, it displays an alert, badges the application
icon, or plays a sound—whatever is specified in the
UILocalNotification object. If the application is running in the
foreground, there is no alert, badging, or sound; instead, the
application:didReceiveLocalNotification: method is called if the
delegate implements it.
The delegate can implement this method if it wants to be notified that
a local notification occurred. For example, if the application is a
calendar application, it can enumerate its list of calendar events to
determine which ones have due dates that have transpired or are about
to transpire soon. It can also reset the application icon badge
number, and it can access any custom data in the local-notification
object’s userInfo dictionary.
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.
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
I'm writing an iPhone app that will use the EventKit framework to create new events in the user's Calendar. That part works pretty well (except for the wonky way it handles the timezone -- but that's another issue). What I can't figure out is how to get a list of the user's calendars so that they can choose which calendar to add the event to. I know that its an EKCalendar object but the docs don't show any way to get the whole collection.
Thanks in advance,
Mark
Searching through the documentation reveals an EKEventStore class that has a calendars property.
My guess is that you'd do something like:
EKEventStore * eventStore = [[EKEventStore alloc] init];
NSArray * calendars = [eventStore calendars];
EDIT: As of iOS 6, you need to specify whether you want to retrieve calendars of reminders or calendars of events:
EKEventStore * eventStore = [[EKEventStore alloc] init];
EKEntityType type = // EKEntityTypeReminder or EKEntityTypeEvent
NSArray * calendars = [eventStore calendarsForEntityType:type];
The code I used to get a useable NSDictionary of calendar names and types is like this:
//*** Returns a dictionary containing device's calendars by type (only writable calendars)
- (NSDictionary *)listCalendars {
EKEventStore *eventDB = [[EKEventStore alloc] init];
NSArray * calendars = [eventDB calendars];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSString * typeString = #"";
for (EKCalendar *thisCalendar in calendars) {
EKCalendarType type = thisCalendar.type;
if (type == EKCalendarTypeLocal) {
typeString = #"local";
}
if (type == EKCalendarTypeCalDAV) {
typeString = #"calDAV";
}
if (type == EKCalendarTypeExchange) {
typeString = #"exchange";
}
if (type == EKCalendarTypeSubscription) {
typeString = #"subscription";
}
if (type == EKCalendarTypeBirthday) {
typeString = #"birthday";
}
if (thisCalendar.allowsContentModifications) {
NSLog(#"The title is:%#", thisCalendar.title);
[dict setObject: typeString forKey: thisCalendar.title];
}
}
return dict;
}
I get the list of calendars OK - the problem is I don't get a user-displayable list. The calendar.title property is null for all of them; I don't see any kind of id property either.
-> Update: It works now for me. The mistake I had made was to put the eventStore object in a temporary variable, then get the list of calendars, then release the eventStore. Well if you do that all your calendars go away too. Containment is not strictly object oriented in some of the iOS frameworks, and this is an example of that. That is, the calendar object is dependent on the event store, it's not its own, separate entity.
Anyway -the solution above is fine!