Create a WKWebView in a UIView - ios

I'm trying to add a WKWebView inside of a UIView ,
The code seems to work fine ( the page is loaded the bounds is good the delegate print the message with no error)
but the View (called ViewForStuffInWeb ) stay blank, nothing in it
can someone explain why ?
import UIKit
import WebKit
class ViewController: UIViewController ,WKNavigationDelegate {
#IBOutlet var ViewForStuffInWeb: UIView!
var webView = WKWebView()
let request = "https://www.google.fr"
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.automaticallyAdjustsScrollViewInsets = false
let config = WKWebViewConfiguration()
webView = WKWebView(frame: ViewForStuffInWeb.bounds ,configuration : config)
webView.navigationDelegate = self
ViewForStuffInWeb = webView
println("webview : frame :\(webView.frame) , bounds : \(webView.bounds)")
requesting(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func requesting (request :String) {
if let url = NSURL(string: request) {
webView.loadRequest(NSURLRequest(URL: url))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
println("decide policy action")
decisionHandler(WKNavigationActionPolicy.Allow)
}
func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {
println("decide policy response")
decisionHandler(WKNavigationResponsePolicy.Allow)
}
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
println("commit navigation")
}
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {
println("fail in didFailNavigation \(error)")
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
println("fail in didFailProvisionalNavigation \(error)")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
println("finish navigation")
}
thanks for reading

You never add the WKWebView to the view hierarchy. In your viewDidAppear, which code should really move to viewDidLoad, you probably want to replace:
ViewForStuffInWeb = webView
with:
ViewForStuffInWeb.addSubview(webView)
Although it is unclear what ViewForStuffInWeb actually is. The above will only work if that view exists in your nib and if it is connected.

Related

Webview Back & Forward Button Not working (Swift, Xcode 14)

My webView Back & Forward buttons used to work but they are not anymore. I've looked everywhere I could for a solution but only found similar ones including here but nothing works for me or for Swift (at least to me). Below are my codes.I have Webkit Framework added, still not working when the buttons did work before even without adding the Framework. App Transport Security setting is set to YES for Arbitrary Load as before when the buttons were working. Did Apple change something? Please help. Thanks!!!
import UIKit
import WebKit
class SocialPlatformController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var BackBtn: UIButton!
#IBOutlet weak var ForwardBtn: UIButton!
#IBOutlet weak var webView: WKWebView!
#IBAction func backToMainScreen(_ sender: Any) {
}
#IBAction func goBackBtnTap(_ sender: Any) {
if webView.canGoBack {
webView.goBack()
}
}
#IBAction func goForwardBtnTap(_ sender: Any) {
if webView.canGoForward {
webView.goForward()
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Swift.Void) {
BackBtn.isEnabled = webView.canGoBack
ForwardBtn.isEnabled = webView.canGoForward
guard
let response = navigationResponse.response as? HTTPURLResponse,
let url = navigationResponse.response.url
else {
decisionHandler(.cancel)
return
}
if let headerFields = response.allHeaderFields as? [String: String] {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)
cookies.forEach { (cookie) in
HTTPCookieStorage.shared.setCookie(cookie)
}
}
decisionHandler(.allow)
}
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
let url:URL = URL(string:"https://www.facebook.com/groups/246328283932460")!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}
Perhaps it's better to handle the Back and Forward button state in a navigation delegate handler that's fired after the navigation has been finished, for example:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
//successful load
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
updateButtons()
}
//failure load
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
updateButtons()
}
//back-forward button update
func updateButtons() {
backButton.isEnabled = webView.canGoBack
forwardbutton.isEnabled = webView.canGoForward
}
You can follow these delegates and methods. I think buttons are not getting updated/loaded as they should.

switch view if the Webview can't loading the URL?

I have 2 views on my screen first is the Webview and the second is a static view "errorView" with the error message "set to hidden". I am trying to load the errorView if we have an error loading website to Webview.
#objc private func loadURL() {
guard let url = URL(string: "https://www.hackingwi") else {
self.errroLoadingWebView.isHidden = false
return
}
webView?.load(URLRequest(url: url))
webView?.allowsBackForwardNavigationGestures = true
}
You can use WKNavigationDelegate.
set delegate
webView.uiDelegate = self
webView.navigationDelegate = self
class StaticWebPageVC: WKNavigationDelegate, WKUIDelegate {
func webView(_: WKWebView, didFinish _: WKNavigation!) {
// Web view loaded
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// Show error view
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
// Show error view
}
}

