如何检测硬件键盘是否连接到iPhone? [英] How do I detect that a hardware keyboard is attached to an iPhone?

查看:260
本文介绍了如何检测硬件键盘是否连接到iPhone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

iPad:检测外部键盘是否存在





<我一直在参考图书馆里挖掘,似乎无法在这里找到答案。

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

我假设某个地方有一些API我可以查询是否有外部硬件键盘正在使用中。

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.

更新
我刚从ExternalAccessory.framework尝试了 EAAccessoryManager.connectedAccessories 。这是不行,当硬件键盘启用时它返回一个空数组。

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.

推荐答案

我认为你必须使用以下代码 -

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];
}

希望这对您有用,并记住您必须使用 ExternalAccessory 此代码的框架。

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

这篇关于如何检测硬件键盘是否连接到iPhone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