iOS Question iEventKit - Create the calendar for my app but crashed ...

Ellen Wu

Member
Licensed User
I followed the suggestion from the thread https://www.b4x.com/android/forum/threads/ieventkit-create-the-calendar-for-my-app.117474/#post-736250 ,
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

but an erroroccurred on line:
Cal.SetField("source", nstore.GetField("defaultCalendarForNewEvents").GetField("source"))
then app crashed.
 
Upvote 0

Ellen Wu

Member
Licensed User
Yes, I have checked the authorization state.
It is solved when add store.Initialize("store") first.
B4X:
Sub CreateNewCalendar(Title As String)
    store.Initialize("store")
    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
 
Upvote 0

Ellen Wu

Member
Licensed User
Another bug caused my app crashed...

When I create an event:
B4X:
    Dim no As NativeObject = ev
    no.SetField("calendar", Calendars.Get("CSCShift"))
    no.SetField("notes", tfNote2.Text)
    store.SaveEvent(ev, False)

When I read the events:
B4X:
            TableViewEventList.Clear
            For Each e As CalendarEvent In listEventTable
                intEvent = intEvent + 1
                stringNote = ""
                Dim no As NativeObject = e
                Dim note As NativeObject
                note.Initialize("")
                If no.GetField("hasNotes").AsBoolean = True Then
                    note = no.GetField("notes")
                    Log(note)
'                    If note <> Null Then
                    stringNote = "(" & note.AsString & ")"
'                    End If
                End If       
                TableViewEventList.AddTwoLines(intEvent & ". " & e.Title & " ⏰" & DateTime.Date(e.StartDate) & "~"  &DateTime.Date(e.EndDate), "     " & stringNote)
            Next
            TableViewEventList.ReloadAll

But sometimes the app crashed and the error logs showed that
<B4INativeObject: (null)>
Error occurred on line: 830 (Main)
Object was not initialized (NSObject)


Therefore, I added log to list "CalendarEvent " details.
If the app worked fine, the logs:
*********************************
Application_Start
Application_Active
Class (b4i_xml2map) instance released.
Class (b4i_xml2map) instance released.
listEventTable:
<B4IList: (
"EKEvent <0x28171c7e0>\n{\n\t EKEvent <0x28171c7e0>\n{\t title = \t\t\U5176\U4ed6; \n\t location = \t(null); \n\t calendar = \tEKCalendar <0x283014b40> {title = CSCShift; type = CalDAV; allowsModify = YES; color = #CC73E1;}; \n\t alarms = \t\t(null); \n\t URL = \t\t\t(null); \n\t lastModified = 2020-09-18 05:36:11 +0000; \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800; \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800 \n}; \n\t location = \t(null); \n\t structuredLocation = \t(null); \n\t startDate = \t2020-09-19 05:30:00 +0000; \n\t endDate = \t\t2020-09-19 05:35:00 +0000; \n\t allDay = \t\t0; \n\t floating = \t0; \n\t recurrence = \t(null); \n\t attendees = \t(null); \n\t travelTime = \t(null); \n\t startLocation = \t(null);\n};",
"EKEvent <0x281701950>\n{\n\t EKEvent <0x281701950>\n{\t title = \t\t\U8acb\U5047; \n\t location = \t(null); \n\t calendar = \tEKCalendar <0x283016d40> {title = CSCShift; type = CalDAV; allowsModify = YES; color = #CC73E1;}; \n\t alarms = \t\t(null); \n\t URL = \t\t\t; \n\t lastModified = 2020-09-18 05:04:01 +0000; \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800; \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800 \n}; \n\t location = \t(null); \n\t structuredLocation = \t(null); \n\t startDate = \t2020-09-27 03:27:00 +0000; \n\t endDate = \t\t2020-09-27 03:27:00 +0000; \n\t allDay = \t\t0; \n\t floating = \t0; \n\t recurrence = \t(null); \n\t attendees = \t(null); \n\t travelTime = \t(null); \n\t startLocation = \t(null);\n};"
)>
*********************************
If the app crashed, the logs:
Application_Start
Application_Active
Class (b4i_xml2map) instance released.
Class (b4i_xml2map) instance released.
Time: 401
listEventTable:
<B4IList: (
"EKEvent <0x282f77720>\n{\n\t EKEvent <0x282f77720>\n{\t title = \t\t\U5176\U4ed6; \n\t location = \t(null); \n\t calendar = \tEKCalendar <0x28084a900> {title = CSCShift; type = CalDAV; allowsModify = NO; color = #CC73E1;}; \n\t alarms = \t\t(null); \n\t URL = \t\t\t(null); \n\t lastModified = (null); \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800; \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800 \n}; \n\t location = \t(null); \n\t structuredLocation = \t(null); \n\t startDate = \t2020-09-19 05:30:00 +0000; \n\t endDate = \t\t2020-09-19 05:35:00 +0000; \n\t allDay = \t\t0; \n\t floating = \t0; \n\t recurrence = \t(null); \n\t attendees = \t(null); \n\t travelTime = \t(null); \n\t startLocation = \t(null);\n};",
"EKEvent <0x282f72c70>\n{\n\t EKEvent <0x282f72c70>\n{\t title = \t\t\U8acb\U5047; \n\t location = \t(null); \n\t calendar = \tEKCalendar <0x28084af80> {title = CSCShift; type = CalDAV; allowsModify = NO; color = #CC73E1;}; \n\t alarms = \t\t(null); \n\t URL = \t\t\t(null); \n\t lastModified = (null); \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800; \n\t startTimeZone = \tAsia/Taipei (GMT+8) offset 28800 \n}; \n\t location = \t(null); \n\t structuredLocation = \t(null); \n\t startDate = \t2020-09-27 03:27:00 +0000; \n\t endDate = \t\t2020-09-27 03:27:00 +0000; \n\t allDay = \t\t0; \n\t floating = \t0; \n\t recurrence = \t(null); \n\t attendees = \t(null); \n\t travelTime = \t(null); \n\t startLocation = \t(null);\n};",
)>
*********************************
B4X:
 note = no.GetField("notes")

                    Log(note)
log(note) --> <B4INativeObject: (null)>

What could I do to solve this?
Any help and information is greatly appreciated.
 
Upvote 0

Ellen Wu

Member
Licensed User
error occurred :
[<EKEvent 0x282d11560> valueForUndefinedKey:]: this class is not key value coding-compliant for the key allowsContentModifications.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
allowsContentModifications is relative to EKCalendar, not to EKEvent.

For example, I see in your code
Dim Cal As NativeObject
Cal = Cal.Initialize("EKCalendar"). ...
So, no.GetField("allowsContentModifications").AsBoolean in your case means Cal .GetField("allowsContentModifications").AsBoolean
 
Upvote 0
Top