I am having an issue with WKWebView I init like this
let configuration = WKWebViewConfiguration()
configuration.applicationNameForUserAgent = "Browser"
configuration.ignoresViewportScaleLimits = true
configuration.allowsInlineMediaPlayback = true
configuration.allowsAirPlayForMediaPlayback = true
configuration.allowsPictureInPictureMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypes.audio
configuration.selectionGranularity = WKSelectionGranularity.character
configuration.dataDetectorTypes = WKDataDetectorTypes.link
let webview = WKWebView(frame: .zero, configuration: configuration)
webview.allowsBackForwardNavigationGestures = true
webview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webview.uiDelegate = self
webview.navigationDelegate = self
let url = URL(string: "https://www.google.com")!
let request = URLRequest(url: url)
webview.load(request)
self.view = webview
everytime I enter a search box on a website for example "Google" I get this log in the debugger... I can't seem to understand why it is throwing constraint issues while I did not set any... and this view is contained in UINavigationController that has a UIToolbar
Screenshot of Storyboard.
Related
Problem:
I would like to implement a similar solution that WKWebView has but with SFSafariViewController.
allowsInlineMediaPlayback = false
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = false
How can I implement a similar functionality with Safari Services?
import SafariServices
let bookmark = NSURL(string: youtubeVideoURL)
let config = SFSafariViewController.Configuration()
let safari = SFSafariViewController(url: bookmark as! URL)
if(UIDevice.current.userInterfaceIdiom == .pad) {
safari.modalPresentationStyle = .fullScreen
}else{
safari.modalPresentationStyle = .fullScreen
}
self.present(safari, animated: true, completion: nil)
Exactly what I want accomplished is in the code below. However, I am wondering if it can be done with a SFSafariViewController by somehow implementing
allowsInlineMediaPlayback
let youtubeVideoURL = posts[sender.view?.tag ?? 0].youtube_video_url + "?playsinline=1?autoplay=1"
var myPlayer: WKWebView!
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = false
webConfiguration.mediaTypesRequiringUserActionForPlayback = []
myPlayer = WKWebView(frame: UIScreen.main.bounds, configuration: webConfiguration)
self.view.addSubview(myPlayer)
if let mediaURL:URL = URL(string: youtubeVideoURL) {
let request:URLRequest = URLRequest(url: mediaURL);
myPlayer.load(request)
}
Pseudocode:
let config = SFSafariViewController.Configuration()
config.allowsInlineMediaPlayback = false
I am trying to load a html which has 2 frames in it using WKWebView. I am getting all the navigation delegates for main frame once but not getting didCommit and didFinish callback for subframes.
My sample html is like this:
https://example.com <html><head><base href="https://example.com"></head><frameset framespacing="0" rows="*,0" frameborder="0" noresize><frame name="sco" src="https://example.com//assets/s/uruq3acajh9u/x/mobile/path/loading.html"><frame name="adaptor" src="https://example.com//assets/s/uruq3acajh9u/content/mobile/path/1.html?sco_url=https://example.com/content/elearningngx/VHDRczdEy5kO_pRfKsNJEA/1571433806/0088Z2lPcUR4MVljWWlPUVYvSjNrby9OVjJzbk84OENkN1h3SVl3UVBPMG9sb3lXNFFsaHUwT2xDbVhFTzVpMWdiNQ==RnJpIE9jdCAxOCAxMzoyMzoyNiBVVEMgMjAxOQ==/eot/index.html"></frameset></html>
func setupWebView(){
if (self.webView != nil){
self.webView.removeFromSuperview()
}
HTTPCookieStorage.shared.cookieAcceptPolicy = .always
let userContentController = WKUserContentController()
userContentController.addUserScript(HTMLViewScripts.viewportScript)
let pref = WKPreferences()
pref.javaScriptEnabled = true
pref.javaScriptCanOpenWindowsAutomatically = true
pref.setValue(true, forKey: "allowFileAccessFromFileURLs")
pref.setValue(true, forKey: "_allowUniversalAccessFromFileURLs")
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.preferences = pref
wkWebConfig.userContentController = userContentController
let wkWebSiteStore = WKWebsiteDataStore.nonPersistent()
wkWebConfig.websiteDataStore = wkWebSiteStore
self.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: Constants.screenSize.width, height: self.playerContainer.frame.size.height), configuration: wkWebConfig)
self.webView.navigationDelegate = self
self.webView.uiDelegate = self
self.playerContainer.addSubview(self.webView)
}
Also same issue with evaluateJavaScript as well. I am not getting any callback when loading "about:blank" using evaluateJavaScript. Seems same frames related issue.
Any leads would be appriciated. Thanks in advance.
let url = NSURL (string: "https://i.stack.imgur.com/0LSmY.jpg");
let webViewInst: UIWebView = UIWebView()
let requestObj = NSURLRequest(url: url! as URL)
webViewInst.frame = self.view.bounds
webViewInst.loadRequest(requestObj as URLRequest);
webViewInst.delegate = self
webViewInst.scalesPageToFit = true
webViewInst.contentMode = .scaleAspectFit
self.view.addsubview(webViewInst)
This is working, but we need to scroll the webview to see full image in the web page. How to fix this?
How to show the full image fit to the view?
P.S, the image that mentioned in the above url is more than 3000 pixel in diomensions(3024x4032)
Try this one I have implemented it and working as you expected.
webView = WKWebView(frame: self.view.frame)
let urlString = "https://i.stack.imgur.com/0LSmY.jpg"
let url = URL(string: urlString)!
let request = URLRequest(url: url)
webView.load(request)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
Rather than loading a WebView from a URL like this
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: imageUrl.urlString)
let request = URLRequest(url: url!)
webView.load(request)
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
How can I load a UIImage onto the WebView in Swift 4?
Try Something like this.
let html = "<html><img src=\"datadb/DestinationGuide/Images/YourImage.png\"/> </html>"
//Save this html in `DocumentDirectory`
let saveHTML = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("temp.html")
try? html.data(using: .utf8)!.write(to: saveHTML)
//Now load this `temp.html` file in webView
webView.loadRequest(URLRequest(url: saveHTML))
I am using MarkDonwView Library : https://github.com/keitaoouchi/MarkdownView
Which is converting MD String to show as a webview. Rendering is taking too long as it loads everything and then shows the view. Is it possible to make image loading async while showing text body ASAP so it will not look like its too slow ?
This is how it is loading the html file.
if bundle.bundleIdentifier?.hasPrefix("org.cocoapods") == true {
htmlURL = bundle.url(forResource: "index",
withExtension: "html",
subdirectory: "MarkdownView.bundle")
} else {
htmlURL = bundle.url(forResource: "index",
withExtension: "html")
}
if let url = htmlURL {
let templateRequest = URLRequest.init(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 60) //URLRequest(url: url)
let escapedMarkdown = self.escape(markdown: markdown) ?? ""
let imageOption = enableImage ? "true" : "false"
let script = "window.showMarkdown('\(escapedMarkdown)', \(imageOption));"
let userScript = WKUserScript(source: script, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let controller = WKUserContentController()
controller.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
let wv = WKWebView(frame: self.bounds, configuration: configuration)
wv.scrollView.isScrollEnabled = self.isScrollEnabled
wv.translatesAutoresizingMaskIntoConstraints = false
wv.navigationDelegate = self
addSubview(wv)
wv.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
wv.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
wv.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
wv.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
wv.backgroundColor = self.backgroundColor
self.webView = wv
wv.load(templateRequest)
} else {
// TODO: raise error
}