Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:26:24+00:00 2026-06-16T17:26:24+00:00

I have a strange behavior on my iPhone. I’m creating an application that uses

  • 0

I have a strange behavior on my iPhone. I’m creating an application that uses calendar events (EventKit). The class that use is as follows:

the .h one

#import "GenericManager.h"
#import <EventKit/EventKit.h>

#define oneDay      60*60*24
#define oneHour     60*60

@protocol CalendarManagerDelegate;

@interface CalendarManager : GenericManager

/*  
 * metodo che aggiunge un evento ad un calendario di nome Name nel giorno onDate.
 * L'evento da aggiungere viene recuperato tramite il dataSource che è quindi
 * OBBLIGATORIO (!= nil).
 *
 * Restituisce YES solo se il delegate è conforme al protocollo CalendarManagerDataSource.
 * NO altrimenti
 */
+ (BOOL) addEventForCalendarWithName:(NSString *) name fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate withDelegate:(id<CalendarManagerDelegate>) delegate;

/*
 * metodo che aggiunge un evento per giorno compreso tra fromDate e toDate ad un
 * calendario di nome Name. L'evento da aggiungere viene recuperato tramite il dataSource
 * che è quindi OBBLIGATORIO (!= nil).
 *
 * Restituisce YES solo se il delegate è conforme al protocollo CalendarManagerDataSource.
 * NO altrimenti
 */
+ (BOOL) addEventsForCalendarWithName:(NSString *) name fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate withDelegate:(id<CalendarManagerDelegate>) delegate;

@end

@protocol CalendarManagerDelegate <NSObject>

// viene inviato quando il calendario necessita informazioni sull' evento da aggiungere
- (void) calendarManagerDidCreateEvent:(EKEvent *) event;

@end

the .m one

//
//  CalendarManager.m
//  AppCampeggioSingolo
//
//  Created by CreatiWeb Srl on 12/17/12.
//  Copyright (c) 2012 CreatiWeb Srl. All rights reserved.
//

#import "CalendarManager.h"
#import "Commons.h"
#import <objc/message.h>

@interface CalendarManager ()

@end

@implementation CalendarManager

+ (void)requestToEventStore:(EKEventStore *)eventStore delegate:(id)delegate fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate name:(NSString *)name
{
    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // ios >= 6.0
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            if (granted)
            {
                [self addEventForCalendarWithName:name fromDate: fromDate toDate: toDate inEventStore:eventStore withDelegate:delegate];
            }
            else
            {

            }
        }];
    }
    else if (class_getClassMethod([EKCalendar class], @selector(calendarIdentifier)) != nil)
    {
        // ios >= 5.0 && ios < 6.0
        [self addEventForCalendarWithName:name fromDate:fromDate toDate:toDate inEventStore:eventStore withDelegate:delegate];
    }
    else
    {
        // ios < 5.0
        EKCalendar *myCalendar = [eventStore defaultCalendarForNewEvents];

        EKEvent *event = [self generateEventForCalendar:myCalendar fromDate: fromDate toDate: toDate inEventStore:eventStore withDelegate:delegate];
        [eventStore saveEvent:event span:EKSpanThisEvent error:nil];
    }
}

/*
 * metodo che recupera l'identificativo del calendario associato all'app o nil se non è mai stato creato.
 */
+ (NSString *) identifierForCalendarName: (NSString *) name
{
    NSString * confFileName = [self pathForFile:kCurrentCalendarFileName];

    NSDictionary *confCalendar = [NSDictionary dictionaryWithContentsOfFile:confFileName];
    NSString *currentIdentifier = [confCalendar objectForKey:name];
    return currentIdentifier;
}

/*
 * memorizza l'identifier del calendario
 */
+ (void) saveCalendarIdentifier:(NSString *) identifier andName: (NSString *) name
{
    if (identifier != nil)
    {
        NSString * confFileName = [self pathForFile:kCurrentCalendarFileName];
        NSMutableDictionary *confCalendar = [NSMutableDictionary dictionaryWithContentsOfFile:confFileName];
        if (confCalendar == nil)
        {
            confCalendar = [NSMutableDictionary dictionaryWithCapacity:1];
        }

        [confCalendar setObject:identifier forKey:name];
        [confCalendar writeToFile:confFileName atomically:YES];
    }
}

+ (EKCalendar *)getCalendarWithName:(NSString *)name inEventStore:(EKEventStore *)eventStore withLocalSource: (EKSource *)localSource forceCreation:(BOOL) force
{
    EKCalendar *myCalendar;
    NSString *identifier = [self identifierForCalendarName:name];

    if (force || identifier == nil)
    {
        NSLog(@"create new calendar");
        if (class_getClassMethod([EKCalendar class], @selector(calendarForEntityType:eventStore:)) != nil) {
            // da ios 6.0 in avanti
            myCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
        } else {
            myCalendar = [EKCalendar calendarWithEventStore:eventStore];
        }
        myCalendar.title = name;
        myCalendar.source = localSource;
        NSError *error = nil;
        BOOL result = [eventStore saveCalendar:myCalendar commit:YES error:&error];
        if (result) {
            NSLog(@"Saved calendar %@ to event store. %@",myCalendar,eventStore);
        } else {
            NSLog(@"Error saving calendar: %@.", error);
        }

        [self saveCalendarIdentifier:myCalendar.calendarIdentifier andName:name];
    }
    //   You can also configure properties like the calendar color etc. The important part is to store the identifier for later use. On the other hand if you already have the identifier, you can just fetch the calendar:

    else
    {
        myCalendar = [eventStore calendarWithIdentifier:identifier];
        NSLog(@"fetch an old-one = %@",myCalendar);
    }

    return myCalendar;
}

