EKEventEditViewController is presented with a disabled Add button - ios

The problem
I'm presenting a EKEventEditViewController view with some predefined information. If the user doesn't want to change this information, he should be able to tap Add to add the event to the calendar.
The problem is, the Add button is disabled by default. It is only enabled if the user changes something (like the event name or the Calendar, for example).
Snippet of code
class EventManager {
private var eventEditViewController: EKEventEditViewController?
private let eventStore = EKEventStore()
func addToCalendar(_ eventData: EventData) {
let event = createEvent(eventData)
presentEvent(event)
}
private func createEvent(_ eventData: EventData) -> EKEvent {
let event = EKEvent(eventStore: eventStore)
event.title = "My event"
event.startDate = Date()
event.endDate = Date()
event.isAllDay = true
event.calendar = eventStore.defaultCalendarForNewEvents
event.availability = .free
event.addAlarm(EKAlarm.init(absoluteDate: event.startDate))
event.url = URL(string: "http://myurl.com/")
return event
}
private func presentEvent(_ event: EKEvent) {
DispatchQueue.main.async {
self.eventEditViewController = EKEventEditViewController()
self.eventEditViewController!.eventStore = self.eventStore
self.eventEditViewController!.event = event
self.eventEditViewController!.editViewDelegate = self
self.viewController?.present(self.eventEditViewController!, animated: true)
}
}
}
extension EventManager: EKEventEditViewDelegate {
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
eventEditViewController?.dismiss(animated: true, completion: {
self.delegate.finish(result: CalendarResult.fromAction(action))
})
}
}
EKEventEditViewController
Here's how the EKEventEditViewController is presented:
One more thing
Another thing I've noticed is that when I remove the start and end date from my EKEvent object, the Add button is enabled by default.
How can I configure my EKEvent object, in a way that it has a custom start and end date, and at the same time enable the Add button of EKEventEditViewController by default?

This was fixed on iOS 13.3 beta
On iOS 12.2.x: Apparently the EKEventEditViewController is treating your event as an already existing event and not as a new event. Thus disabling the Add button since no changes where made (Apple bug).
A small way to prove it is to try to edit the title by removing a character, this will enable the add because now it changed from the original. If you put back the same character you just removed it will disable the Add button again.
A workaround we found was to subclass the EKEventEditViewController like this:
final class FixedEventEditViewController: EKEventEditViewController {
/// Set this variable instead of the `event` property to avoid a crash on iOS 12+ when a fixed timezone is set
var deferredEvent: EKEvent?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let deferredEvent = self.deferredEvent {
// Trick iOS into thinking that the event changed so it enables the Add button on iOS 13.2.x -> Fixed starting iOS 13.3
let titleDeferred = deferredEvent.title
deferredEvent.title = nil
// Set the event to the new deferred event that contains no title
self.event = deferredEvent
// Set the original title. This will let iOS think the event changed and enable the Add button
self.event?.title = titleDeferred
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// This is to hide the keyboard
self.view.endEditing(true)
}
}

Related

Create new event in CalendarKit

In my app, I'm trying to add ability to create a new event with CalendarKit when I press an empty space, but I can't find how to do that.
I know it's possible because you can do it in the demo app:
I've tried adding this to my app's code:
override func create(event: EventDescriptor, animated: Bool = false) {
self.events.append(event) // self.events is my events data source
}
But it didn't worked, in fact, it doesn't even get called when I long press an empty space.
I also tried to look in the source code, but I found nothing. How can I do that? thanks in advance
override func dayView(dayView: DayView, didLongPressTimelineAt date: Date) {
let newEvent = Event()
newEvent.startDate = date
newEvent.endDate = date.addingTimeInterval(3600)
// Customize your event...
newEvent.text = randomName() // A function that generates a new random name that haven't been used before.
self.create(event: newEvent)
}
override func create(event: EventDescriptor, animated: Bool = false) {
super.create(event: event, animated: animated)
self.events.append(event)
}
override func dayView(dayView: DayView, didUpdate event: EventDescriptor) {
for (index, eventFromList) in events.enumerated() {
if eventFromList.text == event.text {
events[index] = event
}
}
self.endEventEditing()
self.reloadData()
}
Please make sure that every Event have it's own unique name, otherwise it won't work

Trigger UIAlertViewController Based on Time

