See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/CreatingaSchemabySavingRecords/CreatingaSchemabySavingRecords.html
Improve the user’s experience by verifying that the user is signed in to their iCloud account
before saving records. If the user is not signed in, present an alert instructing the user how
to enter their iCloud credentials and enable iCloud Drive.
Insert your code that saves records in the else clause below.
CKContainer.default().accountStatus(completionHandler: {(_ accountStatus: CKAccountStatus, _ error: Error?) -> Void in
if accountStatus == .noAccount {
var alert = UIAlertController(title: "Sign in to iCloud", message: "Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .cancel, handler: nil))
self.present(alert, animated: true) { _ in }
}
else {
// Insert your just-in-time schema code here
}
})