我正试图通过airplay连接苹果电视,但问题是如果我连接任何其他外部设备,如蓝牙或其他设备,如窗口中连接的设备连接的问题。所以我想确定连接哪个设备我只有在连接苹果电视时才能启用。
我如何识别它是Apple TV还是其他设备?
这是我如何创建airplay自定义按钮
for (UIButton *button in volumeView.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
self.airplayButton = (UIButton*)button;
button.frame = CGRectMake(0, 0, 30, 23);
button.backgroundColor = [UIColor clearColor];
[self.airplayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
}
}
因此即使某些其他设备连接,alpha也始终会更改按钮。
答案 0 :(得分:0)
之前我已经看过这个,没有简单的方法来确定所连接的设备是否是Apple TV,有一个Airplay Picker可以做到这一点,但它背后的代码/功能似乎不是可用。
您可以做的最好的监控是添加/删除其他屏幕,然后仅在屏幕具有您需要的功能时显示外部内容。
我之前曾在某处读到过你可以获得播放设备的功能并使用这些信息来检测Apple TV,但不幸的是我现在找不到它。如果我找到它,我会添加评论。
目前,您最好的选择是使用此guide
中描述的概念提供的代码在objective-c中,但很容易转换为swift,这是你应该看的主要部分
- (void)checkForExistingScreenAndInitializeIfPresent
{
if ([[UIScreen screens] count] > 1)
{
// Get the screen object that represents the external display.
UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
// Get the screen's bounds so that you can create a window of the correct size.
CGRect screenBounds = secondScreen.bounds;
self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
self.secondWindow.screen = secondScreen;
// Set up initial content to display...
// Show the window.
self.secondWindow.hidden = NO;
}
}
就像我说你可以对此进行编码,以便它检查设备是否支持某些分辨率,这样你就可以排除那些不支持你的UI的设备