0

I am new in swift and I am getting problem in removing sign in button from youtube video

my code is like this

var videoUrl = vedioarr1[indexPath.row] as? String
videoUrl = (videoUrl ?? "")
print(videoUrl)

let youtubeUrl = URL(string: videoUrl ?? "")!
let youtubeRequest = URLRequest(url: youtubeUrl ?? URL(string: "")!)                    
cell.youtubeWebView.load(youtubeRequest)

VedioUrl contains url YoutubeWebView is WKWebView

enter image description here

I Just want to remove sign in button

https://www.youtube.com/watch?v=7cKdlhaQrGo
Mujtaba
  • 97
  • 1
  • 7
  • there is **no key** to remove the signinbutton. see this for example : [YouTube Embedded Players and Player Parameters](https://developers.google.com/youtube/player_parameters) – Anbu.Karthik Jan 02 '20 at 07:24

2 Answers2

2

If you just want to embed a youtube video, Youtube has an embed URL for it. You just need to get the videoID and create your URL.

https://www.youtube.com/watch?v=7cKdlhaQrGo this is the URL you are currently using. The ID after watch?v= is the videoID. In your case, the videoID is 7cKdlhaQrGo.

Now you can create your URL like this:

let myURL = "https://www.youtube.com/watch?v=7cKdlhaQrGo"

let youtubeEmbedURL = myURL.replacingOccurrences(of: "https://www.youtube.com/watch?v=", with: "https://www.youtube.com/embed/") 

let youtubeRequest = URLRequest(url: URL(string: youtubeEmbedURL)!)  

cell.youtubeWebView.load(youtubeRequest)

This will remove the signing button and make sure only video shows up in whole screen.

Update2:

If you want to disable controls, you can add this string to your url,

let youtubeEmbedURL = myURL.replacingOccurrences(of: "https://www.youtube.com/watch?v=", with: "https://www.youtube.com/embed/") + "?rel=0?version=3&autoplay=1&controls=0&&showinfo=0&loop=1" // this string will disable controls.

But you can not disable title.

YouTube deprecated parameter showinfo (can no more hide title) and changed parameter rel behavior (will show related videos).

Details in https://developers.google.com/youtube/player_parameters#release_notes_08_23_2018

Note: This is a deprecation announcement for the showinfo parameter. In addition, the behavior for the rel parameter is changing. Titles, channel information, and related videos are an important part of YouTube’s core user experience, and these changes help to make the YouTube viewing experience consistent across different platforms.

Actual answer here : https://stackoverflow.com/a/52767228/8374890

Keshu R.
  • 5,045
  • 1
  • 18
  • 38
2

Instead of loading a request, load HTML string as:

let ytEmbedString = "<iframe id='ytplayer' type='text/html' width='640' height='360' src='https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com' frameborder='0'></iframe>"

cell.youtubeWebView.loadHTMLString(ytEmbedString, baseURL: nil)

Check YouTube docs for more details on embedding.

Orion Cygnus
  • 168
  • 1
  • 8