Skip to content

Commit

Permalink
Merge pull request #1126 from flexibits/master
Browse files Browse the repository at this point in the history
Fixed acknowledgeSystemAlert on iOS 13.1
  • Loading branch information
justinseanmartin authored Oct 15, 2019
2 parents 8a53f36 + a3713f5 commit 4962c6d
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 = application.alert;

#ifdef __IPHONE_13_1
if ([alert isKindOfClass:[self nilElementClass]]) {
// 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];
}
#endif

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 4962c6d

Please sign in to comment.