2013-04-01 12 views
10

Chcę otworzyć macierzysty kalendarz IOS (ical) z mojej aplikacji i dodać wydarzenie. Czy istnieje sposób, aby otworzyć kalendarz dla określonego wydarzenia?Jak dodać wydarzenie w macierzystym kalendarzu IOS

Podążam też za Open iphone calendar app programmatically, ale jeszcze się nie udało.

+0

'SBSLaunchApplicationWithIdentifier (CFSTR ("com.apple.mobilecal"), false);', ale to jest prywatne. –

+0

@AkshayCzy możesz mi powiedzieć, która biblioteka lub przykładowa aplikacja, do której się odwołujesz, wyświetlają natywny kalendarz IOS? –

Odpowiedz

31

Zobacz Calendar and Reminders Programming Guide. Ale podstawowy proces jest:

  1. Dodaj EventKit.Framework i EventKitUI.Framework do projektu. (Patrz Linking to a Library or Framework.)

  2. Importuj nagłówek:

    #import <EventKitUI/EventKitUI.h> 
    
  3. Jeśli tworzysz wydarzenie, możesz użyć:

    - (IBAction)didPressCreateEventButton:(id)sender 
    { 
        EKEventStore *store = [[EKEventStore alloc] init]; 
    
        if([store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
        { 
         // iOS 6 
         [store requestAccessToEntityType:EKEntityTypeEvent 
               completion:^(BOOL granted, NSError *error) { 
                if (granted) 
                { 
                 dispatch_async(dispatch_get_main_queue(), ^{ 
                  [self createEventAndPresentViewController:store]; 
                 }); 
                } 
               }]; 
        } else 
        { 
         // iOS 5 
         [self createEventAndPresentViewController:store]; 
        } 
    } 
    
    - (void)createEventAndPresentViewController:(EKEventStore *)store 
    { 
        EKEvent *event = [self findOrCreateEvent:store]; 
    
        EKEventEditViewController *controller = [[EKEventEditViewController alloc] init]; 
        controller.event = event; 
        controller.eventStore = store; 
        controller.editViewDelegate = self; 
    
        [self presentViewController:controller animated:YES completion:nil]; 
    } 
    
  4. Twój kontroler widoku powinny być zgodne z protokołem EKEventEditViewDelegate:

    @interface ViewController() <EKEventEditViewDelegate> 
    

    i wdrożenie metody didCompleteWithAction:

    - (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action 
    { 
        [self dismissViewControllerAnimated:YES completion:nil]; 
    } 
    
  5. Można oczywiście stworzyć wydarzenie w dowolny sposób. Na przykład, to wygląda na imprezy w następnym tygodniu z odpowiednim tytułem, a jeśli go nie znaleźć, należy utworzyć nowe zdarzenie (godzina długo wydarzenie, które rozpoczyna się w cztery godziny):

    - (EKEvent *)findOrCreateEvent:(EKEventStore *)store 
    { 
        NSString *title = @"My event title"; 
    
        // try to find an event 
    
        EKEvent *event = [self findEventWithTitle:title inEventStore:store]; 
    
        // if found, use it 
    
        if (event) 
         return event; 
    
        // if not, let's create new event 
    
        event = [EKEvent eventWithEventStore:store]; 
    
        event.title = title; 
        event.notes = @"My event notes"; 
        event.location = @"My event location"; 
        event.calendar = [store defaultCalendarForNewEvents]; 
    
        NSCalendar *calendar = [NSCalendar currentCalendar]; 
        NSDateComponents *components = [[NSDateComponents alloc] init]; 
        components.hour = 4; 
        event.startDate = [calendar dateByAddingComponents:components 
                   toDate:[NSDate date] 
                   options:0]; 
        components.hour = 1; 
        event.endDate = [calendar dateByAddingComponents:components 
                   toDate:event.startDate 
                  options:0]; 
    
        return event; 
    } 
    
    - (EKEvent *)findEventWithTitle:(NSString *)title inEventStore:(EKEventStore *)store 
    { 
        // Get the appropriate calendar 
        NSCalendar *calendar = [NSCalendar currentCalendar]; 
    
        // Create the start range date components 
        NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init]; 
        oneDayAgoComponents.day = -1; 
        NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents 
                    toDate:[NSDate date] 
                   options:0]; 
    
        // Create the end range date components 
        NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init]; 
        oneWeekFromNowComponents.day = 7; 
        NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents 
                     toDate:[NSDate date] 
                     options:0]; 
    
        // Create the predicate from the event store's instance method 
        NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo 
                      endDate:oneWeekFromNow 
                      calendars:nil]; 
    
        // Fetch all events that match the predicate 
        NSArray *events = [store eventsMatchingPredicate:predicate]; 
    
        for (EKEvent *event in events) 
        { 
         if ([title isEqualToString:event.title]) 
         { 
          return event; 
         } 
        } 
    
        return nil; 
    } 
    
