is forKey:fileSize considered accessing non-public API?

is forKey:fileSize considered accessing non-public API?

has your app been rejected at review stage due to this?

let resources = PHAssetResource.assetResources(for: asset)
if let resource = resources.first {
    if let fileSize = resource.value(forKey: "fileSize") as? Int {
        return fileSize
    }
}
Answered by DTS Engineer in 820216022

I want to be crystal clear about this.

WARNING Don’t use key-value-coding (KVC) APIs to access implementation details of Apple’s public classes.

It’s possible that such code may be caught by the app ingestion process. However, even if that’s not the case then there’s no guarantee that they’ll work as you expect. Such properties are implementation details and can change at any time. Moreover, they might have non-obvious restrictions. For example, they might not be safe to access from the context in which you’re accessing them, resulting in hard-to-debug data races.

I’m not an expert in PhotoKit but my understanding is that it does provide a mechanism to get at the raw bytes of the resource, and you can get the count from that. If that’s not sufficient for your requirement, I encourage you to file an enhancement request for the features that you need. And if you do file an ER, please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

is forKey:fileSize considered accessing non-public API?

value(forKey:) itself is public API of course, so you likely wouldn’t get rejected by whatever means App Review uses to try to flag more obvious non-public API usage.

However, you are relying on undefined behavior, which is still a bad idea for compatibility. The documentation of that class doesn’t mention a property by that name being supported via KVC.

In fact, at this very moment, an engineer at Apple may be busy rewriting PHAssetResource in a way that still conforms to the documented API but no longer responds to fetching that particular property via KVC, and your code would stop working in the next OS release. Make sure your app can handle that.

I want to be crystal clear about this.

WARNING Don’t use key-value-coding (KVC) APIs to access implementation details of Apple’s public classes.

It’s possible that such code may be caught by the app ingestion process. However, even if that’s not the case then there’s no guarantee that they’ll work as you expect. Such properties are implementation details and can change at any time. Moreover, they might have non-obvious restrictions. For example, they might not be safe to access from the context in which you’re accessing them, resulting in hard-to-debug data races.

I’m not an expert in PhotoKit but my understanding is that it does provide a mechanism to get at the raw bytes of the resource, and you can get the count from that. If that’s not sufficient for your requirement, I encourage you to file an enhancement request for the features that you need. And if you do file an ER, please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

is forKey:fileSize considered accessing non-public API?
 
 
Q