如何在运行时确定CloudKit环境? [英] How can CloudKit environment be determined at runtime?

查看:153
本文介绍了如何在运行时确定CloudKit环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种编程方式来发现我的应用程序是在与CloudKit开发环境还是生产环境通信?

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

(我知道我可以看看<$ c $在Entitlements.plist中使用c> com.apple.developer.icloud-container-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.)

推荐答案

到目前为止,您可以在 CKContainer 上使用扩展名。这对于调试和开发目的非常有用。

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
    }
}

说明

扩展@garafajon的答案。如果您查看iOS运行时标头(例如 https://github.com/JaviSoto/iOS10 -Runtime-Headers ),您会看到不公开的类 CKContainerID

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)")

输出为 com.apple.developer.icloud-container-environment = 生产

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

使用 com.apple.developer.icloud-输出容器环境 = 开发

<CKContainer: 0x60800017a1c0; containerID=<CKContainerID: 0x618000035360; containerIdentifier=iCloud.com.yourdomain.yourapp, containerEnvironment="Sandbox">>
<CKContainerID: 0x618000035360; containerIdentifier=iCloud.com.yourdomain.yourapp, containerEnvironment="Sandbox">
2

这篇关于如何在运行时确定CloudKit环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