Using "targets" value of . interactivePopGestureRecognizer

I would like to use the "targets" value of UINavigationController's .interactivePopGestureRecognizer for my custom gesture recognizer. Does it infringe upon the private API rule of Apple or is it safe to use?

guard let interactivePopGestureRecognizer = interactivePopGestureRecognizer, let targets = interactivePopGestureRecognizer.value(forKey: "targets") else { return }

 customGestureRecognizer.setValue(targets, forKey: "targets")

Thank you!

Answered by Frameworks Engineer in 682414022

Yes, it does break the private API rule. Using value(forKey:) to obtain a value that is not exposed by API is the equivalent of using private API and exposes you to the risk that the underlying code may change in ways that cause your application to crash on a future version of iOS.

Even if the private API doesn't change, there is no publicly exposed contract for those APIs, so you cannot be guaranteed that their function will not change, or that you will not run afoul of a requirement of those APIs. Your example where you then set the new "targets" for another gesture recognizer may subtly break that gesture recognizer in various ways because the underlying change you are making may not have been validated by the customGestureRecognizer you are setting it on. This may not even be obvious early on, and may manifest in instabilities that are hard to trace back to this.

I know a lot of code out there says "use [set]value(forKey:) to solve [some issue]" but outside of a few narrow cases, its relying on private API, or it would just call a relevant public API instead.

I would absolutely welcome you to start another thread to discuss the issue you are trying to solve though!

Accepted Answer

Yes, it does break the private API rule. Using value(forKey:) to obtain a value that is not exposed by API is the equivalent of using private API and exposes you to the risk that the underlying code may change in ways that cause your application to crash on a future version of iOS.

Even if the private API doesn't change, there is no publicly exposed contract for those APIs, so you cannot be guaranteed that their function will not change, or that you will not run afoul of a requirement of those APIs. Your example where you then set the new "targets" for another gesture recognizer may subtly break that gesture recognizer in various ways because the underlying change you are making may not have been validated by the customGestureRecognizer you are setting it on. This may not even be obvious early on, and may manifest in instabilities that are hard to trace back to this.

I know a lot of code out there says "use [set]value(forKey:) to solve [some issue]" but outside of a few narrow cases, its relying on private API, or it would just call a relevant public API instead.

I would absolutely welcome you to start another thread to discuss the issue you are trying to solve though!

Thank you for your answer Ricewind!

Using "targets" value of . interactivePopGestureRecognizer
 
 
Q