Search code examples
iosobjective-cmirrorexternal-display

How To Display Custom Content On A External Screen From A iOS Device


I have created one application in Objective C. I want to implement functionality for showing selected content area of the screen on external display. For example, I have total 10 screens and I want to show 4 screens and don't want to show entire portion of the screen, just want to show selected view and area of the screen in external display.

I have done research on this, I found one tutorial, but that tutorial is available in swift language and I want to implement this things in objective c language.


Solution

  • The Swift tutorial in Objective C Code:

    #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