23

The accessibility identifier is a developer generated ID for GUI objects, which can be used for automation tests.

A UIBarButtonItem does not implement UIAccessibilityIdentification. However is there a possibility that I can assign an accessibility identifier?

Chris
  • 8,031
  • 10
  • 41
  • 67

4 Answers4

18

You could subclass UIBarButtonItem and implement the UIAccessibilityIdentification protocol in that subclass, lets's say BarButtonWithAccesibility.

In BarButtonWithAccesibility.h:

@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification>

@property(nonatomic, copy) NSString *accessibilityIdentifier NS_AVAILABLE_IOS(5_0);

The only (strict) requirement for adhering to this protocol is defining the accessibilityIdentifier property.

Now in your view controller, let's say in viewDidLoad, you could set up a UIToolbar and add your subclassed UIBarButtonItem:

#import "BarButtonWithAccesibility.h"

- (void)viewDidLoad{

    [super viewDidLoad];

    UIToolbar *toolbar = [[UIToolbar alloc]  initWithFrame:CGRectMake(0, 0, 320, 44)];

    BarButtonWithAccesibility *myBarButton = [[BarButtonWithAccesibility alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonPressed:)];
    myBarButton.accessibilityIdentifier = @"I am a test button!";

    toolbar.items = [[NSArray alloc] initWithObjects:myBarButton, nil];
    [self.view addSubview:toolbar];
}

And within the buttonPressed: you could verify that you have access to the accessibilityIdentifier:

- (void)buttonPressed:(id)sender{
    if ([sender isKindOfClass:[BarButtonWithAccesibility class]]) {
        BarButtonWithAccesibility *theButton = (BarButtonWithAccesibility *)sender;
        NSLog(@"My accesibility identifier is: %@", theButton.accessibilityIdentifier);
    }
}

Hope this helps.

Don Miguel
  • 882
  • 9
  • 19
  • 9
    Looks like iOS 8 fixed the issue, and `accessibilityIdentifier` can now also be used with `UIBarButtonItem`. – fabb Sep 16 '14 at 10:52
  • @fabb - Table view cells are not showing in scripts in iOS 8. Do u have any idea – Sandeep Sep 30 '14 at 10:34
  • It's not working with _Appium Inspector_. It shows 'BarButtonWithAccesibility' (and 'UIBarButtonItem') as 'UIAButton' and it sets name as icon file name. – Radek Wilczak May 12 '15 at 09:49
5

As of iOS 5 you can do it like this:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] init...;
btn.accessibilityLabel = @"Label";
Stas Zhukovskiy
  • 799
  • 9
  • 21
  • Is the accessibility identifier the same as label? – Chris Dec 19 '13 at 12:00
  • not sure what you mean, but i'll give it a try: by default it's nil, you have to set it yourself. – Stas Zhukovskiy Dec 20 '13 at 12:36
  • u can use tag instead accessibility and also get object from tag. – Aklesh Rathaur Dec 21 '13 at 07:22
  • 9
    The identifier is not the same as the label. If I'm localizing the element then the label may change, but you'd want the identifier to stay the same - typical for cases where automated testing uses accessibility to identify UI elements. – tyler May 27 '14 at 18:56
3

If you have UIToolbar created inside that if you want to create multiple UIBarButtonItem programatically then it can be accessed like that and set accessibilityLabel as well like that below:-

    -(void)viewDidAppear:(BOOL)animated
    {
        UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];
        [self.customToolBar setItems:[NSArray arrayWithObject:infoButtonItem]];
//Here if you have muliple you can loop through it 
       UIView *view = (UIView*)[self.customToolBar.items objectAtIndex:0];
    [view setAccessibilityLabel:NSLocalizedString(@"Test", @"")];
    }
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
2

The subclassing UIBarButtonItem is a good solution. Depending on your needs, though, it may make more sense to simply assign the accessibilityIdentifier to the custom image of your UIBarButtonItem, assuming your UIBarButtonItem uses a custom image.

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
Ben Boral
  • 355
  • 3
  • 11