Create calendar event with WatchKit on the Apple Watch

Hi,


I am new to the programming language SWIFT. During our school project at school we would like to create an Apple Watch app which allows you to create an calendar event. I have search many Google pages, but I can't find the correct answer. Does anybody know if this implementation is possible through WatchKit? On Google I found a few solutions, but these are developed with Objective C.


I would like to thank you in advance!


Jelle

Answered by DTS Engineer in 124965022

OK, so, to confirm, you’re getting an “Extra argument error in call” error on line 17? If so, that’s likely a Swift 1 vs Swift 2 thing. Swift 2 has a new error handling model that translates Objective-C methods that return an NSError into Swift methods that throw. This code was written for the Swift 1 model so you’ll have to update it to the Swift 2 model.

In this case you need code like this:

do {
    try eventStore.saveCalendar(yourReminderCalendar, commit: true)
    print("reminder cal saved")
} catch {
    // … handle the error …
}

You can learn more about Swift error handling in the standard Swift docs.

Notwithstanding the above, I should also point out that saving an event store is specifically not allowed on watchOS. If you open the Objective-C header

<EventKit/EKEventStore.h>
, you’ll see the method is decorated with
__WATCHOS_PROHIBITED
.
- (BOOL)saveCalendar:(EKCalendar *)calendar commit:(BOOL)commit error:(NSError **)error NS_AVAILABLE(10_8, 5_0) __WATCHOS_PROHIBITED;

So if you put the Swift code above into a watchOS extension it fails to compile with a different error message, namely, “'saveCalendar(_:commit:)' has been explicitly marked unavailable here”.

One option here is to use WatchKit Connectivity to request that your iOS app make this change on behalf of the watchOS extension.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

In general you access the calendar via the EventKit framework. Consult the following docs:

Also, there’s a DevForums topic area for this stuff (System Frameworks > Calendar Events), so if you have questions about EventKit you should ask them there.

EventKit is an Objective-C API but Swift makes it easy to use Objective-C APIs. For some background on how that works in general, read Using Swift with Cocoa and Objective-C (Swift 2.1).

If there are specific EventKit APIs where you’re unclear as to how they translate to Swift, post the details here and we’ll see what we can do to help out.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi,


Thank you for your answer. I got the following code (from StackOverflow: http://stackoverflow.com/questions/29286359/creating-an-ekcalendar-in-ekeventstore-swift):


let eventStore = EKEventStore() var yourReminderCalendar: EKCalendar? func retrieveYourCalendar() { let calendars = eventStore.calendarsForEntityType(EKEntityTypeReminder) if(yourReminderCalendar == nil) { for calendar in calendars { if calendar.title == "Your Title" { yourReminderCalendar = (calendar as EKCalendar) break } } if(yourReminderCalendar == nil) { yourReminderCalendar = EKCalendar(forEntityType: EKEntityTypeReminder, eventStore: eventStore) yourReminderCalendar!.title = "Your Title" yourReminderCalendar!.source = eventStore.defaultCalendarForNewReminders().source var error: NSError? if(eventStore.saveCalendar(yourReminderCalendar, commit: true, error: &error)) { println("reminder cal saved") } else { / } } } }


It is weird, because this kind of code works in a ViewController, but not in a InterfaceController for the Apple Watch. Always I get the error: 'Extra argument error in call' at this part of the code:


if(eventStore.saveCalendar(yourReminderCalendar, commit: true, error: &error)) { println("reminder cal saved") }


But in a normal IOS application it works fine.


Thank you in advance!


Jelle

Alas, I can’t read your code snippet. If you edit the post and format the snippet as code (using the

<>
icon in the editor), I’d be happy to take a look.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
let eventStore = EKEventStore() 
var yourReminderCalendar: EKCalendar? 
func retrieveYourCalendar() { 
     let calendars = eventStore.calendarsForEntityType(EKEntityType.Reminder) 
     if(yourReminderCalendar == nil) { 
          for calendar in calendars { 
               if calendar.title == "Your Title" { 
                    yourReminderCalendar = (calendar as EKCalendar) 
                         break 
               } 
          } 
          if(yourReminderCalendar == nil) {
               yourReminderCalendar = EKCalendar(forEntityType: EKEntityType.Reminder, eventStore: eventStore) 
               yourReminderCalendar!.title = "Your Title" 
               yourReminderCalendar!.source = eventStore.defaultCalendarForNewReminders().source 
               var error: NSError? 
               if(eventStore.saveCalendar(yourReminderCalendar, commit: true, error: &error)) { 
                    print("reminder cal saved")
               } 
               else 
               { 
               }
           }
     }

}
Accepted Answer

OK, so, to confirm, you’re getting an “Extra argument error in call” error on line 17? If so, that’s likely a Swift 1 vs Swift 2 thing. Swift 2 has a new error handling model that translates Objective-C methods that return an NSError into Swift methods that throw. This code was written for the Swift 1 model so you’ll have to update it to the Swift 2 model.

In this case you need code like this:

do {
    try eventStore.saveCalendar(yourReminderCalendar, commit: true)
    print("reminder cal saved")
} catch {
    // … handle the error …
}

You can learn more about Swift error handling in the standard Swift docs.

Notwithstanding the above, I should also point out that saving an event store is specifically not allowed on watchOS. If you open the Objective-C header

<EventKit/EKEventStore.h>
, you’ll see the method is decorated with
__WATCHOS_PROHIBITED
.
- (BOOL)saveCalendar:(EKCalendar *)calendar commit:(BOOL)commit error:(NSError **)error NS_AVAILABLE(10_8, 5_0) __WATCHOS_PROHIBITED;

So if you put the Swift code above into a watchOS extension it fails to compile with a different error message, namely, “'saveCalendar(_:commit:)' has been explicitly marked unavailable here”.

One option here is to use WatchKit Connectivity to request that your iOS app make this change on behalf of the watchOS extension.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you very much!

Is it also not possible to create a calendar event within the AppDelegate class (with Watch connectivity)?


Jelle

I expect it’ll compile, but it’s possible that you’ll run into problems.

I’ve not tried this myself. Have you? Did you encounter problems?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Create calendar event with WatchKit on the Apple Watch
 
 
Q