How can we play YouTube Videos now with iOS?










2















I'm taking a course online that shows how to play videos from a url. But it's always the bunny one. I've been looking how to play YouTube videos, been finding that they use to use UIWebView. Since it's been deprecated, how would I be able to play them now. This is how my code looks like.



import UIKit
import AVKit
import AVFoundation

class CourseDetailVC: UIViewController {
@IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var authorButton: UIButton!
@IBOutlet weak var backgroundImage: UIImageView!

var course: Courses.Course?

@IBAction func playURLVideo()
guard let videoURL = URL(string: course?.videoUrl ?? "") else return
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true)
playerViewController.player!.play()




I just have a button. When I press it I would like to play a YouTube video. Now this works with the bunny one. How would I be able to play this YouTube video.



Screenshot after button is press



enter image description here



Updated Code



 @IBAction func playButtonPress() 
// Create Video player
var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/JePnQ1gSagc?playsinline=1?autoplay=1")
let youtubeRequest = URLRequest(url: myURL!)

mywkwebview?.load(youtubeRequest)

guard let webView = mywkwebview else return
self.view.addSubview(webView)











share|improve this question



















  • 1





    The documentation for UIWebView tells you what to use in its place.

    – rmaddy
    Nov 15 '18 at 6:10











  • use this github.com/malkouz/youtube-ios-player-helper-swift

    – Harshad Patel
    Nov 15 '18 at 8:10















2















I'm taking a course online that shows how to play videos from a url. But it's always the bunny one. I've been looking how to play YouTube videos, been finding that they use to use UIWebView. Since it's been deprecated, how would I be able to play them now. This is how my code looks like.



import UIKit
import AVKit
import AVFoundation

class CourseDetailVC: UIViewController {
@IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var authorButton: UIButton!
@IBOutlet weak var backgroundImage: UIImageView!

var course: Courses.Course?

@IBAction func playURLVideo()
guard let videoURL = URL(string: course?.videoUrl ?? "") else return
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true)
playerViewController.player!.play()




I just have a button. When I press it I would like to play a YouTube video. Now this works with the bunny one. How would I be able to play this YouTube video.



Screenshot after button is press



enter image description here



Updated Code



 @IBAction func playButtonPress() 
// Create Video player
var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/JePnQ1gSagc?playsinline=1?autoplay=1")
let youtubeRequest = URLRequest(url: myURL!)

mywkwebview?.load(youtubeRequest)

guard let webView = mywkwebview else return
self.view.addSubview(webView)











share|improve this question



















  • 1





    The documentation for UIWebView tells you what to use in its place.

    – rmaddy
    Nov 15 '18 at 6:10











  • use this github.com/malkouz/youtube-ios-player-helper-swift

    – Harshad Patel
    Nov 15 '18 at 8:10













2












2








2








I'm taking a course online that shows how to play videos from a url. But it's always the bunny one. I've been looking how to play YouTube videos, been finding that they use to use UIWebView. Since it's been deprecated, how would I be able to play them now. This is how my code looks like.



import UIKit
import AVKit
import AVFoundation

class CourseDetailVC: UIViewController {
@IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var authorButton: UIButton!
@IBOutlet weak var backgroundImage: UIImageView!

var course: Courses.Course?

@IBAction func playURLVideo()
guard let videoURL = URL(string: course?.videoUrl ?? "") else return
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true)
playerViewController.player!.play()




I just have a button. When I press it I would like to play a YouTube video. Now this works with the bunny one. How would I be able to play this YouTube video.



Screenshot after button is press



enter image description here



Updated Code



 @IBAction func playButtonPress() 
// Create Video player
var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/JePnQ1gSagc?playsinline=1?autoplay=1")
let youtubeRequest = URLRequest(url: myURL!)

mywkwebview?.load(youtubeRequest)

guard let webView = mywkwebview else return
self.view.addSubview(webView)











share|improve this question
















