EventKit. How can I get videoconference url from EKEvent - swift

I have an event that has a meet or zoom link. However, I can't find a way to get the link of type https://meet.google.com/.... An example:
let calendars = eventStore.calendars(for: .event)
var events: [EKEvent] = []
for calendar in calendars {
let oneMonthAfter = Date(timeIntervalSinceNow: 30*24*3600)
let predicate = eventStore.predicateForEvents(withStart: Date(), end: oneMonthAfter, calendars: [calendar])
let collection = eventStore.events(matching: predicate)
events.append(contentsOf: collection)
}
let event = events.first! // This event has a meet link associated but I can't get the link
EventKit has a class named EKVirtualConferenceURLDescriptor (more info here). However, the documentation isn't good and only talks about creating a new one, not how we can get it from an existing event.

Related

Cloudkit notifications only when certain parts of record are changed

I am creating an app that has profiles stored as records on CloudKit. When one user adds another user. The Requester is added to the requestees. Request list. (this is where I would like a notification with the requesters name going to the requestee).
Then once the requestee accepts they are added to the requesters friendlist property on CloudKit and visa-versa.
However all the documentation I have found only shows how to send a notification anytime the CKRecord/Profile is updated in anyway. Which is what I currently have implemented but too many notifications are sent (I.e. when they change their profile name, avatar, etc...) Here is my current implementation:
func subscribeToNotifications(profile: OKGNProfile) async {
let predicate = NSPredicate(format: "name == %#", profile.name)
let subscription = CKQuerySubscription(recordType: "OKGNProfile", predicate: predicate, subscriptionID: "friendRequestAddedToDatabase", options: .firesOnRecordUpdate)
let notification = CKSubscription.NotificationInfo()
notification.title = "Friend Request"
notification.alertBody = "Open friend feed in app to see new friend request from \(profile.name)!"
notification.soundName = "default"
subscription.notificationInfo = notification
CKContainer.default().publicCloudDatabase.save(subscription) { returnedSub, returnedError in
if let error = returnedError {
print(error)
} else {
print("✅ sucessfully subscribed to notifications")
}
}
}

Empty EKEventStore calendar list after authentication

I would like to grab the names of my calendars on my Mac. I have 1 that's listed as "On My Mac", another (Birthdays) under "Other", and 4 that are pulled in from Google Calendar.
let eventStore = EKEventStore()
eventStore.requestAccess(to: EKEntityType.event) { (accessGranted, error) in
if (accessGranted) {
let calendars = eventStore.calendars(for: .event)
print(calendars)
}
}
The goal is to determine how many days remain until the next event across all selected calendars.
Using the above code, I'm getting back an empty array. How can I access these calendars?

Swift saving new comments using CloudKit

I'm trying to make an app which stores a user's comment on CloudKit and then shows it to the other users. User simply enters his/her comment on a text field and clicks on a submit button to submit his/her comment (just like a restaurant app). However, I can't seem to find the correct way no matter what I try. Here is my code, I'd be very glad for any help as I've been stuck on this problem for some time now. Thank you very much in advance!
#IBAction func OnSubmitTouched(_ sender: UIButton) {
if (textField.text != ""){
let newComment = CKRecord(recordType: "Users")
let publicDB = CKContainer.default().publicCloudDatabase
newComment.setValue(textField.text!, forKey: "comment")
publicDB.save(newComment){
rec ,err in
if let error = err {
print(err.debugDescription)
return
}
publicDB.fetch(withRecordID: newComment.recordID){
rec, err in
print(rec!["comment"]!)
return
}
}
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "comment", predicate: predicate)
let operation = CKQueryOperation(query: query)
var commentRecords: [CKRecord] = []
operation.recordFetchedBlock = { record in
commentRecords.append(record)
}
operation.queryCompletionBlock = { cursor, error in
print(commentRecords)
}
CKContainer.default().publicCloudDatabase.add(operation)
}
}
You are getting a permission error because Users is a protected record type that CloudKit creates automatically for users of your app. You should name it something else and then it should work.
For example, you could make a Comment record type. This might need a field that references the current user. You can get the current userID with:
CKContainer fetchUserRecordIDWithCompletionHandler:
Here is the Apple documentation for this method.
It is also possible to use the Users record type, but you would have to find the existing userID from CloudKit as above then build a record around that.
See also this answer.

Add Reminder List programmatically

I'm building an application that interacts with the macOS Reminder App. I'm trying to create a new Reminder list into which I later can import reminders.
This is what I have so far:
func setCalendar(_ type: EKEntityType) {
let eventStore = EKEventStore()
let newCalendar = EKCalendar(for: type, eventStore: eventStore)
newCalendar.title="newcal"
print("Cal: " + newCalendar.title)
try? eventStore.saveCalendar(newCalendar, commit: true)
}
However, there is no reminder list being created.
The problem is that you have omitted to specify the new calendar's .source. You cannot create a calendar of any kind (event or reminder) without doing that.

SWIFT: do I have someone set a record with different text every time?

So I made a chatroom and when someone sends a message they also add a Subscription in my cloud kit database but the problem is there cant be more then one of the same name that is a subscription and I want them to be able to set more subscriptions then one. Here is some code:
func setupCloudKitSubscription () {
let userDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.boolForKey("subscribed") == false {
let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)
let subscription = CKSubscription(recordType: "Extra1", predicate: predicate, options: CKSubscriptionOptions.FiresOnRecordCreation)
let notificationInfo = CKNotificationInfo()
notificationInfo.alertLocalizationKey = "New Sweet"
notificationInfo.shouldBadge = true
subscription.notificationInfo = notificationInfo
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveSubscription(subscription) { (subscription:CKSubscription?, error:NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
}else{
userDefaults.setBool(true, forKey: "subscribed")
userDefaults.synchronize()
You see how it says recordType: "Extra1" how can I made the "Extra1" different text every time someone makes a subscription? Thanks!
Your question is not completely clear. I think what you wanted to ask is that you want the subscription to send you a different message with each notification.
You could set it to display one or more fields of the record. For doing that you should use something like this:
notificationInfo.alertLocalizationKey = "Response: %1$#"
notificationInfo.alertLocalizationArgs = ["responseField"]
Then you also need this in your Localization.Strings file.
"Response: %1$#" = "Response: %1$#";