URL loading issue in WKWebView

I'm getting a URL from API, I'm passing that URL to my webview and trying to load that, but it isn't loading. I don't know why but it isn't working. Even it's not showing any error or crashes. It's so weird but I don't know what I'm missing in that all the code is written fine. This is my code:
self.loadWeb(webURL: newsUrl!)
func loadWeb(webURL: String) {
let url = URL(string: webURL)
MBProgressHUD.hide(for: self.view, animated: true)
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(URLRequest(url: url!))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame.size.height = 1
webView.scrollView.isScrollEnabled = false
webView.frame.size = webView.scrollView.contentSize
self.bgViewHeight.constant = webView.frame.size.height
}
private func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Start to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}
How i can load this?

WKWebView goBack requiring two invocations

I'm working on a iOS app that uses WKWebView, with custom navigation buttons. We're finding that following some links will require using our Back button twice, as the first invocation seems to just reload the current page. This does not happen in mobile Safari.
What could cause goBack() to need to be called twice to actually navigate back one page, while it works correctly in Safari? Are there changes I can make in the app to correct the issue?
Note: I cannot make changes to the web site's content or structure, so any fixes would have to be in the app.
Update:
It appears the site is using JavaScript for some of its content loads. The observeValue method is not invoked at all when this happens and is probably the culprit.
import UIKit
import WebKit
import SafariServices
class LandingPageViewController: UIViewController, SFSafariViewControllerDelegate, WKUIDelegate, WKNavigationDelegate {
#IBOutlet var urlNavigationItem: UINavigationItem!
#IBOutlet var backButton: UIBarButtonItem!
#IBOutlet var forwardButton: UIBarButtonItem!
#IBOutlet var reloadButton: UIBarButtonItem!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
view.addSubview(webView)
webView.addObserver(self, forKeyPath: "loading", options: .new, context: nil)
let request = URLRequest(url: url) //url is defined elsewhere
webView.navigationDelegate = self
webView.load(request)
webView.allowsBackForwardNavigationGestures = true
webView.uiDelegate = self
backButton.isEnabled = false
forwardButton.isEnabled = false
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "loading"){
backButton.isEnabled = webView.canGoBack
forwardButton.isEnabled = webView.canGoForward
}
}
#IBAction func back(_ sender: UIBarButtonItem) {
webView.goBack()
}
#IBAction func forward(_ sender: UIBarButtonItem){
webView.goForward()
}
#IBAction func reload(_ sender: UIBarButtonItem) {
let request = URLRequest(url: webView.url!)
webView.load(request)
}
func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
// Custom logic here
}
}
try with following delegate methods,
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void) {
decisionHandler(.allow)
}
You have implemented one but not call decisionHandler(.allow)
I was facing the same issue. What worked for me was stopping the navigation and then go back.
func goBack() {
self.webView.stopLoading()
if (self.webView.canGoBack) {
self.webView.goBack()
return;
}
}

Activity Indicator won't stop spinning after WKWebView load finish

I am very new to Swift and iOS development so thank you so much in advance for helping me!
I have tried every example online and every page on this site and I cannot get my Activity Indicator to stop being displayed once the page completes loading in my WKWebview.
Any assistance would be so appreciated! Thank you!
import UIKit
import WebKit
class FirstViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
#IBOutlet var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
activityIndicator.startAnimating()
activityIndicator.isHidden = true
activityIndicator.hidesWhenStopped = true
let url = Bundle.main.url(forResource: "Web/bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
self.webView.load(request)
}
func showActivityIndicator(show: Bool) {
if show {
activityIndicator.startAnimating()
} else {
activityIndicator.stopAnimating()
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
showActivityIndicator(show: false)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
showActivityIndicator(show: true)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
showActivityIndicator(show: false)
}
}
Just replace
webView.uiDelegate = self
with
webView.navigationDelegate = self
And it will work because
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
showActivityIndicator(show: false)
}
is WKNavigationDelegate's delegate method.

Resources