I submitted my app a little over a week ago and got the dreaded rejection email today. It tells me that my app cannot be accepted because I'm using a non-public API; specifically, it says,
The non-public API that is included in your application is firstResponder.
Now, the offending API call is actually a solution I found here on SO:
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
How do I get the current first responder on the screen? I'm looking for a way that won't get my app rejected.
Swift version of @thomas-müller's response
In one of my applications I often want the first responder to resign if the user taps on the background. For this purpose I wrote a category on UIView, which I call on the UIWindow.
The following is based on that and should return the first responder.
iOS 7+
Swift:
Usage example in Swift:
Code below work.
With a category on
UIResponder
, it is possible to legally ask theUIApplication
object to tell you who the first responder is.See this:
Is there any way of asking an iOS view which of its children has first responder status?
You can try also like this:
I didn't try it but it seems a good solution
Here's a solution which reports the correct first responder (many other solutions won't report a
UIViewController
as the first responder, for example), doesn't require looping over the view hierarchy, and doesn't use private APIs.It leverages Apple's method sendAction:to:from:forEvent:, which already knows how to access the first responder.
We just need to tweak it in 2 ways:
UIResponder
so it can execute our own code on the first responder.UIEvent
in order to return the first responder.Here is the code: