Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can CloudKit environment be determined at runtime?

Is there a programmatic way to discover whether my app is talking to the CloudKit Development or Production environment?

(I know I can look at the com.apple.developer.icloud-container-environment key in Entitlements.plist to make an intelligent guess, but that doesn't really -prove- which environment I'm using.)

like image 672
Greg Maletic Avatar asked Sep 08 '15 18:09

Greg Maletic


1 Answers

As of now you can use an extension on CKContainer. This can be very helpful for debugging and development purposes.

extension CKContainer {
    public var isProductionEnvironment:Bool {
        let containerID = self.value(forKey: "containerID") as! NSObject // CKContainerID
        return containerID.value(forKey: "environment")! as! CLongLong == 1
    }
}

Explanation:

Expanding on @garafajon's answer. If you have a look at the iOS Runtime Headers (e.g. https://github.com/JaviSoto/iOS10-Runtime-Headers), you'll see that there is the class CKContainerID that is not publicly available.

You can access the environment property using key value coding like this:

let container = CKContainer.default()
let containerID = container.value(forKey: "containerID") as! NSObject // CKContainerID
let environment = containerID.value(forKey: "environment")!
print("\(container)")
print("\(containerID)")
print("\(environment)")

Output with com.apple.developer.icloud-container-environment=Production:

<CKContainer: 0x60800017a040; containerID=<CKContainerID: 0x608000232ea0; containerIdentifier=iCloud.com.yourdomain.yourapp, containerEnvironment="Production">>
<CKContainerID: 0x608000232ea0; containerIdentifier=iCloud.com.yourdomain.yourapp, containerEnvironment="Production">
1

Output with com.apple.developer.icloud-container-environment=Development:

<CKContainer: 0x60800017a1c0; containerID=<CKContainerID: 0x618000035360; containerIdentifier=iCloud.com.yourdomain.yourapp, containerEnvironment="Sandbox">>
<CKContainerID: 0x618000035360; containerIdentifier=iCloud.com.yourdomain.yourapp, containerEnvironment="Sandbox">
2
like image 94
Klaas Avatar answered Sep 19 '22 13:09

Klaas