I'm taking a course online that shows how to play videos from a url. But it's always the bunny one. I've been looking how to play YouTube videos, been finding that they use to use UIWebView. Since it's been deprecated, how would I be able to play them now. This is how my code looks like.



import UIKit
import AVKit
import AVFoundation

class CourseDetailVC: UIViewController {
@IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var authorButton: UIButton!
@IBOutlet weak var backgroundImage: UIImageView!

var course: Courses.Course?

@IBAction func playURLVideo()
guard let videoURL = URL(string: course?.videoUrl ?? "") else return
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true)
playerViewController.player!.play()




I just have a button. When I press it I would like to play a YouTube video. Now this works with the bunny one. How would I be able to play this YouTube video.



Screenshot after button is press



enter image description here



Updated Code



 @IBAction func playButtonPress() 
// Create Video player
var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/JePnQ1gSagc?playsinline=1?autoplay=1")
let youtubeRequest = URLRequest(url: myURL!)

mywkwebview?.load(youtubeRequest)

guard let webView = mywkwebview else return
self.view.addSubview(webView)








ios swift avfoundation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:44







Luis F Ramirez

















asked Nov 15 '18 at 5:59









Luis F RamirezLuis F Ramirez

688




688







  • 1





    The documentation for UIWebView tells you what to use in its place.

    – rmaddy
    Nov 15 '18 at 6:10











  • use this github.com/malkouz/youtube-ios-player-helper-swift

    – Harshad Patel
    Nov 15 '18 at 8:10












  • 1





    The documentation for UIWebView tells you what to use in its place.

    – rmaddy
    Nov 15 '18 at 6:10











  • use this github.com/malkouz/youtube-ios-player-helper-swift

    – Harshad Patel
    Nov 15 '18 at 8:10







1




1





The documentation for UIWebView tells you what to use in its place.

– rmaddy
Nov 15 '18 at 6:10





The documentation for UIWebView tells you what to use in its place.

– rmaddy
Nov 15 '18 at 6:10













use this github.com/malkouz/youtube-ios-player-helper-swift

– Harshad Patel
Nov 15 '18 at 8:10





use this github.com/malkouz/youtube-ios-player-helper-swift

– Harshad Patel
Nov 15 '18 at 8:10












1 Answer
1






active

oldest

votes


















0














Here's a solution to WKWebView



Since YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.



The given video above has an ID of (https://www.youtube.com/watch?v=1roy4o4tqQM) -- [1roy4o4tqQM]



mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)


Updated code:



@IBAction func playURLVideo() 

var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)

mywkwebview.load(youtubeRequest)



You need to add ?autoplay=1 to the end of the embed URL






share|improve this answer

























  • How would you be able to play the video automatic?

    – Luis F Ramirez
    Nov 15 '18 at 15:06











  • What do you mean “automatic”?

    – excitedmicrobe
    Nov 15 '18 at 15:20












  • Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

    – Luis F Ramirez
    Nov 15 '18 at 15:51











  • check the updated code:

    – excitedmicrobe
    Nov 15 '18 at 15:58











  • check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

    – Luis F Ramirez
    Nov 15 '18 at 16:14












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313306%2fhow-can-we-play-youtube-videos-now-with-ios%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here's a solution to WKWebView



Since YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.



The given video above has an ID of (https://www.youtube.com/watch?v=1roy4o4tqQM) -- [1roy4o4tqQM]



mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)


Updated code:



@IBAction func playURLVideo() 

var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)

mywkwebview.load(youtubeRequest)



You need to add ?autoplay=1 to the end of the embed URL






share|improve this answer

























  • How would you be able to play the video automatic?

    – Luis F Ramirez
    Nov 15 '18 at 15:06











  • What do you mean “automatic”?

    – excitedmicrobe
    Nov 15 '18 at 15:20












  • Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

    – Luis F Ramirez
    Nov 15 '18 at 15:51











  • check the updated code:

    – excitedmicrobe
    Nov 15 '18 at 15:58











  • check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

    – Luis F Ramirez
    Nov 15 '18 at 16:14
















