Skip to content

Commit

Permalink
Fixed acknowledgeSystemAlert on iOS 13.1
Browse files Browse the repository at this point in the history
We need to dig through the hierarchy manually rather than asking for the application's alert
Fixes #1125
  • Loading branch information
ksuther committed Oct 10, 2019
1 parent 8a53f36 commit 5735ec4
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions Classes/UIAutomationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ - (void)tap;
- (void)tapWithOptions:(NSDictionary *)options;
- (NSNumber *)pid;
- (UIAXElement *)uiaxElement;
- (NSArray<UIAElement *> *)elements;
@end

@interface UIAElementArray : NSArray
Expand All @@ -34,6 +35,7 @@ - (BOOL)isVisible;

@interface UIAApplication : UIAElement
- (UIAAlert *)alert;
- (NSArray<UIAElement *> *)windows;
- (NSString *)name;
- (id)appItemScrollView;
@end
Expand Down Expand Up @@ -114,8 +116,26 @@ + (void)deactivateAppForDuration:(NSNumber *)duration {
[[self sharedHelper] deactivateAppForDuration:duration];
}

- (UIAAlert *)currentSystemAlert
{
UIAApplication *application = [[self target] frontMostApp];
UIAAlert *alert;

if (@available(iOS 13.1, *)) {
// application.alert returns UIAElementNil on iOS 13.1
// Instead find the alert by looking for the alert's window and getting the UIAAlert off of it
alert = (UIAAlert *)[[[[[application windows] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIAElement *_Nullable evaluatedObject, NSDictionary<NSString *, id> *_Nullable bindings) {
return [[evaluatedObject valueForKey:@"type"] isEqualToString:@"SBAlertItemWindow"];
}]] firstObject] elements] firstObject];
} else {
alert = application.alert;
}

return alert;
}

- (BOOL)acknowledgeSystemAlert {
UIAAlert* alert = [[self target] frontMostApp].alert;
UIAAlert *alert = [self currentSystemAlert];
// Even though `acknowledgeSystemAlertWithIndex:` checks the index, we have to have
// an additional check here to ensure that when `alert.buttons.count` is 0, subtracting one doesn't cause a wrap-around (2^63 - 1).
if (alert.buttons.count > 0) {
Expand All @@ -126,8 +146,8 @@ - (BOOL)acknowledgeSystemAlert {

// Inspired by: https://github.com/jamesjn/KIF/tree/acknowledge-location-alert
- (BOOL)acknowledgeSystemAlertWithIndex:(NSUInteger)index {
UIAApplication *application = [[self target] frontMostApp];
UIAAlert *alert = application.alert;
UIAAlert *alert = [self currentSystemAlert];

BOOL isIndexInRange = index < alert.buttons.count;
if (![alert isKindOfClass:[self nilElementClass]] && [self _alertIsValidAndVisible:alert] && isIndexInRange) {
[alert.buttons[index] tap];
Expand Down

0 comments on commit 5735ec4

Please sign in to comment.