Check if a UIAlertView is showing

后端 未结 10 1351
刺人心
刺人心 2020-11-27 16:45

I have a method that posts HTTP data and displays a UIAlertView if there is an error. If I have multiple HTTP post I will show multiple UIAlertView for every error.

10条回答
  •  孤城傲影
    2020-11-27 17:21

    Some notes on my quest to find the UIAlertView in the view hierarchy:

    I tried to loop through all of the [UIApplication sharedApplication].windows view's recursively but couldn't find anything.

    The windows property of UIApplication docs states the following:

    This property contains the UIWindow objects currently associated with the app. This list does not include windows created and managed by the system, such as the window used to display the status bar.

    So this made me realize that the UIWindow where UIAlertView could be located is not even presented to us.

    HOWEVER, there is also a property on UIApplication called keyWindow. Upon looping through that, I found private classes that would compose an alert view:

    On iOS 7: _UIModalItemHostingWindow, _UIModalItemAlertContentView, _UIBackdropEffectView etc.

    On iOS 8: _UIAlertControllerActionView, _UIAlertControllerShadowedScrollView, _UIBackdropView etc.

    I could not find the UIAlertView that I presented, but rather, a bunch of classes that compose it internally. So to answer the original question, you can probably use the keyWindow property and see if you notice these classes, but your app could get rejected for trying to check for private classes.

    For folks using, the newer, UIAlertController available for iOS 8 could get the reference to it using: [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.

提交回复
热议问题