{"id":220,"date":"2017-07-03T21:21:58","date_gmt":"2017-07-03T11:21:58","guid":{"rendered":"http:\/\/www.codenkeg.com\/blog\/?p=220"},"modified":"2017-07-06T20:36:20","modified_gmt":"2017-07-06T10:36:20","slug":"adding-airplay","status":"publish","type":"post","link":"https:\/\/www.codenkeg.com\/blog\/2017\/07\/03\/adding-airplay\/","title":{"rendered":"Adding AirPlay"},"content":{"rendered":"<p>I&#8217;ve been meaning to look at this since 2013. Easy to get a second screen. Next, interacting with it.<\/p>\n<p>References:<br \/>\n<a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/WindowsViews\/Conceptual\/WindowAndScreenGuide\/WindowScreenRolesinApp\/WindowScreenRolesinApp.html\" target=\"_blank\">Understanding Windows and Screens<\/a><br \/>\n<a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uiwindow\" target=\"_blank\">UIWindow<\/a><br \/>\n<a href=\"https:\/\/developer.apple.com\/documentation\/uikit\/uiscreen\" target=\"_blank\">UIScreen<\/a><br \/>\n<a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/WindowsViews\/Conceptual\/WindowAndScreenGuide\/UsingExternalDisplay\/UsingExternalDisplay.html\" target=\"_blank\">Presenting Content on an External Display<\/a><\/p>\n<pre class=\"lang:swift\" title=\"ViewController.swift\" mark=\"7-10\">\r\n\/\/  ViewController.swift\r\n\r\nimport UIKit\r\n\r\nclass ViewController: UIViewController {\r\n    \r\n    override func loadView() {\r\n        let overall = UIView(frame: UIScreen.main.bounds)\r\n        self.view = overall\r\n    }\r\n\r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \/\/ Do any additional setup after loading the view, typically from a nib.\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"lang:swift\" title=\"SecondViewController.swift\" mark=\"7-10\">\r\n\/\/  SecondViewController.swift\r\n\r\nimport UIKit\r\n\r\nclass ViewController: UIViewController {\r\n    \r\n    override func loadView() {\r\n        let overall = UIView(frame: UIScreen.main.bounds)\r\n        self.view = overall\r\n    }\r\n\r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \/\/ Do any additional setup after loading the view, typically from a nib.\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"lang:swift\" title=\"AppDelegate.swift\" mark=\"10,15-23,28-76\">\r\n\/\/  AppDelegate.swift\r\n\r\nimport UIKit\r\nimport CoreData\r\n\r\n@UIApplicationMain\r\nclass AppDelegate: UIResponder, UIApplicationDelegate {\r\n\r\n    var window: UIWindow?\r\n    var secondWindow: UIWindow?\r\n\r\n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {\r\n        \/\/ Override point for customization after application launch.\r\n        \r\n        \/\/ <a href=\"http:\/\/- [ ] https:\/\/developer.apple.com\/library\/content\/documentation\/WindowsViews\/Conceptual\/WindowAndScreenGuide\/WindowScreenRolesinApp\/WindowScreenRolesinApp.html\" target=\"_blank\">Understanding Windows and Screens<\/a>In rare cases you might want to create your app\u2019s window programmatically https:\/\/developer.apple.com\/library\/content\/documentation\/WindowsViews\/Conceptual\/WindowAndScreenGuide\/WindowScreenRolesinApp\/WindowScreenRolesinApp.html#\/\/apple_ref\/doc\/uid\/TP40012555-CH4-SW1\r\n        window = UIWindow(frame: UIScreen.main.bounds)\r\n        let myViewController = ViewController()\r\n        window!.rootViewController = myViewController\r\n        window!.backgroundColor = UIColor.purple\r\n        window!.makeKeyAndVisible()\r\n        \r\n        setUpScreenConnectionNotificationHandlers()\r\n        checkForExistingSecondScreen()\r\n        \r\n        return true\r\n    }\r\n    \r\n    \/\/ Listing 2-1 Checking for the presence of an external display https:\/\/developer.apple.com\/library\/content\/documentation\/WindowsViews\/Conceptual\/WindowAndScreenGuide\/UsingExternalDisplay\/UsingExternalDisplay.html#\/\/apple_ref\/doc\/uid\/TP40012555-CH3-SW3\r\n    func checkForExistingSecondScreen () {\r\n\r\n        if (UIScreen.screens.count > 1) {\r\n            \/\/ Get the screen object that represents the external display.\r\n            let secondScreen = UIScreen.screens[1]\r\n            \/\/ Get the screen's bounds so that you can create a window of the correct size.\r\n            let secondScreenBounds = secondScreen.bounds\r\n            \r\n            secondWindow = UIWindow(frame: secondScreenBounds)\r\n            secondWindow!.screen = secondScreen\r\n            \/\/ Set up initial content to display...\r\n            let mySecondViewController = SecondViewController()\r\n            secondWindow!.rootViewController = mySecondViewController\r\n            secondWindow!.backgroundColor = UIColor.lightGray\r\n            \/\/ Show the window.\r\n            secondWindow!.isHidden = false\r\n        }\r\n    }\r\n\r\n    \/\/ Listing 2-2 Registering for screen connection and disconnection notifications https:\/\/developer.apple.com\/library\/content\/documentation\/WindowsViews\/Conceptual\/WindowAndScreenGuide\/UsingExternalDisplay\/UsingExternalDisplay.html#\/\/apple_ref\/doc\/uid\/TP40012555-CH3-SW6\r\n    func setUpScreenConnectionNotificationHandlers() {\r\n        let center = NotificationCenter.default\r\n        center.addObserver(self, selector: #selector(handleScreenDidConnect(notification:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)\r\n        center.addObserver(self, selector: #selector(handleScreenDidDisconnect(notification:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)\r\n    }\r\n    func handleScreenDidConnect(notification:NSNotification) {\r\n\r\n        let secondScreen = notification.object as! UIScreen\r\n        let secondScreenBounds = secondScreen.bounds\r\n        if (secondWindow == nil) {\r\n            secondWindow = UIWindow(frame: secondScreenBounds)\r\n            secondWindow!.screen = secondScreen\r\n            \/\/ Set up initial content to display...\r\n            let mySecondViewController = SecondViewController()\r\n            secondWindow!.rootViewController = mySecondViewController\r\n            secondWindow!.backgroundColor = UIColor.darkGray\r\n            \/\/ Show the window.\r\n            secondWindow!.isHidden = false\r\n        }\r\n    }\r\n    func handleScreenDidDisconnect(notification:NSNotification) {\r\n\r\n        if (secondWindow != nil) {\r\n            \/\/ Hide and then delete the window.\r\n            secondWindow?.isHidden = true\r\n            secondWindow = nil\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been meaning to look at this since 2013. Easy to get a second screen. Next, interacting with it. References: Understanding Windows and Screens UIWindow UIScreen Presenting Content on an External Display \/\/ ViewController.swift import UIKit class ViewController: UIViewController { override func loadView() { let overall = UIView(frame: UIScreen.main.bounds) self.view = overall } override func &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.codenkeg.com\/blog\/2017\/07\/03\/adding-airplay\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Adding AirPlay&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[6,23],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7VdP6-3y","_links":{"self":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts\/220"}],"collection":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":10,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}