How to add Youtube videos in your application

In this tutorial i’m gonna show you how to add Youtube videos in your application.

Screen Shot 2016-04-22 at 10.02.35 AM

Not a big deal.So let’s start.

Start with a New Xcode Project -> Single view application -> Name it as whatever you want.Language as Swift and Devices Universal and doesn’t matter weather you select the core data or not. -> Save it as always

Screen Shot 2016-04-22 at 9.34.59 AM

We can just use the initial View Controllers for the tutorial and just drag and drop a Web View from the Utility panel at the right bottom of the window.

Then press control + drag you pointer with three fingers to the ViewContrller through the Assistant Editor.Hope you can figure it out easily.

Screen Shot 2016-04-22 at 10.02.12 AM

You may need to get the embed youtube code for this tutorial so just go to the video you gonna select -> Share -> Embed .Now you can copy this code and use wherever you want.

Screen Shot 2016-04-22 at 9.40.26 AM

Here you can find the code that i used to make this app with comments.Hope that is clear enough.Otherwise let me know. 😀

</pre>
//

//  ViewController.swift

//  YoutubeApp

//

//  Created by Akila j wasala on 4/22/16.

//  Copyright © 2016 Akilajwasala. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

//This is the Web View linked with the ViewController

@IBOutlet weak var videoView: UIWebView!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

//Use this string to keep the Youtube urls

let youtubeURL = "https://www.youtube.com/embed/hX3j_qAbJgU"

//When you play the video it takes the full screen by default.Used this inline method to avoid that and just play inside of the selected Web View

videoView.allowsInlineMediaPlayback = true

videoView.loadHTMLString("<iframe width = \"\(videoView.frame.width)\" height=\"\(videoView.frame.height)\" src=\(youtubeURL)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}
<pre>

Hope you learnt something.Leave a comment if you get into any troubles.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s