I have UITable to display different animals. When you select a cell in the table, a new view controller with a large UIImage is pushed. Currently, when you zoom in on the image, a UIAlertView is triggered that asks the user if they would like to download hi res images. If they click yes, the "hi-res-flag" is set to "yes" in user defaults and they no longer see the pop up. However, if they select no, the hi-res-flag will continue to pop up each time they zoom in on a photo.
Instead, if they answer no, I would like to have this flag pop up occasionally. Not every time the click a cell in the species table, nor every time they open the app. Something more like once or twice a month. Is there a way to use time in the logic of an iOS app? For instance, erase the value set for "high-res-flag" (if already equals 'no') in user defaults, once a month?
Store the time you showed the alert last in the user preferences, and then check that value every time before you present the alert whether a certain time has passed.
I have written a time checker class that does the job. The code is in Swift. You can use it from your Objective-C code as well. You can find this code in gist here.
Solution
Below, you use the viewWillAppear delegate method to see if the hiResFlag is existing. If it is present and false, then you check to see if you can display the popup:
import UIKit
class ImageViewController: UIViewController {
//Whenever you enter the Image View Controller, you check whether to show popup or not
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let hiResFlag = hiResFlag {
if hiResFlag == false {
if PopUpTimeChecker.shouldShowPopUp() {
self.presentAlert()
}
}
}
}
func presentAlert() {
let alert = UIAlertController.init(title: nil, message: "Show Pop up", preferredStyle: .alert)
let action = UIAlertAction.init(title: "Yeahh!", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
The following code implements the time-checking algorithm. Edit popUpTimeInterval below for setting your minimum time. Right now, it is set to be 15 days (in seconds). Once in every 15 days the pop-up will be shown when you call the shouldShowPopUp method.
import UIKit
//Below 4 variables, I have made them Global. No need to make them global in your case
#objc var popUpTimeInterval: UInt64 = 1296000 //15 days in seconds
#objc var hiResFlag: Bool? {
get {
return UserDefaults.standard.object(forKey: "HiResFlag") as? Bool
}
set {
UserDefaults.standard.setValue(newValue, forKey: "HiResFlag")
}
}
#objc var isFirstTimePopUp: Bool {
get {
let value = UserDefaults.standard.object(forKey: "IsFirstTimePopUp")
return value == nil ? true : value as! Bool
}
set {
UserDefaults.standard.setValue(newValue, forKey: "IsFirstTimePopUp")
}
}
#objc var lastDateOfPopUp: Date? {
get {
return UserDefaults.standard.object(forKey: "LastDateOfPopUp") as? Date
}
set {
UserDefaults.standard.setValue(newValue, forKey: "LastDateOfPopUp")
}
}
#objc class PopUpTimeChecker {
#objc static fileprivate func setLastPopUpDate() {
//Setting current date to last shown pop up date
lastDateOfPopUp = Date()
}
#objc static fileprivate func timeIntervalSinceLastPopUp() -> UInt64 {
//Returning how much time (in seconds) has passed from last popup date until now
return UInt64(Date().timeIntervalSince(lastDateOfPopUp!))
}
#objc static func shouldShowPopUp() -> Bool {
//We proceed further only if we have the last date when pop up was displayed, else we create and set it as the current date
if let _ = lastDateOfPopUp {
let timeInterval = timeIntervalSinceLastPopUp()
if timeInterval > popUpTimeInterval {
self.setLastPopUpDate()
return true //Show pop up
} else {
if isFirstTimePopUp {
//If this is the first time, you just allow the pop up to show, don't allow otherwise
isFirstTimePopUp = false
return true
} else {
return false
}
}
} else {
self.setLastPopUpDate() //Since we don't have a last date, we set it here for starting off
return self.shouldShowPopUp() //Recursively call method
}
}
}

How to accept/decline EKEvent invitation?

I would like to allow my users to accept/decline a meeting invitation within my app.
I think what I need is to update somehow the EKParticipantStatus but it looks like it isn't possible to update.
Apple Docs: Event Kit cannot add participants to an event nor change participant
information
In this stackOverflow question someone suggested to bring the native EventKitUI, which I've tried like this:
class CalendarViewController: UIViewController, EKEventViewDelegate {
// .....
let eventController = EKEventViewController()
guard let eventWithIdentifier = MeetingsFetcher.eventStoreClass.event(withIdentifier: meeting.UUID) else {
return nil
}
eventController.delegate = self
eventController.event = eventWithIdentifier
eventController.allowsEditing = true
eventController.allowsCalendarPreview = true
let navCon = UINavigationController(rootViewController: eventController)
// customizing the toolbar where the accept/maybe/decline buttons appear
navCon.toolbar.isTranslucent = false
navCon.toolbar.tintColor = .blueGreen
navCon.toolbar.backgroundColor = .coolWhite10
// customizing the nav bar where the OK button appears
navCon.navigationBar.callinAppearence()
present(navCon, animated: true, completion: nil)
// .....
// view gets dismissed, so it does detects the action, but no effect
func eventViewController(_ controller: EKEventViewController, didCompleteWith action: EKEventViewAction) {
controller.dismiss(animated: true, completion: nil)
}
The native UI shows pretty good but the buttons don't make any effect in the native calendar.
Am I missing something or it's just not possible? Why would they allow to interact with those buttons if it's not possible to save anything?
Thank you!
PD: I have these permissions in the info.plist:
Privacy - Calendars Usage Description
Privacy - Contacts Usage Description
Privacy - Reminders Usage Description
UPDATE:
Please note that EKEventEditViewController is not what I am looking for. This screen wouldn't allow me to accept or decline the event, it would only allow me to edit the details.
To allow the user to create, edit, or delete events, use the EKEventEditViewDelegate protocol.
let eventController = EKEventViewController()
guard let eventWithIdentifier = MeetingsFetcher.eventStoreClass.event(withIdentifier: meeting.UUID) else {
return nil
}
eventController.delegate = self
eventController.event = eventWithIdentifier
eventController.editViewDelegate = self
...
CalendarViewController class must conform to the EKEventEditViewDelegate protocol and must implement the eventEditViewController method to dismiss the modal view controller as shown below:
func eventEditViewController(_ controller: EKEventEditViewController,
didCompleteWith action: EKEventEditViewAction) {
switch (action) {
case EKEventEditViewActionCanceled:
case EKEventEditViewActionSaved:
...
}
}

Swift bool changes by itself

I have this weird bug. I have a global Bool called CalendarAccess:
var EventStore: EKEventStore!
var Calendar: NSCalendar!
var CalendarAccessRequestComplete = false
var _CalendarAccess = false
var CalendarAccess: Bool {
set(value)
{
_CalendarAccess = value;
}
get {
return _CalendarAccess;
}
}
As you can see, I made a setter and getter for it so I could put a breakpoint to see where it is being set. I did that and every single time I hit the breakpoint the value is true. I'm sure I never set the _CalendarAccess directly because this variable was called just CalendarAccess until just now.
But, when I do the following in a view controller, CalendarAccess is false!
#IBAction func saveEvent(_ sender: Any) {
if(CalendarAccess)
{
let event = EKEvent(eventStore: EventStore)
event.title = "My Event"
event.startDate = Date();
event.endDate = Calendar.date(byAdding: .hour, value: 1, to: Date(), options: .matchFirst)!
event.notes = "A note"
event.calendar = EventStore.defaultCalendarForNewEvents
do
{
try EventStore.save(event, span: .thisEvent)
} catch
{
print("Unable to save event")
}
}
else
{
ErrorAlert(title: "No calendar access", text: "Please give the app access to your calendar. You can do that in your iOS device's settings.")
}
}
I have no idea how this even is possible - the variable is a global that doesn't have anything to do with any view or controller.
The controller the last code block is from is presented modally, if that information is useful at all.
EDIT: CalendarAccess is set in only one place (AppDelegate):
func updateCalendarAccess()
{
Calendar = NSCalendar.current as NSCalendar!
CalendarAccessRequestComplete = false
EventStore = EKEventStore()
EventStore.requestAccess(to: .event, completion: {
(access: Bool, e: Error?) in
CalendarAccessRequestComplete = true
CalendarAccess = access;
return
})
while(!CalendarAccessRequestComplete) { }
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
updateCalendarAccess()
return true
}
EDIT: I get these messages when I tap the button that calls the #IBAction func saveEvent:
2017-02-17 13:58:00.980237 Calendar[16695:5163026] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-02-17 13:58:00.989165 Calendar[16695:5163026] [MC] Reading from public effective user settings.
EDIT: When I dismiss the modally presented view controller (the one with the saveEvent func) and log the value of CalendarAccess, it's true again. And the setter breakpoint isn't hit.
EDIT: It seems like the value of _CalendarAccess goes back to the initial value when I present the VC. If I change var _CalendarAccess = false to var _CalendarAccess = true it is true when the VC is presented. Also, the Calendar variable is nil when the VC is presented, but not otherwise.
Project had same-named framework included, which was causing the compiler to look in multiple places for the same values. Remove framework, problem solved :)

UIMenuController doesn't update menu for first time

I have UITextView on which I want to add highlight as custom menu item. I have registered to following notification UIMenuControllerWillShowMenuNotification.
The method for the notification is something like this:
if textIsHighlighted {
let highlightMenuItem = UIMenuItem(title: "Highlight", action: Selector("highlightText"))
UIMenuController.sharedMenuController().menuItems = [highlightMenuItem]
}
else {
let highlightMenuItem = UIMenuItem(title: "Dehighlight", action: Selector("highlightText"))
UIMenuController.sharedMenuController().menuItems = [highlightMenuItem]
}
Although the first time the menucontroller fails to update even though it executes the part of code. It shows the last value. Where should I write this part of code as I feel that during willShow menuController it's already created and thus fails to update.
Hopefully you've solved this by now, but I've just figured this one out myself:
Other answers have said you can update the menu items by adding it when the UIMenuControllerWillShowMenuNotification is called, but this wasn't working for me (iOS 9, Swift 2).
Instead I implemented the UITextView delegate method: textViewDidChangeSelection and set the relevant menu items there:
func textViewDidChangeSelection(textView: UITextView) {
if self.currentSelectionIsInHighlightedRange() {
self.setUpUnhighlightMenuItem()
} else {
self.setUpHighlightMenuItem()
}
}
private func currentSelectionIsInHighlightedRange() -> Bool {
let allHighlightedRanges = self.document.highlightedRanges()
let selectedTextRange = self.documentView.textView.selectedRange
for range in allHighlightedRanges {
let intersectionRange = NSIntersectionRange(range, selectedTextRange)
if intersectionRange.length > 0 {
return true
}
}
return false
}

Resources