+0

Dziękuję, rob. Chociaż nie jestem w stanie otworzyć aplikacji kalendarza bezpośrednio, rozwiązanie jest bliskie temu, czego potrzebowałem. –

+0

dziękuję ..! Pracowałem dla mnie. – iGo

+0

Dzięki alots, działa dobrze mnie, po prostu kopiuj i wklej jako jego. – ravinder521986

3

Poniższy kod pomaga w dodawaniu zdarzeń do natywnego kalendarza.

- (NSString*)addEventToCalendarWithTitle:(NSString *)eventTitlestr Description:(NSString *)eventDescriptionstr startDate:(NSString *)startDatestr startTime:(NSString *)startTimestr { 
        NSString *returneventidentifier=nil; 
        if (__IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED) { 
             NSDateFormatter *formatter_=[[NSDateFormatter alloc]init]; 
             NSTimeZone *gmtZone = [NSTimeZone systemTimeZone]; 
             NSDate *startedDate=nil; 
             NSDate *endedDate=nil; 
             [formatter_ setTimeZone:gmtZone]; 
             NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]; 
             formatter_.locale = enLocale; 
             [enLocale release]; 
             if (startDatestr!=nil&&startTimestr!=nil) { 
                  startDatestr = [startDatestr stringByAppendingString:startTimestr]; 
              } 
             [formatter_ setDateFormat:[DATE_FORMAT_FROM_RESPONSE stringByAppendingString:TIME_FORMAT_FROM_RESPONSE]]; // here date and time formats are appended as datestring and time string are appended 
             if(startDatestr!=nil){ 
                   
                  startedDate = [formatter_ dateFromString:startDatestr]; 
                  EKEventStore *eventStore = [[EKEventStore alloc] init]; 
                  //            __block BOOL accessGranted = NO; 
                  //            if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) { 
                  //                dispatch_semaphore_t sema = dispatch_semaphore_create(0); 
                  //                [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
                  //                    accessGranted = granted; 
                  //                    dispatch_semaphore_signal(sema); 
                  //                }]; 
                  //                dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 
                  //            } else { // we're on iOS 5 or older 
                  //                accessGranted = YES; 
                  //            } 
                  //             
                  //            if (accessGranted) { 
                  //            } 
                  EKEvent *event  = [EKEvent eventWithEventStore:eventStore]; 
                  event.title     = eventTitlestr; 
      event.notes = eventDescriptionstr; 
                  event.startDate = startedDate; 
                  endedDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate]; 
                  event.endDate   =endedDate ; 
                  event.alarms=[NSArray arrayWithObject:[EKAlarm alarmWithRelativeOffset:DEFAULTALERTTIME]]; 
                  [event setCalendar:[eventStore defaultCalendarForNewEvents]]; 
                  NSError *err; 
                  [eventStore saveEvent:event span:EKSpanThisEvent error:&err];   
                  [eventStore release]; 
                  eventStore=nil; 
                  [endedDate release]; 
                  endedDate=nil; 
                  returneventidentifier=event.eventIdentifier; 
              } 
             [formatter_ release]; 
             formatter_=nil; 
              
         } 
        else { 
             UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert !" message:@"Lower IOS versions event adding functionality is not possible." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
     [alert release]; 
         } 
        return returneventidentifier; 
} 
+0

Ten kod działa dla mnie: –

10

Użyj schematu URL, aby otworzyć aplikację natywnego kalendarza.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"calshow://"]]; 

http://devsupport.layar.com/entries/23687993-How-to-use-Custom-URI-Scheme-Button-in-the-Creator-

+0

. Twoje rozwiązanie działa idealnie na iOS 7, ale nie działa, gdy próbowałem w symulatorze iOS 6.1. Jakieś sugestie ? – GoGreen

+4

Nie będzie działać na symulatorze 6.x, ponieważ symulator 6.x nie ma w nim aplikacji kalendarza. Spróbuj na urządzeniu. – nanjunda

Powiązane problemy