如何从 iOS 设备在外部屏幕上显示自定义内容
How To Display Custom Content On A External Screen From A iOS Device
我在 Objective C 中创建了一个应用程序。我想实现在外部显示器上显示屏幕选定内容区域的功能。例如,我总共有 10 个屏幕,我想显示 4 个屏幕,不想显示整个屏幕部分,只想在外部显示器中显示选定的视图和屏幕区域。
我对此进行了研究,我找到了一个 tutorial,但是该教程有 swift 语言版本,我想用 objective c 语言实现这些内容。
Swift教程在Objective C代码:
#import "ViewController.h"
#import "ExternalScreenViewController.h"
@interface ViewController ()
{
UIWindow *externalWindow;
UIWebView *webView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webView = [[UIWebView alloc] init];
[self setupScreenNotifications];
NSURL *url = [NSURL URLWithString:@"http://www.spazstik-software.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
if ([[UIScreen screens] count] > 1) {
[self setupExternalScreen:[UIScreen screens][1]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setupScreenNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidConnect:) name:UIScreenDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil];
}
-(void)externalScreenDidConnect:(NSNotification *)notification {
UIScreen *screen = (UIScreen *)[notification object];
if (screen != nil) {
[self setupExternalScreen:screen];
}
}
-(void)externalScreenDidDisconnect:(NSNotification *)notification {
id obj = [notification object];
if (obj != nil) {
[self teardownExternalScreen];
}
}
-(void)setupExternalScreen:(UIScreen *)screen {
ExternalScreenViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ExternalScreen"];
externalWindow = [[UIWindow alloc]initWithFrame:screen.bounds];
externalWindow.rootViewController = vc;
externalWindow.screen = screen;
externalWindow.hidden = false;
}
-(void)teardownExternalScreen {
if (externalWindow != nil) {
externalWindow.hidden = true;
externalWindow = nil;
}
}
@end
对于 iOS 13 岁及以上,将 setupExternalScreen
方法更改为此
来源:
-(void)setupExternalScreen:(UIScreen *)screen shouldRecurse:(bool)recurse {
UIWindowScene* matchingWindowScene = nil;
// For iOS13 and up find matching UIWindowScene
for(UIScene *aScene in UIApplication.sharedApplication.connectedScenes){
UIWindowScene *windowScene = (UIWindowScene*)aScene;
// Look for UIWindowScene that has matching screen
if (windowScene.screen == screen) {
matchingWindowScene = windowScene;
break;
}
}
if (matchingWindowScene == nil) {
// UIWindowScene has not been created by iOS rendered yet
// Lets recall self after delay of two seconds
if (recurse) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self setupExternalScreen:screen shouldRecurse:NO];
});
}
// Dont proceed further if none found
return;
}
// Instantiate view controller
ExternalScreenViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ExternalScreen"];
// Setup external window
externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
[externalWindow setRootViewController: vc];
// Set scene
[externalWindow setWindowScene:matchingWindowScene];
// Show the window.
[externalWindow setHidden:NO];
}
我在 Objective C 中创建了一个应用程序。我想实现在外部显示器上显示屏幕选定内容区域的功能。例如,我总共有 10 个屏幕,我想显示 4 个屏幕,不想显示整个屏幕部分,只想在外部显示器中显示选定的视图和屏幕区域。
我对此进行了研究,我找到了一个 tutorial,但是该教程有 swift 语言版本,我想用 objective c 语言实现这些内容。
Swift教程在Objective C代码:
#import "ViewController.h"
#import "ExternalScreenViewController.h"
@interface ViewController ()
{
UIWindow *externalWindow;
UIWebView *webView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webView = [[UIWebView alloc] init];
[self setupScreenNotifications];
NSURL *url = [NSURL URLWithString:@"http://www.spazstik-software.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
if ([[UIScreen screens] count] > 1) {
[self setupExternalScreen:[UIScreen screens][1]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setupScreenNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidConnect:) name:UIScreenDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil];
}
-(void)externalScreenDidConnect:(NSNotification *)notification {
UIScreen *screen = (UIScreen *)[notification object];
if (screen != nil) {
[self setupExternalScreen:screen];
}
}
-(void)externalScreenDidDisconnect:(NSNotification *)notification {
id obj = [notification object];
if (obj != nil) {
[self teardownExternalScreen];
}
}
-(void)setupExternalScreen:(UIScreen *)screen {
ExternalScreenViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ExternalScreen"];
externalWindow = [[UIWindow alloc]initWithFrame:screen.bounds];
externalWindow.rootViewController = vc;
externalWindow.screen = screen;
externalWindow.hidden = false;
}
-(void)teardownExternalScreen {
if (externalWindow != nil) {
externalWindow.hidden = true;
externalWindow = nil;
}
}
@end
对于 iOS 13 岁及以上,将 setupExternalScreen
方法更改为此
来源:
-(void)setupExternalScreen:(UIScreen *)screen shouldRecurse:(bool)recurse {
UIWindowScene* matchingWindowScene = nil;
// For iOS13 and up find matching UIWindowScene
for(UIScene *aScene in UIApplication.sharedApplication.connectedScenes){
UIWindowScene *windowScene = (UIWindowScene*)aScene;
// Look for UIWindowScene that has matching screen
if (windowScene.screen == screen) {
matchingWindowScene = windowScene;
break;
}
}
if (matchingWindowScene == nil) {
// UIWindowScene has not been created by iOS rendered yet
// Lets recall self after delay of two seconds
if (recurse) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self setupExternalScreen:screen shouldRecurse:NO];
});
}
// Dont proceed further if none found
return;
}
// Instantiate view controller
ExternalScreenViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ExternalScreen"];
// Setup external window
externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
[externalWindow setRootViewController: vc];
// Set scene
[externalWindow setWindowScene:matchingWindowScene];
// Show the window.
[externalWindow setHidden:NO];
}