Example for iOS 12 on main UIViewController, you dont need even real external display programming.
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:@selector(checkExternalScreen) withObject:nil afterDelay:1.0];
}
- (void) checkExternalScreen
{
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"Screens"
message:[NSString stringWithFormat:@"Screens connected:%ld ",UIScreen.screens.count]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:NSLocalizedString(@"AceptarKey", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:NULL];
}
-(IBAction) crashButton:(id)sender
{
@throw NSInternalInconsistencyException;
}
On iOS 12:
1) First Launch: Screens connected: 2
2) Press crashButton
3) Launch again the app: Screens connected: 1
On iOS 11:
1) First Launch: Screens connected: 2
2) Press crashButton
3) Launch again the app: Screens connected: 2
For detecting on iOS 12 the screen again, you need unplug and plug HDMI (lighting to HDMI). Or you can detect again the screen if you are launching other different app before your crashed app.
EDIT: For reproducing this bug, you must launch the app from dashboard and not from XCode.
Same issue with Airplay. Is not hardware fault. iOS 11 is working properly.
Thanks.