How to Make the Use of YouTube Player in Swift?


This article explains the use of Youtube Player in Swift. You will learn how iOS developers India make the use of Youtube player and see videos in iOS application.

One-liner: In this tutorial, we will learn about how to play YouTube video in the iOS app. This tutorial implemented in Xcode 8 and it built for iOS 10.

Start with creating one single view application with the product name “YoutubeVideoPlayer”. In the language field select swift language and devices field select universal.

In this tutorial, we use YouTube Helper Library created by Google and open source, which you can download from here. After that go to download folder and select YTPlayerview.h, YTPlayerView.m, the Assets file. Drag the selected file to our project and select “Copy items into destination group’s folder”. Check below screenshot.


First, go to File -> New -> File -> iOS -> header file and add to our project. After that import objective c file “YTPlayerview.h”. Now go to Main.storybord and add one view from object library to the main view.  Next, go to Identity Inspector and change the class name to YTPlayerView. Ctrl + drag from view to ViewController and make the connection of IBOutlet as “viewYouTube”. Next, add two buttons with the title “Stop” and “Play” respectively. Ctrl + drag from buttons to ViewController and make the connection of IBAction as btnStopClick and btnPlayClick respectively. Please check below screenshot for more info.

Now go to ViewController.swift class and in viewDidLoad() method pass one video id to YTPlayerView variable. Check this below screenshot.

Next is play and stop method. In btnPlayClick() call playVideo method for start player and btnStopClick() call stopVideo method for stop player. These things show in below screenshot.

Now build and run the project. If everything is, correct your app should run properly, and you will see below output. You can play video in full screen and half screen. Also, you can stop and play from default buttons or our custom buttons.

So, you can play and see youtube videos in the iOS application by using Youtube Player API in Swift. IOS developers India have explained the method to use Youtube Player API in Swift. If you want to ask anything, you can make comments below.

You might like

About the Author: Vijay Aegis

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.