Google Cast in iOS(Swift) Sender App

Jay Savsani
Mac O’Clock
Published in
4 min readFeb 29, 2020

--

iOS Google Cast sender App using SDK by Google (Tested on iOS 13 & 12) developed in Swift 5.x

This article is for developers who are looking for integrating Google Cast into their exiting application or want to learn how Google Cast works. Integrating Google Cast seems very straight forward from their documentation but still, you’ll find many headaches while integrating it into your existing application like how to show mini media controller and how to add a controller to your TabView kind of application.

This is going to be a series of 3 articles, this article will walk you through basic integration of Google Cast SDK into iOS SDK. In the second article I’ll walk you through how to show media controller and in the last article we’ll learn about how to customise view controller according to your app theme.

Create New Xcode Project & Install Pod

Now you need to create a new Xcode project and use pod init to generate pod file. Now add we’re going to use no Bluetooth Google Cast SDK for iOS13. By adding no Bluetooth we’re just leaving Guest mode out of our application. If you want more details on Guest Mode you can find out in this article.

Now go with

pod install

and you’ll have Google Cast SDK inside your project. Open Google Cast Demo.xcworkspace. Now open your AppleDelegate.swift and add following lines inside didFinishLaunchingWithOptions. This will start discovering available Casting devices inside your network with options that you’re providing. kReceiverAppID, is the default id for receiver app provided by the framework. You can also create Cast Receiver app for customising receiver app’s styling (like Netflix, Prime Videos).

let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID)
let options = GCKCastOptions(discoveryCriteria: criteria)
GCKCastContext.setSharedInstanceWith(options)
GCKLogger.sharedInstance().delegate = self

Cast Button inside Navigation Bar

Now we’re going to add this cast button to our navigation bar. You can add this cast button as per your convenience, inside any other view apart from navigation view. As you can see in documentation GCKUICastButton is an extension of UIButton only.

let castButton = GCKUICastButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
castButton.tintColor = UIColor.gray
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: castButton)

Now you can see Cast button inside the navigation bar. This cast button will only appear if there’s any Chrome cast available in your network. It’ll be shown/hidden automatically based on the availability of Chrome cast inside your network.

Now click that Cast button and Voila!! It’ll show you Chrome cast which is available in your network. In my case, it’s Bedroom TV. Now select available Chrome cast and Cast icon will be turned into a connected one.

Cast Video on Chrome Cast

Now it’s time to cast video on your Chrome cast. For Video and it’s information I’m using google’s sample file which they’ve used in their demo. You can find it’s location inside project, labeled as sample.json. I’m displaying this video’s inside TableView Controller and have used SwiftyJSON for parsing JSON.

Preparing Request For Chrome Cast

Now the first thing will be providing proper URL for casting to Chrome cast.

let url = URL.init(string: "URL_OF_YOUR_VIDEO")
guard let mediaURL = url else {
print("invalid mediaURL")
return
}

Providing Metadata to Chrome Cast

You can provide metadata to Chrome Cast via like title, thumbnail and description. It’s completely optional what metadata you’d like to provide for casting. You can find a complete list of metadata that cast SDK provides.

let metadata = GCKMediaMetadata()
metadata.setString(movieModel!.desc, forKey: kGCKMetadataKeyTitle)
metadata.setString(movieModel!.desc, forKey: kGCKMetadataKeySubtitle)metadata.addImage(GCKImage(url: URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg")!, width: 480, height: 360))

Creating Request

Now that we’ve URL and Metadata, it’s time to cast video. For that, we’ll use GCKMediaInformationBuilder & GCKMediaInformation and provide URL and metadata to that class.

let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: mediaURL)
mediaInfoBuilder.streamType = GCKMediaStreamType.none;
mediaInfoBuilder.contentType = "video/mp4"
mediaInfoBuilder.metadata = metadata; \\(providing metadata)
let mediaInformation = mediaInfoBuilder.build() // Now finally handing over all information and load video
if let request = sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInformation){
request.delegate = self \\extend GCKRequestDelegate for this
}

Inside of Class declare sessionManager variable and use ViewDidLoad to get a session to which we’re connected in the previous screen.

var sessionManager: GCKSessionManager!
override ViewDidLoad(){
sessionManager = GCKCastContext.sharedInstance().sessionManager
}

Now your complete code block should look like something below.

Click on docPlay method and Done.

You can see Thumbnail, Title and Description that we’ve provided in mediaInformation.

Now that you’ve played the video to your Casting device and you made Google Cast sender App for iOS, To keep things simple, I’m concluding this article here. Download this sample code from github link.

But Wait!!!

what about media controls? How to pause/stop this video, how to add videos in the queue, where’s volume controls?

I’ve made another article where I’m showing you how to add media controls and add videos in the queue.

--

--