Search code examples
iosswiftauthenticationcloudkit

CloudKit App to handle different iCloud accounts


I have an app that keeps user data in a private database using CloudKit and iCloud. I have written code to handle when the user logs out of iCloud and back in as a different user via Settings -> iCloud on their device. I am currently in Beta Testing mode and when I try this as an internal tester, the app crashes when I change accounts. When I test this in development mode using either the Xcode simulator or a real device, my code works great. My question is: should CloudKit apps be able to handle when the iCloud account changes on the device? If so, is this case able to be tested with internal testing via TestFlight. My code to handle account changes is below...

func isCloudAccountAvailableASync() {
  container.accountStatusWithCompletionHandler { (accountStatus, error) in

    switch accountStatus {
    case .Available:
      print("INFO: iCloud Available!")
      // begin sync process by finding the current iCloud user
      self.fetchUserID()
      return
    case .NoAccount:
      print("INFO: No iCloud account")
    case .Restricted:
    print("WARNING: iCloud restricted")
    case .CouldNotDetermine:
      print("WARNING: Unable to determine iCloud status")
    }

    // if you get here, no sync happened so make sure to exec the callback
    self.syncEnded(false)
  }
}


func fetchUserID(numTries: Int = 0) {

  container.fetchUserRecordIDWithCompletionHandler( { recordID, error in

    if let error = error {

      // error handling code

      // reach here then fetchUser was a failure
      self.syncEnded(false)
    }
    else {

      // fetchUserID success

      if self.lastiCloudUser == nil {
        print("INFO: our first iCloud user! \(recordID)")
        self.saveUserID()
      }
      else if !recordID!.isEqual(self.lastiCloudUser) {
        // User has changed!!!!!!!!!!!!!
        self.saveUserID()

        // delete all local data

        // reset the saved server token
        self.saveServerToken(nil)
        // try again with reset fields
      }
      // else user never changed, then just create a zone

      // continue the sync process
      self.initZone()
    }
  })
}

---EDIT/UPDATE:--- Here is a screenshot of my crash log

CrashLogScreenshot

---EDIT/UPDATE 2:--- I was able to generate another crash with a different crash log. This crash log still doesn't point to my code, but at least describes a function...

CrashLog with sharedIMPL_removeObjectFromSet_core


Solution

  • I kept making it crash and somehow got a crash log that included a line of code I could link to a line in my Xcode project. My issue was with NSOrderedSet and iOS 9. That issue can be found here. I do not know why I only got hex in my crash log before, if anyone knows how to deal with hex crash logs I would love to hear it.

    Here are the answers to my original question for anyone out there:

    1. Should CloudKit apps be able to handle when the iCloud account changes on the device? Answer: Yes
    2. If so, is this case able to be tested with internal testing via TestFlight? Answer: Yes