Search code examples
iosswifticloudcloudkit

Saving, Updating or any operation to iCloud is not working with ad-hoc or appstore profiles


In my application saving or updating the CKRecords to the iCloud in release mode(Build with ad-hoc, app store profiles) is not working.

Created the default container in capabilities and find the screenshot attached below.

enter image description here

let container = CKContainer.default
var privateDatabase: CKDatabase?
var currentRecord: CKRecord?
var photoURL: URL?
var recordZone: CKRecordZone?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    privateDatabase = container().privateCloudDatabase
    recordZone = CKRecordZone(zoneName: "HouseZone")

    privateDatabase?.save(recordZone!,
                          completionHandler: {(recordzone, error) in
                            if (error != nil) {
                                self.notifyUser("Record Zone Error",
                                                message: "Failed to create custom record zone.")
                            } else {
                                print("Saved record zone")
                            }
    }) 
}


@IBAction func saveAction(_ sender: Any) {

    if let img = selImageView.image{
        photoURL = self.saveImageToFile(img)
    }


    let asset = CKAsset(fileURL: photoURL!)
    let myRecord = CKRecord(recordType: "Houses",
                            zoneID: (recordZone?.zoneID)!)

    myRecord.setObject(addressTextField.text as CKRecordValue?,
                       forKey: "address")

    myRecord.setObject(descriptionTextView.text as CKRecordValue?,
                       forKey: "comment")

    myRecord.setObject(asset, forKey: "photo")


    let modifyRecordsOperation = CKModifyRecordsOperation(
        recordsToSave: [myRecord],
        recordIDsToDelete: nil)

    if #available(iOS 10.0, *) {
        modifyRecordsOperation.timeoutIntervalForRequest = 10
    } else {
        // Fallback on earlier versions
       // modifyRecordsOperation.timeoutIntervalForRequest = 10
    }

    modifyRecordsOperation.modifyRecordsCompletionBlock =
        { records, recordIDs, error in
            if let err = error {
                self.notifyUser("Save Error", message:
                    err.localizedDescription)
            } else {
                DispatchQueue.main.async {
                    self.notifyUser("Success",
                                    message: "Record saved successfully")
                }
                self.currentRecord = myRecord
            }
    }
    privateDatabase?.add(modifyRecordsOperation)
}

Above code is working in Debug Mode. When I generate the build with Ad-hoc profiles is not working.

Submitted the application to appstore and tried, still not working. Please suggest,


Solution

  • OK so I've repeated the mistake twice now - for me, it was because I added new fields to the CKRecord, but didn't deploy the Development CloudKit database to Production.

    The reason is that Development database is automatically updated when there are new fields to CKRecord, but that won't go to production until you make an explicit deployment. As a result, in Prod environment a write to the database would be rejected because of unknown fields.

    To fix it, go to CloudKit Dashboard, make sure it's in Development environment, then click Deployment, and you should see new fields to be deployed. After the deployment it should work right away.