Apple cloudkit sample encryption

Overview

CloudKit Samples: Encryption

Goals

This project demonstrates using encrypted values with CloudKit and iCloud containers. CloudKit encrypts data with key material stored in a customer’s iCloud Keychain. If a customer loses access to their iCloud Keychain, CloudKit cannot access the key material previously used to encrypt data stored in the cloud, meaning that data can no longer be decrypted and accessed by the customer. More information about this is covered in the “Error Handling” section below.

Prerequisites

Setup Instructions

  • Ensure the simulator or device you run the project on is signed in to an Apple ID account with iCloud enabled. This can be done in the Settings app.
  • If you wish to run the app on a device, ensure the correct developer team is selected in the “Signing & Capabilities” tab of the Encryption app target, and a valid iCloud container is selected under the “iCloud” section.

Using Your Own iCloud Container

  • Create a new iCloud container through Xcode’s “Signing & Capabilities” tab of the Queries app target.
  • Update the containerIdentifier property in Config.swift with your new iCloud container ID.

How it Works

This project only differs very slightly from other samples, in that it uses the encryptedValues property of CKRecord in two places.

Setting the phoneNumber value in ViewModel.swift addContact:

contactRecord.encryptedValues["phoneNumber"] = phoneNumber

…and retrieving the phoneNumber value (in Contact.swift Contact.init(record:)):

let phoneNumber = record.encryptedValues["phoneNumber"] as? String

You can confirm that the value is encrypted by viewing the schema in CloudKit Dashboard and confirming that the phoneNumber custom field under the Contact type shows “Encrypted Bytes” for its “Field Type”.

Notes on Encrypted Fields

  • Encrypted fields cannot have indexes.
  • Existing fields in a CloudKit schema are not eligible for encryption.
  • CKReference fields cannot be encrypted.
  • CKAsset fields are encrypted by default, and therefore should not be set as encryptedValues fields.
  • CKRecordID, CKRecordZoneID or any other data types that is not one of NSString, NSNumber, NSDate, NSData, CLLocation and NSArray cannot be set as encryptedValues fields.

Error Handling

  • As described above, CloudKit encrypts data with key material store in a customer’s iCloud Keychain. If this key material is lost, for example by a customer resetting their iCloud Keychain, CloudKit is unable to decrypt previously encrypted data and returns a specific error code.
  • This is demonstrated in the handleError function, where a CKError with a zoneNotFound code may have a CKErrorUserDidResetEncryptedDataKey NSNumber value in the userInfo dictionary.
  • It is outside the scope of this sample, but it is recommended when encountering this error to first delete the relevant zone(s), re-create them, and then re-upload locally-cached data from the device to those zones. This new data is encrypted using the new key material from the user’s iCloud Keychain.

Things To Learn

  • Creating, fetching from, and saving to a custom zone.
  • Saving and retrieving encrypted values in a record in the remote Private Database.
  • Handling errors specifically related to using Encrypted Fields.
  • Using XCTest to asynchronously test creating new temporary records, fetching records, and cleaning up records created during tests with tearDown functions.

Further Reading

You might also like...
A Swift sample code to reads ISO 10303-21 exchange structures (STEP P21 files for AP242) split into multiple files using external references approach.

multipleP21ReadsSample A Swift sample code to reads ISO 10303-21 exchange structures (STEP P21 files for AP242) split into multiple files using extern

Sample projects for using .NET and Swift with SourceGear Bridge

bridge-samples-swift This repo contains various sample projects which show using .NET and Swift with SourceGear Bridge. (Note that SourceGear Bridge i

 Sample iOS App  - A collection of examples and patterns for Unit Testing, UI Testing, handling Result/Optionals, writing documentation
 Sample iOS App - A collection of examples and patterns for Unit Testing, UI Testing, handling Result/Optionals, writing documentation

 Sample iOS App - A collection of examples and patterns for Unit Testing, UI Testing, handling Result/Optionals, writing documentation, and more

This sample app use the Star Wars public api to show a list of characters

StarWars-MVVM In this sample app, I use the Star Wars public api to show a list of characters from the Star Wars movie series. There are two goals I w

My Winning Submission for Apple's WWDC 2021 Swift Student Challenge
My Winning Submission for Apple's WWDC 2021 Swift Student Challenge

Symmetries This playground book gives a quick insight into the symmetry groups of 3-dimensional objects like the platonic solids. The user can play ar

Simple examples that help you get started with Appwrite + Apple with SwiftUI (=❤️)
Simple examples that help you get started with Appwrite + Apple with SwiftUI (=❤️)

Appwrite's Apple Playground 🎮 Appwrite playground is a simple way to explore the Appwrite API & Appwrite Apple SDK. Use the source code of this repos

Official demo app released by Apple to teach SwiftUI

Landmarks This repository contains a demo application that I developed while following Apple's SwiftUI tutorials. Cloning Clone the repository using c

My winning contribution to Apple's Swift Student Challenge 2021

SynthApp My winning contribution to Apple's Swift Student Challenge 2021 This repository contains an Xcode project and the final Swift Playgrounds fil

Example app source code developed by swift language from apple
Example app source code developed by swift language from apple

AboutMe Example app source code developed by swift language from apple. Display data from a central source in multiple views. Welcome to the About Me

Owner
null
Sample Code for WWDC21

WWDC21 Sample Code Accessibility Create Accessible Experiences for watchOS Creating Accessible Views WWDC21 Challenge: Large Text Challenge WWDC21 Cha

Artem Novichkov 84 Dec 27, 2022
Sample app to demonstrate using Firebase features

Photoram Sample app to demonstrate using Firebase features Uses: Firebase authentification for login and registrations Firebase storage to keep image

Anna Zharkova 3 Jun 4, 2021
This is a sample project that supplements the tutorial written over at my blog on 'Building a music recognization app in SwiftUI with ShazamKit'

Shazam-Kit-Tutorial This is a sample project that supplements the tutorial written over at my blog on 'Building a music recognization app in SwiftUI w

Swapnanil Dhol 7 Mar 17, 2022
A sample iOS app built using the Clean Swift architecture

Main Purpose is to create a simple project for Clean-Swift This project wrote with using Clean-Swift and MVP Design Pattern

Eyup Cimen 8 Oct 24, 2022
InputMethodKit Sample App with macOS11, Xcode13, Swift5.5 in 2021.

What is this? This is a sample implementation of IMKit App with Swift/SwiftUI. Working Environment macOS 11.5 Swift 5.5 Xcode13 (beta) Procedure to ma

ensan 19 Nov 4, 2022
REDUX like architecture sample for iOS

perdux_sample_swiftUI REDUX like architecture sample for iOS (target 14.*) Motivation: to share reactive architecture approach with single direction d

alexis 3 Jan 27, 2022
Sample app to demonstrate the integration code and working of Dyte SDK for iOS, using Swift

docs-template by dyte ADD_DESCRIPTION_HERE Explore the docs » View Demo · Report Bug · Request Feature Table of Contents About the Project Built With

Dyte 8 Nov 26, 2021
UIKit Chat List Sample

When I tried to make a UI like this, I faced a problem. When keyboard appears, TableView doesn't scroll automatically. I wanted to make the feature tableview scrolls automatically so I tried to make this project. Below images describe the difference without feature and with feature properly.

null 3 Jan 1, 2022
A sample app that will display some airport information using MVVM pattern

Simulator.Screen.Recording.-.iPhone.11.-.2021-11-15.at.14.27.41.mp4 AirportDisplayApp A sample app that will display some airport information using MV

Sabrina Hoque Tuli 0 Nov 15, 2021