开发者

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

开发者 https://www.devze.com 2023-04-01 12:00 出处:网络
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 calenda

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.. T开发者_StackOverflow中文版hanks 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号