Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
See:
https://www.hackingwithswift.com/read/33/8/delivering-notifications-with-cloudkit-push-messages-ckquerysubscription
01. In Appdelegate, add:
import UserNotifications
02. Now put this code into the didFinishLaunchingWithOptions method,
before the return true line:
/**     Return the difrence of 2 dates in minutes        
- parameter fromDate: The first date for the comparison    
- parameter toDate: The second date for the comparison
*/
func dateDiffInMinutes(_ fromDate: Date, toDate: Date) -> Int {   
return (Calendar.current as NSCalendar).components(.minute, from: fromDate, to: toDate, options: NSCalendar.Options(rawValue: 0)).minute!
}
See:https://www.hackingwithswift.com/read/33/8/delivering-notifications-with-cloudkit-push-messages-ckquerysubscription
let database = CKContainer.default().publicCloudDatabase
database.fetchAllSubscriptions { [unowned self] subscriptions, error in
if error == nil {
if let subscriptions = subscriptions {
for subscription in subscriptions {
database.delete(withSubscriptionID: subscription.subscriptionID) { str, error in
if error != nil {
Use Predicate - How can an NSPredicate test that a value contains one of two other values?   
See: https://stackoverflow.com/questions/47445895/how-can-an-nspredicate-test-that-a-value-contains-one-of-two-other-values  
I'm trying to write an NSPredicate that expresses: "If uid contains myID OR targetUser"
If you'd wrote it in "code" and not in a predicate, you'd write if ([uid containsCD:myID] || (uid containsCD:targetUser]) not if ([uid containsCD:myID || targetUser]), where containsCD: is the equivalent of contains[cd]. Same logic for the predicate.                                                                                     
Try:
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"(uid contains[cd] %@) OR (uid contains[cd] %@)", myID, targetUser];
01. See:https://stackoverflow.com/questions/39980063/cloudkit-predicate
Problem:
I am making a messaging application and as push notification service I am using CloudKit.
The push notifications work probably with the predicate 'TRUEPREDICATE', but the problem is if A sends a message to B, C gets a push notification too.My question now is how I can filter the predicate that only the person who gets a message gets a push notification.
let database = CKContainer.default().publicCloudDatabase
let predicate = NSPredicate(format: "TRUEPREDICATE")
let subscription = CKQuerySubscription(recordType: "Message", predicate: predicate, options: .firesOnRecordCreation)
let notification = CKNotificationInfo()
See:https://code.tutsplus.com/tutorials/using-apples-cloudkit-for-push-notifications--cms-28125
See: https://code.tutsplus.com/tutorials/an-introduction-to-the-usernotifications-framework--cms-27250
For local notifications, see: https://medium.com/yay-its-erica/usernotifications-in-swift-3-part-1-d250e54440c1
See: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
See: https://www.appcoda.com/ios10-user-notifications-guide/
Subscribe to record changes: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/SubscribingtoRecordChanges/SubscribingtoRecordChanges.html
01. Open CloudKit dashboard
See: https://medium.com/@adammillers/cksharing-step-by-step-33800c8950d2
01. Public database: this is where you’d store things you want everyone to be able to access. There are no custom zones in Public databases, everything exists in the default zone.
02. Private database: This is where you store records that you don’t want the public to see. The private database has a default zone, and users can create custom zones. Important: put anything you want to share in custom zones in your private database.
03. Shared database: This database holds the “windows into objects” that you have been given access to. The records you see in the shared database exist on other people’s private databases. You just have a window into that object. Another way of saying it is that you are a “contributor” to the items in your shared database, not the owner.
04. Don’t let the term “shared zone” trip you up. There’s no such thing as a CKRecordZone that you put records in where it’s just automatically shared with another user. The sh
See:https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/MaintainingaLocalCacheofCloudKitRecords/MaintainingaLocalCacheofCloudKitRecords.html
01. Maintaining a Local Cache of CloudKit Records
You might want to add a local cache of CloudKit records to your app to support offline use of your
app or to improve performance. Or you may already have a data store for your app and you'd like to
add support for persisting that data in CloudKit as well.
02. The General Workflow
See:https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/DeployingYourCloudKitApp/DeployingYourCloudKitApp.html
Deploying the Schema
After you finalize your schema and test your app in the development environment, you are ready to deploy the schema to production. Deployment promotes the schema to the production environment, but it doesn’t copy the records in the development environment to the production environment. Therefore, after deployment, populate the production environment with records as needed. Then test your app in the production environment. You can continue making changes to the schema in the development environment, but after deploying the schema, you are limited to creating record types and adding fields. Indexes are part of the schema so need to be deployed similar to record type changes. The next time you deploy the development schema, the changes are merged with the production schema.
You must have privileges to edit the production environ
@jazzedge
jazzedge / Swift - CloudKit - Testing your App
Last active November 30, 2017 05:06
Give your CloudKit app a real-world test with multiple simultaneous users by running it on multiple devices using different iCloud accounts. Initially, test your CloudKit app in the development or production environment on designated test devices. Later upload your iOS or tvOS app to iTunes Connect and test your app using the production environm…
See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/TestingYourApp/TestingYourApp.html
Give your CloudKit app a real-world test with multiple simultaneous users by running it on multiple
devices using different iCloud accounts. Initially, test your CloudKit app in the development or
production environment on designated test devices.
Later upload your iOS or tvOS app to iTunes Connect and test your app using the production environment.
Using iTunes Connect, invite internal testers (your team’s iTunes Connect users) or invite external
testers (users specifying only their email addresses) to test your app. Testers download your iOS or
tvOS app using the TestFlight app. Note that apps distributed via TestFlight or the store can’t use