iOS Question iEventKit - Create the calendar for my app?

Ellen Wu

Member
Licensed User
I want to create a new calendar named "myapp" which is not exist before. I used the code as followings,
Dim no As NativeObject = ev
no.SetField("calendar", "myapp")
no.SetField("title", "test")
no.SetField("notes", "test!!")
store.SaveEvent(ev, False)

and I got the error messages :
-[EKPersistentCalendar initWithObject:]: unrecognized selector sent to instance 0x283181c80
Stack Trace: (
CoreFoundation 7519E999-1053-3367-B9D5-8844F6D3BDC6 + 1227356
libobjc.A.dylib objc_exception_throw + 56
CoreFoundation 7519E999-1053-3367-B9D5-8844F6D3BDC6 + 193960
EventKit D8555AC8-8207-3642-8A8F-883F5A4876F0 + 411008
EventKit D8555AC8-8207-3642-8A8F-883F5A4876F0 + 575612
EventKit D8555AC8-8207-3642-8A8F-883F5A4876F0 + 150844
Foundation 7B1733B1-74C9-3A33-8A58-853B0A029826 + 205168
result -[B4INativeObject SetField::] + 132
result -[ResumableSub_main_btnEvent_Click resume::] + 8036
CoreFoundation 7519E999-1053-3367-B9D5-8844F6D3BDC6 + 1252384
CoreFoundation 7519E999-1053-3367-B9D5-8844F6D3BDC6 + 7472
 
Upvote 0

Ellen Wu

Member
Licensed User
Is it possible to implement the following code to my B4i project?

AppleScript:
func checkCalendar() -> EKCalendar { 
        var retCal: EKCalendar? 
      
        let calendars = eventStore.calendarsForEntityType(EKEntityTypeEvent) as! [EKCalendar] // Grab every calendar the user has 
        var exists: Bool = false 
        for calendar in calendars { // Search all these calendars 
            if calendar.title == "Auto Schedule Cal" { 
                exists = true 
                retCal = calendar 
            } 
        } 
      
        var err : NSError? 
        if !exists { 
            let newCalendar = EKCalendar(forEntityType:EKEntityTypeEvent, eventStore:eventStore) 
            newCalendar.title="Auto Schedule Cal" 
            newCalendar.source = eventStore.defaultCalendarForNewEvents.source 
            eventStore.saveCalendar(newCalendar, commit:true, error:&err) 
            retCal = newCalendar 
        } 
      
        return retCal! 
      
    } 
 

This functions searches for a calendar named "Auto Schedule Cal". If it doesn't find it, it creates one. It then returns the calender for use.

 
Usage:

(Given an EKEventStore titled "store")

 
let currentCal = checkCalendar() 
var calEvent = EKEvent(eventStore: store) 
calEvent.calendar = currentCal

Thank you a lot for helping~
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub GetCalendars As Map
    Dim res As Map
    res.Initialize
    Dim no As NativeObject = store
    Dim Cals As List = no.GetField("store").RunMethod("calendarsForEntityType:", Array(0))
    For Each Cal As NativeObject In Cals
        res.Put(Cal.GetField("title"), Cal)
    Next
    Return res
End Sub

Sub CreateNewCalendar(Title As String)
    Dim nstore As NativeObject = store
    nstore = nstore.GetField("store")
    Dim Cal As NativeObject
    Cal = Cal.Initialize("EKCalendar").RunMethod("calendarForEntityType:eventStore:", Array(0, nstore))
    Cal.SetField("title", Title)
    Cal.SetField("source", nstore.GetField("defaultCalendarForNewEvents").GetField("source"))
    Dim nme As NativeObject = Me
    Dim Success As Boolean = nme.RunMethod("SaveCalendar::", Array(Cal, nstore)).AsBoolean
    Log(Success)
End Sub

#if OBJC
-(BOOL)SaveCalendar:(EKCalendar*)cal :(EKEventStore*)store {
    NSError* e;
    BOOL res = [store saveCalendar:cal commit:true error:&e];
    if (e != nil) {
        NSLog(@"Error saving calendar: %@", e);
    }
    return res;
}
#End If
Usage:
B4X:
Calendars = GetCalendars 'Calendars is a global Mar
If Calendars.ContainsKey("Test") = False Then
    CreateNewCalendar("Test")
    Calendars = GetCalendars
End If
Later when you create an event:
B4X:
Dim ev As CalendarEvent = store.CreateEvent

    ev.Title = "New Event"
    Dim no As NativeObject = ev
    no.SetField("notes", "this is a test")
    no.SetField("calendar", Calendars.Get("Test"))
 
Upvote 0
Top