This post is part of a series where we take a first look at tvOS. Check our first post to see how TVMLKit helps developers quickly (and easily) create media apps that behave just like those that ship with the Apple TV.
There are multiple frameworks that were not carried over from iOS 9.1 to tvOS 9.0, but one in particular seemed a bit odd to me. Why no Photos framework ლ(ಠ益ಠლ)?
The Apple TV seems like the perfect platform for users to enjoy their photos and videos. With tvOS 9.2 came iCloud photo library support in the photos app, but still no tvOS Photos framework for developers. So what can be done if you want access to a users photos in your Apple TV app? The Apple forums suggests filing a feature request (bugreporter.apple.com) in hopes of it being added in a future release. Or maybe you could make an app that depends on Dropbox or Amazon cloud drive, but I am hesitant to depend on another 3rd party API for one of my apps (here’s looking at you Netflix). One possible solution I decided to test was leveraging the Photos framework on an iOS device to stream photos and videos to the Apple TV using a simple http server. The result: a decent solution for getting photos and videos to the big screen.
The first implementation step was to create an iOS app to act as the server for photos and videos. CocoaHTTPServer is the server I went with for serving the photos and videos up to the TV. The one tricky piece is making sure the assets retrieved by the Photos framework can be saved and passed to the Apple TV app. The solution I came up with was to use PHImageManager to retrieve the asset data and saved the data to a temp file that would be streamed. In the case of images, it was simple to save the data directly from the PHImageManager request to file, but for video there was an additional step. For video the AVAsset must be retrieved from PHImageManager and saved to the temporary file using AVAssetExportSession. Once the data is being saved, I was able to setup the CocoaHTTPServer connections to handle requests for image and video files.
From the Apple TV perspective, the app is simply requesting image and video the same way it would in any other client-server architecture. Since this is expected to be done on a local network, Bonjour can be used to detect the iOS app acting as the server. Once we have the endpoint for our server, requests can be made to retrieve photos and videos from Photos framework!
Performance was reasonable in my tests of this solution. The one place that took the most time was around saving video data to file on the server side. This is even more noticeable when the PHAsset is not saved to the device (and is instead living in the users iCloud photo library). In that case, it would probably be best to simply pass the video URL to the Apple TV to stream directly. This is admittedly a hacky solution, but until Apple adds Photos framework to tvOS it looks to be the best option. Additional considerations around authorization would be required for a production ready implementation, but this test serves as a proof of concept for streaming users photos and videos to the big screen.
Alexander Pal, has a degree in Electronics and Computer Science, and surprisingly Quilting. With 15 years of experience in the field of IT, ranging from database systems, business intelligence, CRM/ERP, mobile apps and E-commerce sites, Alex has hands-on experience clearly with many subjects. He's currently working as a systems architect and tech lead on the Roku platform. When not solving complex software problems, Alex spends most of his time playing hide and seek with his dog Nina as well as trying to unravel the mysteries of the universe. He's a big fan of discovering new music, traveling and appreciating art.