create a custom calendar - ios

I want to create a new custom calendar on iOS, in my application.
I search for some explications on how do it work. But my code doesn't work / the calendar is not created
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKSource *theSource = nil;
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal) {
theSource = source;
break;
}
}
NSString *identifier;
EKCalendar *cal;
if (identifier == nil)
{
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
cal.title = #"MyCustomCalendar";
cal.source = theSource;
[eventStore saveCalendar:cal commit:YES error:nil];
NSLog(#"cal id = %#", cal.calendarIdentifier);
}
else
{
cal = [eventStore calendarWithIdentifier:identifier];
}

I'm not sure if you are completely set on using EKCalendar, but you do not need to to make a calendar in iOS. You can design your own however you want with a UICollectionView or Buttons laid out dynamically. The important part is to make sure all of your dates are arranged correctly, which is not necessarily trivial. But, if you have a good way to get the first weekday for the first day of a given month, you can get it done. I followed this algorithm: http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week which seemed a bit complicated, but in practice was easy to code and reliable. Otherwise I believe apple has a 'firstWeekday' method/property you can try, but that didn't work for me. Once you have the right first weekday you can enumerate through the days of the rest of the month and put them how you want on your calendar.
It seems like you are unsure how to setup your calendar data with EKCalendar. I found a nice tutorial you may find useful:
http://oleb.net/blog/2012/05/creating-and-deleting-calendars-in-ios/
For more specific questions try the documentation, you can find it with a google search.
Otherwise, what kind of data export are you looking for? iCloud? Local? Are you working with an actual calendar provided or just saving something with all of your calendar data that another application can read?

The one possible way to get this issue is, mismatching calendar source type.
Since you've created the calendar with local source type, system could not find local source if you already synced your iCloud / gmail / exchange calendars.
So my suggestion is don't stick with local source and try to create the calendar in any available source. If you go for this approach, you'll also be ready for solving appropriate issues (like the case iCloud is synced but user has not given permission for iCloud-calendar sync).

Related

EKEventStore will not return Exchange Calendar items by external or local identifier

I am using the EKEventStore API to save events to the default calendar using the following:
EKEventStore - saveEvent:span:commit:error:.
Once the event is saved, I store the externalID and localID in my database for future reference using the following:
externalID = [myEvent calendarItemExternalIdentifier]; and
localID = [myEvent eventIdentifier].
The problem I am having is that when I then go back to try and retrieve the event using the following:
[[eventStore calendarItemsWithExternalIdentifier:externalID] firstObject]
OR
[eventStore eventWithIdentifier:localID],
iOS is not able to find my event.
If I run the exact same code, but have my default calendar set to an iCloud calendar, however, everything works correctly.
But if the default calendar is an Exchange calendar, I am getting the following error message:
"Error getting calendar item with UUID [insert externalID here]: Error Domain=EKCADErrorDomain Code=1010 "(null)""
Has anyone encountered this issue?
I have had this code deployed for over 2 years now and users recently reported they weren't able to open appointments created on Exchange calendars. Not sure what changed or when, but I have tested this on iOS 10 and 11, and both have the issue.
Any insight would be greatly appreciated,
Sincerely,
~Arash
Unfortunately not... we were able to work around this issue by including an internal reference ID in the notes of the event. Then, if both the externalEventID and eventID failed to return the event, which is our issue with Exchange, then we fall back to use the start and end date in an EKEventSearchCallback to look for our event.
Basically, it ends up looking like this:
EKEventSearchCallback searchBlock = ^(EKEvent *event, BOOL *stop)
{
if([MyCustomClass event:event
notesMatchesKnownValueDict:knownValueDict])
{
foundEvent = event;
*stop = YES;
}
};
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:nil];
[eventStore enumerateEventsMatchingPredicate:predicate usingBlock:searchBlock];
Another solution might be waiting for the event to sync with Exchange. For example, I save the event and keep track of the ID assigned to it. Have a loop check every few seconds for a change. When the change happens, it's on the exchange server and you're good to reference that in your DB.
Try it out, let me know if it works!

How to not display all the local calendars

