Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect that a hardware keyboard is attached to an iPhone? [duplicate]

Tags:

ios

iphone

Possible Duplicate:
iPad: detect if external keyboard is present

I've been digging around in the reference library and just can't seem to find the answer here.

I'm assuming there's some API somewhere that I can query to find out if an external hardware keyboard is in use or not.

Update I just tried the EAAccessoryManager.connectedAccessories from the ExternalAccessory.framework. That was a no-go, it returns an empty array when the hardware keyboard is enabled.

like image 1000
user174448 Avatar asked Jul 01 '10 15:07

user174448


1 Answers

I think you have to use following code -

- (void)viewDidLoad {
    UIView*  _noExternalAccessoriesPosterView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_noExternalAccessoriesPosterView setBackgroundColor:[UIColor whiteColor]];
    _noExternalAccessoriesLabelView = [[UILabel alloc] initWithFrame:CGRectMake(60, 170, 240, 50)];
    [_noExternalAccessoriesLabelView setText:@"No Accessories Connected"];
    [_noExternalAccessoriesPosterView addSubview:_noExternalAccessoriesLabelView];
    [[self view] addSubview:_noExternalAccessoriesPosterView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];

    _eaSessionController = [EADSessionController sharedController];
    _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];

    [self setTitle:@"Accessories"];

    if ([_accessoryList count] == 0) {
        [_noExternalAccessoriesPosterView setHidden:NO];
    } else {
        [_noExternalAccessoriesPosterView setHidden:YES];
    }
}

- (void)_accessoryDidConnect:(NSNotification *)notification {
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
    [_accessoryList addObject:connectedAccessory];

    if ([_accessoryList count] == 0) {
        [_noExternalAccessoriesPosterView setHidden:NO];
    } else {
        [_noExternalAccessoriesPosterView setHidden:YES];
    }

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([_accessoryList count] - 1) inSection:0];
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

Hope this works for you and remember you have to use ExternalAccessory Framework for this code.

like image 88
Nishant Mahajan Avatar answered Sep 23 '22 17:09

Nishant Mahajan