how can playing video within webView in swift - webview

i have an issue for playing video from youtube within webView i try to playing video from youtube not in full screen in webView but i couldn't and i set the the property allowsInlineMediaPlayback for webView in viewDidLoad() but it's not working
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://www.youtube.com")
let YOUTUBE = URLRequest(url:url!)
webView.loadRequest(YOUTUBEURL)
webView.allowsInlineMediaPlayback = true
}
so please can help me for how can i playing video youtube not in full screen in webView
thanks A lot

you should use UIWebView instead of WKWebView.
here is the needed code for you:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
myWebView.allowsInlineMediaPlayback = true
myWebView.loadHTMLString("<iframe width=\"\(myWebView.frame.width)\" height=\"\(myWebView.frame.height)\" src=\"https://www.youtube.com/embed/FDT343j3zD0?playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)
}
}

Related

Put a video into a UIView with push , start controls in ios swift

I want to put video in to the simple view and I want to push, start that video. I code for that. My video is playing into the view. But problem is I cannot push or start that video and when I go from one viewcontroller to another viewcontroller video start in background. Video also repeat all time not stop. my code for this is here:
import UIKit
import AVKit
import AVFoundation
class ComingSoonViewController: UIViewController {
#IBOutlet weak var videoview: UIView!
#IBOutlet weak var nextBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
let videoURL = URL(string: "http://conceptioni.in/DonorPlus/public/assets/1168255646_new.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoview.bounds
self.videoview.layer.addSublayer(playerLayer)
player.play()
}
}
please Help.Thank you in advance.

WKWebView loaded twice, trying to go back I have to press twice on back navigation button

I'm trying to show a website using the WKWebView, it works fine as for the internet work.
problem is it is loading the site twice (it seems like it's going to another ViewController).
here is my code:
#IBOutlet weak var myWebView: WKWebView!
override func loadView() {
myWebView = WKWebView()
myWebView.navigationDelegate = self as? WKNavigationDelegate
view = myWebView
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = false
let url = URL(string: "https://google.com")!
myWebView.load(URLRequest(url: url))
}

How do I correctly disable link detection in UIWebView?

I Have a UIWebView strictly for viewing PDF's, and I don't want any link detection in the view, Like this:
So I added the following code to negate the detection of links and the presentation of that view:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
However it still picks up links, and presents that modal view. How do I correctly implement code to stop this?
Here is my updated code:
class PdfViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var myWebView: UIWebView!
var contentUrlPassedOn: String!
var allowLoad = true
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
let url: NSURL! = NSURL(string: contentUrlPassedOn)
myWebView.loadRequest(NSURLRequest(URL: url))
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return allowLoad
}
func webViewDidStartLoad(webView: UIWebView) {
var hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
self!.allowLoad = false
}
}
}
If your webview was added by the storyboard, You can disable this in the attributes inspector.
[1
It's an objective-c code, but u can do the same in Swift. It will fix the interaction with links. The con is that they will be still detected.
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
return NO;
}
return YES;
}
Finely Solve the problem for you,
Working Demo
Cannot disable long press link menu, Which is Bug in iOS, That's solve in iOS 8.2.
According to bug, when press long on link then its open Action Sheet, Now in iOS 8.2 its solve as below.
Load PDF using WKWebView as below.
import UIKit
import WebKit
class ViewController: UIViewController , WKUIDelegate {
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
var url = NSURL(string:"http://www.nursing.umich.edu/studentresources/documentation/CreatingBookmarksPDFdocsV7.pdf")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
}
Now you Click on your long Press link then its doesn't open ActionSheet PopUp.
Enjoy..!!
Output :

How to: watchkit button displays image at url in Swift?

I want a button press in Watchkit to display an image from a URL. Given there's no UIWebView in Watchkit, my assumption is that I will need to trigger via the watch button, the loading of a UIWebView for the URL on the phone, store in an UIImage, that I can pass back to the watch to display. First is that the best strategy, secondly can anyone help me with the code for storing a UIImage from UIWebView in Swift?
Phone's ViewController:
import UIKit
class ViewController: UIViewController {
var URLPath = "myURL.jpeg"
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL()
}
//to fit webpage within webView
override func viewDidAppear(animated: Bool) {
webView.frame = UIScreen.mainScreen().bounds
webView.center = self.view.center
webView.scalesPageToFit = true
}
func loadAddressURL () {
let url = NSURL(string: URLPath)
let requestObj = NSURLRequest(URL: url!)
webView.loadRequest(requestObj)
}
}

UIWebView auto size in landscape (Swift)

i'm new to the Swift programming language!
I'm building a simple app that shows a webpage, my problems is, when i rotate the device, the WebView isn't sized to fill the Scene...
I've tried to use this code:
func resizeWebView(){
webView.scalesPageToFit = true
webView.contentMode = UIViewContentMode.ScaleToFill
webView.multipleTouchEnabled = true
webView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
self.view.addSubview(webView)
}
i call this function on the viewDidLoad() function, is that right?
Are there anyway to do this? Everything that i found on the internet is for Objective-C...
Thanks :)
I just did a simple example:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var containerView : UIView?
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string:"http://chart.finance.yahoo.com/z?s=AAPL&t=6m&q=l&l=on&z=s&p=m50,m200")
var req = NSURLRequest(URL:url)
self.webView!.loadRequest(req)
}
}
I did not use auto layout or deal with status bar issues, but no problem resizing on rotation.

Resources