Hello and thanks for the help,
What i want to do is create an app that will display several calendars with different tasks for example:
I have one calendar for my dog for when he will take a shower or go to the vet.
One for my bird that also has when he will take a shower and go to the vet.
One for me, cause i need shower and go to the vet too! haha
something like that.
So i have been following this tutorial:
http://www.appcoda.com/ios-event-kit-programming-tutorial/
And my goal is when i press the bird profile and press the shower button only the birdShower calendar is shown and when i press the doctor only the birdDoctor calendar is shown.
And when i press the dog or my profile the same happens.
But what is happening now is that all local calendars are been displayed and i don't want that.
I am very bad at programming and i am just starting and i have been trying to fix this for days but i just failed. So my question is how can i access and display the calendars i want for each profile menu (bath or doctor) without the other profiles or menus.. knowing about each other?
And how can i automatically set a new calendar for the user?
for example if he creates a new profile named Goku and than he presses the button BATH is it possible to create a calendar called GokuBaths automatically?
Thank you for the help.
Use Below Code for getting calender list.
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^(void) {
NSArray *cals = [eventStore calendarsForEntityType: EKEntityTypeEvent];
NSLog(#"Cals: %#",cals);
});
}
else
{
//Error
}
}];

Get List of all available Calendars for storing event - iOS (EKEventkit)

I am using EventKit for storing event in a calendar.
Right now I am using [event setCalendar:self.defaultCalendar];
Where,
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents.
Now, I like to access all available calendar list. As i want to give select desire calendar to user from averrable list.
After looking Apple's document i come to know that i can set any calendar by using delegate method,
- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller
But how can I get/access available calendars (an array of available calendars so I can show that list to user)?
It's relatively simple:
EKEntityType type = EKEntityTypeEvent;
NSArray *calendars = [self.eventStore calendarsForEntityType:type];
Note that EKEntityType can be EKEntityTypeEvent or EKEntityTypeReminder depending on what you're looking to edit.

iCloud calendar synchronization issue while creating calendar from application

Trying to implement iCloud Calendar synchronization for iOS.
The idea is to create a new calendar from my app and sync it with iCloud when iCloud sync is on actually.
To get corresponding source I'm using the following code:
EKSource* localSource=nil;
for (EKSource* source in self.eventStore.sources)
{
if(source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:#"iCloud"])
{
localSource = source;
break;
}
}
Then creating a calendar in that source and saving.
When iCloud sync is on and Calendar synchronization is on for iCloud as well from iPhone->Settings->iCloud->Calendar It's working just fine.
After switching off calendar synchronization from above mentioned settings theoretically it should not allow to create calendar in that store any more. But actually even in that case it allows to get corresponding iCloud store from my application and create/save a new calendar.
After creating a new calendar it's not showing it in iPhone's calendars list. But when you logging in to the iCloud web interface you can see there a lot of calendars with the same name that you have just added. The number of calendars with that name is getting more and more. Seams like there is an infinite loop problem in calendar synchronization for iCloud. So far seams like it's an iOS problem and could not find any report on that anywhere.
Not sure if you figured this out, if you did, post your solution :-)
But it does seem to be a bug - I just checked iCal on my Mac and it's loaded up with duplicate calendars.
Just figuring it out, but some rough code I think I have working is to create a Calendar in the EKSource and then check for that Calendar.
Something like this:
-(BOOL)testCal {
BOOL cal = 0;
NSUInteger counter = 1;
for (EKCalendar *thisCalendar in [[DGEK eventStore]calendars] ){
NSLog(#"%#", thisCalendar.title);
if ([thisCalendar.title isEqualToString:#"YourCalName"]) {
cal = YES;
return cal;
}
counter++;
}
return cal;
}
I think that works. Just doing some more testing at the moment.
If the Cal doesnt exist I'm getting the default source with something like this:
source = [[[self eventStore] defaultCalendarForNewEvents] source];
Hope that helps.

Accessing the browsing history of iphone using webkit private framework

Hi I request please read the question completely before marking it as duplicate.
I am trying to get the iphone browsing history by using Webkit private framework.I get the headers for it from the github site. But I am not getting which headers or which methods to use to accomplish my task. I tried with the following code but its not returning anything not even null.
WebHistory *history=[WebHistory optionalSharedHistory];
NSDate *now = [NSDate date];
//id date;
NSArray *arr = [history orderedItemsLastVisitedOnDay:now];
NSLog(#"%#",[history allItems]);
I am writing in house app so i don't mind with this private framework. But i just can't go for jailbreaking. Please guide me the right way.
In order for the optionalSharedHistory method to return anything but null it must be instantiated and set like so in a place that's convenient in your application. Like a root view controller or the AppDelegate.
// Create a shared WebHistory object
WebHistory *myHistory = [[WebHistory alloc] init];
[WebHistory setOptionalSharedHistory:myHistory];
Hope this helps!

Resources