0














Here's a solution to WKWebView



Since YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.



The given video above has an ID of (https://www.youtube.com/watch?v=1roy4o4tqQM) -- [1roy4o4tqQM]



mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)


Updated code:



@IBAction func playURLVideo() 

var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)

mywkwebview.load(youtubeRequest)



You need to add ?autoplay=1 to the end of the embed URL






share|improve this answer

























  • How would you be able to play the video automatic?

    – Luis F Ramirez
    Nov 15 '18 at 15:06











  • What do you mean “automatic”?

    – excitedmicrobe
    Nov 15 '18 at 15:20












  • Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

    – Luis F Ramirez
    Nov 15 '18 at 15:51











  • check the updated code:

    – excitedmicrobe
    Nov 15 '18 at 15:58











  • check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

    – Luis F Ramirez
    Nov 15 '18 at 16:14














0












0








0







Here's a solution to WKWebView



Since YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.



The given video above has an ID of (https://www.youtube.com/watch?v=1roy4o4tqQM) -- [1roy4o4tqQM]



mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)


Updated code:



@IBAction func playURLVideo() 

var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)

mywkwebview.load(youtubeRequest)



You need to add ?autoplay=1 to the end of the embed URL






share|improve this answer















Here's a solution to WKWebView



Since YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.



The given video above has an ID of (https://www.youtube.com/watch?v=1roy4o4tqQM) -- [1roy4o4tqQM]



mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)


Updated code:



@IBAction func playURLVideo() 

var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()

mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)

let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)

mywkwebview.load(youtubeRequest)



You need to add ?autoplay=1 to the end of the embed URL







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:17

























answered Nov 15 '18 at 8:28









excitedmicrobeexcitedmicrobe

1,1131519




1,1131519












  • How would you be able to play the video automatic?

    – Luis F Ramirez
    Nov 15 '18 at 15:06











  • What do you mean “automatic”?

    – excitedmicrobe
    Nov 15 '18 at 15:20












  • Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

    – Luis F Ramirez
    Nov 15 '18 at 15:51











  • check the updated code:

    – excitedmicrobe
    Nov 15 '18 at 15:58











  • check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

    – Luis F Ramirez
    Nov 15 '18 at 16:14


















  • How would you be able to play the video automatic?

    – Luis F Ramirez
    Nov 15 '18 at 15:06











  • What do you mean “automatic”?

    – excitedmicrobe
    Nov 15 '18 at 15:20












  • Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

    – Luis F Ramirez
    Nov 15 '18 at 15:51











  • check the updated code:

    – excitedmicrobe
    Nov 15 '18 at 15:58











  • check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

    – Luis F Ramirez
    Nov 15 '18 at 16:14

















How would you be able to play the video automatic?

– Luis F Ramirez
Nov 15 '18 at 15:06





How would you be able to play the video automatic?

– Luis F Ramirez
Nov 15 '18 at 15:06













What do you mean “automatic”?

– excitedmicrobe
Nov 15 '18 at 15:20






What do you mean “automatic”?

– excitedmicrobe
Nov 15 '18 at 15:20














Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

– Luis F Ramirez
Nov 15 '18 at 15:51





Like the AVPlayerViewController when you allow player to play. ex: plaverViewController.player.play(). So when I click the button, the video plays in full screen.

– Luis F Ramirez
Nov 15 '18 at 15:51













check the updated code:

– excitedmicrobe
Nov 15 '18 at 15:58





check the updated code:

– excitedmicrobe
Nov 15 '18 at 15:58













check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

– Luis F Ramirez
Nov 15 '18 at 16:14






check the screenshot. It shows what happens in after I press the button. I'm trying to play the video when the button is click and in fullscreen. Would it be possible?

– Luis F Ramirez
Nov 15 '18 at 16:14




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313306%2fhow-can-we-play-youtube-videos-now-with-ios%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus