Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to embed youtube or facebook live streaming in my ios App ?

I have been looking over facebook SDK for IOS but I could not really get a solid answer . So I am asking here , weather is it possible to stream the live facebook video or youtube video in my IOS app using swift ? if yes , then feel free to give me some tips or share good reads and references Tnx

like image 363
Danial Kosarifa Avatar asked Oct 19 '25 04:10

Danial Kosarifa


1 Answers

To embed youtube live stream onto your iOS app use the following:

   @IBOutlet weak var LiveStreamViewer: UIWebView!
   let channelID = "[channelID]"

   func loadLiveStream {
        guard
            let youtubeURL = URL(string: "https://www.youtube.com/embed?
                live=1&channel=\(channelID)")
            else { return }
        LiveStreamViewer.loadRequest( URLRequest(url: youtubeURL) )
   }

However, using this will bring up an error within the WebView if you are not actually streaming, and someone tries to play the video. To plan for that add something similar to the following function:

   let googleAPI = "[youtubeAPIKey]"


   func checkLive() {
       let liveChannel = URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=\(channelID)&type=video&eventType=live&key=\(googleAPI)")
       URLSession.shared.dataTask(with:liveChannel!, completionHandler: {(data, response, error) in
           guard let data = data, error == nil else { return }

           do {
               let parsedData = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
               let pageInfo = parsedData["pageInfo"] as! [String:Any]

               print(pageInfo)

               let currentState = pageInfo["totalResults"] as! Int
               print(currentState)

               if currentState == 0 {
                   print("No Stream Right Now")
                   self.LiveStreamViewer.isHidden = true
                   //DO SOMETHING TO LET THEM KNOW YOU ARENT STREAMING
               } else {
                   print("Streaming")
                   self.LiveStreamViewer.isHidden = false
                   self.loadLiveStream()
               }

           } catch let error as NSError {
               print(error)
           }
       }).resume()
   }
like image 120
Humble Avatar answered Oct 21 '25 17:10

Humble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!