Error when tapping on anything inside loaded page in WKWebview - ios

I have an issue with WKWebview. The pages load without any problems, but whenever I tap on any button on website, slide photos or accept cookies policy I get the following error:
2019-10-14 11:27:02.933458+0200 AppTitle[2592:447958] [general] Connection to daemon was invalidated
Does anyone know what may be the issue or what am I missing? This error causes further problems inside the app. Here is my code:
import UIKit
import WebKit
import TinyConstraints
class DetailsVC: UIViewController, WKNavigationDelegate, WKUIDelegate {
let myUrl = URL(string: "https://google.pl/")
var webView: WKWebView = {
let webview = WKWebView()
return webview
}()
func addWebView() {
view.addSubview(webView)
webView.edgesToSuperview(insets: .top(40) + .right(0) + .left(0), usingSafeArea: true)
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(URLRequest(url: self.myUrl!))
}
override func viewDidLoad() {
super.viewDidLoad()
// Add webview
addWebView()
}
}

Related

How can I link an app page to a website on Xcode 12?

I'm a beginner in programming and am trying to make a simple app for displaying a webpage after choosing from menu options. However, I'm having trouble linking the code window to the storyboard, especially for linking a new page for the app to a new class. I think it's partly b/c I can't view the code and storyboard side by side.
I've also written code for displaying the website in on an app page, but I'm getting an error message saying "Argument passed to call that takes no arguments" (code below). Any tips on how I can address these?
import WebKit
import UIKit
//class ViewController: UIViewController {
class WebViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad() {
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
}
override func viewDidLoad() {
super.viewDidLoad() {
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
}
Above should be:
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
without the inner brackets.

WebKit View not opening certain URLs

I want the webkit view to display my domain webpage but it won't work. It works for regular known websites but not for my own domain. I am using the code below. Also I added this to the pList file but no joy. Thanks in advance.
pList
NSAppTransportSecurity
NSAllowsArbitraryLoads
Code:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
webView.frame = view.bounds
webView.navigationDelegate = self
let url = URL(string: "https://www.MYDOMAIN.co.uk")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Try for this
class YourCalssviewController : UIViewController ,WKNavigationDelegate ,WKUIDelegate{ }
Write in viewDidLoad()
let url = NSURL(string: "https://www.MYDOMAIN.co.uk")
let request = NSURLRequest(url: url! as URL)
self.webView.load(request as URLRequest)
your code is working fine, the problem here with the URL itself, if you tried an other URL like "https://www.youtube.com/" it will work fine

WebView does not load

I just started developing with swift, so I am sorry if the question is basic/stupid.
I have the following setup, just a test
import WebKit
import UIKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://hackingswift.com")!
webView.load(URLRequest(url:url))
webView.allowsBackForwardNavigationGestures = true
}
}
Unfortunately the browser doesn't load. The simulator only shows an empty navigation bar.
Suggestions? I am following a tutorial on hackingswift, so it's supposed to work.
You have to add webView as a subview or make an IBOutlet using Interface builder.
Try this:
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
func loadView() {
webView = WKWebView()
webView?.navigationDelegate = self
self.view.addSubview(webView!)
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadView()
let url = URL(string: "https://hackingswift.com")!
webView?.load(URLRequest(url:url))
webView?.allowsBackForwardNavigationGestures = true
}
}
If you want it a bit more simple (without nullable variable), for example:
class ViewController: UIViewController, WKNavigationDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
self.view.addSubview(webView)
webView?.allowsBackForwardNavigationGestures = true
self.loadUrl("https://hackingswift.com")
}
func loadUrl(_ url: String) {
if let url = URL(string: url) {
webView.load(URLRequest(url:url))
}
}
}
EDIT: it looks like some websites to load, while others do not, even if they are secure. If I put apple.com in the example, it loads, but a few others do not
Your url should be started with http or https for the webView to load.
Another possible reason is that your url containing an invalid certificate. Add the delegate function below into your code. You have to let WKWebView to bypass the certificate checking. However, this code is never recommended to go into production. You should be careful about what website your webView should and will load.
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
The problem is this line:
let url = URL(string: "https://hackingswift.com")!
There is no such URL on the Internet, so you're not actually going to see anything. (You won't see anything if you paste that URL into any browser.)
So change that line to this:
let url = URL(string: "https://www.hackingwithswift.com")!
Now run the app, and presto, you'll see the web site:

WKWebView show white screen when loading HTTPS URL of Angular App

apologies for the simple question:
I am following Apple's guide on WKWebView to the dot. Same code and everything.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
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://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
Source
This is the URL I am trying to link to: https://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3
Except that I am getting a white screen when the viewcontroller tries to load the page.
Any reasons why? The code works fine for Google's homepage and this URL is secure so that isn't the issue.
Also, the website is not mine and I cannot edit it's files.

Simple Webview: navigationDelegate crashes on start

I have a small problem with my WebView. This is what I do in my app:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView! // draged from storyboard
override func viewDidLoad() {
super.viewDidLoad()
var urlToLoad : (String)
urlToLoad = "http://wwww.myapp.com"
webView.sizeToFit()
webView.isUserInteractionEnabled = true
let url = URL(string: urlToLoad)
let request = URLRequest(url:url!)
//webView.navigationDelegate = self
webView.load(request)
}
As you see the row "webView.navigationDelegate = self" is commented. If I uncomment it my app will crash on start and telling me this:
Debugger: libc++abi.dylib: terminating with uncaught exception of type NSException
Any idea of why is this happening and how to solve this? I need this in order to use methods like "didFinish navigation" that I already implemented in the page but they are never called.
Also, by having a look at my code... you see anything I should change regarding the use of my webview?
Thank you for helping me.
You are using UIWebView and trying to call the methods of WKWebView which is leading to crash your app due to unrecognised selector send on UIWebView.
Try to create new project and use below mentioned code.
import "WebKit" framework to your class.
override func viewDidLoad() {
super.viewDidLoad()
let myWebView:WKWebView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
myWebView.navigationDelegate = self;
let url = URL(string: "https://www.Apple.com")!
myWebView.load(URLRequest(url: url))
self.view.addSubview(myWebView)
}
then implement WKNavigationDelegate method
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
let url = webView.url
print(url as Any) // this will print url address as option field
if url?.absoluteString.range(of: ".pdf") != nil {
pdfBackButton.isHidden = false
print("PDF contain")
}
else {
pdfBackButton.isHidden = true
print("No PDF Contain")
}
}
Hope this will help you!
You are binding an UIWebView to a property of type WKWebView. As a result it crashes when trying to call navigationDelegate on the UIWebView, as it does not offer this method.
There is no template version of WKWebView available in Storyboard, so you have to create the WKWebView programatically:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: view.frame)
webView.navigationDelegate = self
view.addSubview(webView)
let url = URL(string: "https://www.google.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
You don't initialize the webView. The app crashes because it's nil.
You need to bind it to the storyboard or the xib file (uncomment the #IBOutlet) or initialize it programmatically and then add it to you viewController's view.
I was getting the error Value of type '(WKWebView, WKNavigation?) -> ()' has no member 'navigationDelegate' (which I'm guessing was the same issue as what was causing the crash when adding webView.navigationDelegate = self).
I found that the view controller class needs to implement the WKNavigationDelegate protocol (kinda like inheritance) by adding it to the class definition, which fixed the error for me. Make sure to have that as in the first line of this code:
class ViewController: UIViewController, WKNavigationDelegate {
// ...
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// ...
}
}

Resources