+ (EKCalendar *)addEventForCalendarWithName: (NSString *) name fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate inEventStore:(EKEventStore *)eventStore withDelegate: (id<CalendarManagerDelegate>) delegate
{
    // da ios 5.0 in avanti
    EKCalendar *myCalendar;
    EKSource *localSource = nil;
    for (EKSource *source in eventStore.sources)
    {
        if (source.sourceType == EKSourceTypeLocal)
        {
            localSource = source;
            break;
        }
    }

    @synchronized(self)
    {
        myCalendar = [self getCalendarWithName:name inEventStore:eventStore withLocalSource:localSource forceCreation:NO];
        if (myCalendar == nil)
            myCalendar = [self getCalendarWithName:name inEventStore:eventStore withLocalSource:localSource forceCreation:YES];

        NSLog(@"End synchronized block %@",myCalendar);
    }

    EKEvent *event = [self generateEventForCalendar:myCalendar fromDate:fromDate toDate:toDate inEventStore:eventStore withDelegate:delegate];
    [eventStore saveEvent:event span:EKSpanThisEvent error:nil];

    return myCalendar;
}

+ (EKEvent *) generateEventForCalendar: (EKCalendar *) calendar fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate inEventStore:(EKEventStore *) eventStore withDelegate:(id<CalendarManagerDelegate>) delegate
{
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.startDate=fromDate;
    event.endDate=toDate;
    [delegate calendarManagerDidCreateEvent:event];
    [event setCalendar:calendar];

    // ricerca dell'evento nel calendario, se ne trovo uno uguale non lo inserisco
    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:fromDate endDate:toDate calendars:[NSArray arrayWithObject:calendar]];
    NSArray *matchEvents = [eventStore eventsMatchingPredicate:predicate];

    if ([matchEvents count] > 0) {
        // ne ho trovati di gia' presenti, vediamo se uno e' quello che vogliamo inserire

        BOOL found = NO;

        for (EKEvent *fetchEvent in matchEvents) {
            if ([fetchEvent.title isEqualToString:event.title] &&
                [fetchEvent.notes isEqualToString:event.notes])
            {
                found = YES;
                break;
            }
        }
        if (found)
        {
            // esiste già e quindi non lo inserisco
            NSLog(@"OH NOOOOOO!!");
            event = nil;
        }
    }

    return event;
}

#pragma mark - Public Methods

+ (BOOL) addEventForCalendarWithName:(NSString *) name fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate withDelegate:(id<CalendarManagerDelegate>) delegate
{
    BOOL retVal = YES;

    EKEventStore *eventStore=[[EKEventStore alloc] init];

    if ([delegate conformsToProtocol:@protocol(CalendarManagerDelegate)])
    {

        [self requestToEventStore:eventStore delegate:delegate fromDate:fromDate toDate: toDate  name:name];
    }
    else
    {
        retVal = NO;
    }

    return retVal;
}

+ (BOOL) addEventsForCalendarWithName:(NSString *) name fromDate:(NSDate *)fromDate toDate: (NSDate *) toDate withDelegate:(id<CalendarManagerDelegate>) delegate
{
    BOOL retVal = YES;

    NSDate *dateCursor = fromDate;

    EKEventStore *eventStore=[[EKEventStore alloc] init];


    if ([delegate conformsToProtocol:@protocol(CalendarManagerDelegate)])
    {

        while (retVal && ([dateCursor compare:toDate] == NSOrderedAscending)) {
            NSDate *finish = [dateCursor dateByAddingTimeInterval:oneDay];
            [self requestToEventStore:eventStore delegate:delegate fromDate: dateCursor toDate: finish  name:name];

            dateCursor = [dateCursor dateByAddingTimeInterval:oneDay];
        }


    }
    else
    {
        retVal = NO;
    }

    return retVal;
}

@end

In practice, on my iphone I get the log:

fetch an old-one = (null)
19/12/2012 11:33:09.520 AppCampeggioSingolo [730:8 b1b] create new calendar
19/12/2012 11:33:09.558 AppCampeggioSingolo [730:8 b1b] Saved calendar EKCalendar

every time I add an event, then I look and I can not find it on iCal calendar event he added. On the iPhone of a friend of mine, however, everything is working correctly. I doubt that the problem stems from the code, but just do not understand what it could be.

I searched all day yesterday and part of today on google but have not found anything yet.

Any help will be greatly appreciated

EDIT: I forgot the call wich is

[CalendarManager addEventForCalendarWithName: @"myCalendar" fromDate:fromDate toDate: toDate withDelegate:self];

in the delegate method simply set title and notes of the event like this

- (void) calendarManagerDidCreateEvent:(EKEvent *) event
{
    event.title = @"the title";
    event.notes = @"some notes";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:26:25+00:00Added an answer on June 16, 2026 at 5:26 pm

    Solved !!! today the iPhone was stuck on black screen with no option to turn it off. So ‘I decided to restore it and … MAGIC! The calendar is now working normally … 🙂

    Strange ways of informatic :p

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hard time to understand my iPhone Application strange behavior in iOS4.
I have an iPhone application that uses a set of plists to load the
I have a strange behavior for an application that I have implemented and that
I have some strange behavior occurring in an ASP.NET application that I am trying
I have a horizontal animated menu that has a strange behavior because it works
I have a strange behavior of Rails 3.0.10. Got this application controller: app/controllers/application_controller.rb require
I have strange behavior with android.R.layout.simple_dropdown_item_1line while using MultiAutoCompleteTextView . The code I use
I have a strange behavior in a tests where I want to test that
I have some strange behavior in my iphone app. I have an NSTimer which
I have come across some strange behavior in my iPhone Objective-C app. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.