Asmin Ghale
Asmin Ghale

Reputation: 195

Google Cast new session not starting swift ios

I need a custom view for displaying available devices and connecting. I used GCKDiscoveryManager. which returns a GCKDevice. Now, when I try to start a new session with the device, it fails every time. Here is my code:

let sessionManager = GCKCastContext.sharedInstance().sessionManager
 var devices = [GCKDevice](){
    didSet{
        discoveryTableView.reloadData()
    }
 }

override func viewDidLoad() {
    super.viewDidLoad()

    GCKCastContext.sharedInstance().discoveryManager.add(self)
    GCKCastContext.sharedInstance().discoveryManager.startDiscovery()

    sessionManager.add(self)

}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let device = devices[indexPath.row]

    print(sessionManager.startSession(with: device))

}

Only the device discovery is working right now.

extension DevicesViewController: GCKDiscoveryManagerListener, GCKSessionManagerListener{
func didUpdateDeviceList() {
    print("did update here")
}

func didInsert(_ device: GCKDevice, at index: UInt) {
    self.devices.append(device)
}

func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKSession) {
    sessionManager.currentCastSession!.add(textChannel)
    print("session started")
}

func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKCastSession) {
    print("cast session started")
}

func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKSession, withError error: Error) {
    print(error.localizedDescription)
}

func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKCastSession, withError error: Error) {
    print(error.localizedDescription)
}


}

I have already implemented required codes in AppDelegate.swift as per the documentation.

Upvotes: 4

Views: 1245

Answers (1)

oxigen
oxigen

Reputation: 6263

Chromecast SDK is written on Objective-C So you need to add @objc to your listener methods

@objc func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKSession) {
    sessionManager.currentCastSession!.add(textChannel)
    print("session started")
}

@objc func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKCastSession) {
    print("cast session started")
}

@objc func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKSession, withError error: Error) {
    print(error.localizedDescription)
}

@objc func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKCastSession, withError error: Error) {
    print(error.localizedDescription)
}

Upvotes: 2

Related Questions