I tried to play video embedded from youtube INLINE using WKWebView. However, I couldn't be able to fix that problem because my video still automatically go to full screen when I click to play it. I've spent several hours already, but I still couldn't find a solution yet. I'll appreciate your help, pls. Here is my code:
#IBOutlet weak var myPlayer: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let videoId = "9n1e1N0Sa9k"
if let mediaURL:URL = URL(string: "https://www.youtube.com/embed/\(videoId)?playsinline=1") {
let request:URLRequest = URLRequest(url: mediaURL)
myPlayer.load(request)
}
}
Here is a solution as you want, i make player programatically and change some code.
var myPlayer: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
webConfiguration.mediaTypesRequiringUserActionForPlayback = []
myPlayer = WKWebView(frame: CGRect(x: 0, y: 0, width: 375, height: 300), configuration: webConfiguration)
self.view.addSubview(myPlayer)
if let videoURL:URL = URL(string: "https://www.youtube.com/embed/9n1e1N0Sa9k?playsinline=1") {
let request:URLRequest = URLRequest(url: videoURL)
myPlayer.load(request)
}
}
Related
I have a memory leak on WebKit, and profiling through Xcode instruments gives a memory leak on the following.
WKHoverPlatter 1 0x281e7f7e0 96 Bytes WebKit -[WKContentView(WKInteraction) setUpInteraction]
Would anyone know how to fix this?
Here is my sample test code:
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
- (void)loadView;
This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.
You can put your code in viewDidLoad.
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
Please excuse my ignorance. I am very new to iOS development and Xcode. I have tried searching many pages on this site and haven't found any that directly solve my issue. Any help you can provide would be greatly appreciated!
I am unable to get my webview (WKWebview) to remain within the Safe Area boundaries (I think that's the best way to describe it). I have autogenerated constraints set on the webview and I can see within the Main.Storyboard editor that the webview is within the safe area.
Unfortunately the webview ignores these boundaries and encompasses the entire view and the text on my webpage appears at the top of the display behind the time, battery and connection icons.
What am I doing wrong?
Here is my code and a few pictures:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}
}
Your constraints are OK. The problem is with your overridden loadView() method. You don't need to create a new WKWebView since you are using storyboards and Interface Builder is creating it. Remove this method and move uiDelegate to viewDidLoad():
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self // Move delegate here
let url = Bundle.main.url(forResource: "bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}
}
Tried to make this work programmatically. Is it OK? Does anybody know how to change the color of top inset of safe area?
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
guard let url = URL(string: "https://apple.com/") else {
return
}
webView.load(URLRequest(url: url))
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// webView.frame = view.bounds // previously
let topPadding = view.safeAreaInsets.top
webView.frame = view.frame.inset(by: UIEdgeInsets(top: topPadding, left: CGFloat(0), bottom: CGFloat(0), right: CGFloat(0)))
}
it doesn't keep WKWebView inside safe area before and here what I've thought to add
view.insetsLayoutMarginsFromSafeArea = true
I added a WKWebView to my main View, but for some reason when I run the app it gives me a whitescreen. Do I need to set some additional size variables to the view or webView variable?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
view.frame = view.bounds
webView = WKWebView(frame: CGRect( x: 0, y: 20, width: 380, height: 150 ), configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
view.addSubview(webView)
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
loadView()
You should never call this method directly. The view controller calls
this method when its view property is requested but is currently nil.
This method loads or creates a view and assigns it to the view
property
You can try with viewDidLoad
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
view.frame = view.bounds
webView = WKWebView(frame: CGRect( x: 0, y: 20, width: UIScreen.main.bounds.width, height: 250 ), configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
view.addSubview(webView)
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
The first line in documentation of UIViewController.loadView says:
Creates the view that the controller manages
Which means, there is no mainView in loadView, here you have to assign/load a view for the UIViewController.
So, you have to replace this line:
view.addSubview(webView)
With this line:
self.view = webView
Anyways, here is your fixed view controller that is using loadView:
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .null, configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
self.view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com")!
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
}
}
Notice that I changed frame: .null, this too is managed by the ViewController.
Here is another class that shows you how to use addSubview instead of loadView to do the same
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView = WKWebView(frame: CGRect(x: 0, y: 20, width: 380, height: 150))
self.webView.navigationDelegate = self
self.webView.uiDelegate = self
self.view.addSubview(webView)
if let url = URL(string: "https://www.google.com") {
self.webView.load(URLRequest(url: url))
}
}
}
I have a log when I load youtube URL in WKWebView.
I have searched the same title of my question in stackOverFlow.
But it didn't work for me.
What's wrong with my code?
And I use swift4 & xcode9.2.
Thanks.
Transport also set true.
Warning like this:
Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service
import UIKit
import WebKit
class DetailViewController: UIViewController {
var videoId: String = ""
var videoTitle: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadUI() {
view.backgroundColor = UIColor.white
naviSetting()
webViewSetting()
}
func naviSetting() {
self.title = videoTitle
}
func webViewSetting() {
let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let url = URL(string: "https://www.youtube.com/watch?v=\(self.videoId)")
let request = URLRequest(url: url!)
webview.load(request)
self.view.addSubview(webview)
}
}
I have tried to encode parameter and Its working fine for me. Please check below code.
var wkWebView = WKWebView()
override func viewDidLoad()
{
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.wkWebView.translatesAutoresizingMaskIntoConstraints = false
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.wkWebView.frame.size.width, height: self.wkWebView.frame.size.height)
self.view.addSubview(wkWebView)
loadUrl()
}
override func viewWillLayoutSubviews()
{
super.viewWillLayoutSubviews()
self.wkWebView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.wkWebView.contentMode = .scaleAspectFit
}
func loadUrl()
{
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
let url_join = "v=1p38GWfCIhQ"
let myURL = URL(string: "https://www.youtube.com/watch?")
var myRequest = URLRequest(url: myURL!)
myRequest.httpMethod = "POST"
myRequest.httpBody = url_join.data(using: String.Encoding.utf8)
wkWebView.load(myRequest)
}
I hope It will also work for you. :)
For me this Code worked fine.
Notice, I Used http:// and not https://.
But you already have set the settings for http:// to work.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
private let url = URL(string: "http://www.google.com")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let request = URLRequest(url: url!)
webView = WKWebView(frame: self.view.frame)
self.view.addSubview(webView)
webView.navigationDelegate = self
webView.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am new to Swift 3.0 and I need some help...
I am trying to create a frame with should display a website. The website should not fill the entire view but just the frame. Every time I start the App, the website fills the entire screen and not just the frame... :(
This is my code:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet weak var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com/")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
What I want:
What I get:
Add constraints (anchor) for webview with equal padding.
Try this with storyboard layout:
Remove loadView() code from your file:
Or Try this programatically:
import UIKit
import WebKit
class WebKitController: UIViewController {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com/")
let myRequest = URLRequest(url: myURL!)
setupWKWebViewConstraints()
webView.load(myRequest)
}
// add constraints to your web view
func setupWKWebViewConstraints() {
let paddingConstant:CGFloat = 30.0
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: paddingConstant).isActive = true
webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -paddingConstant).isActive = true
webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: paddingConstant).isActive = true
webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -paddingConstant).isActive = true
}
}