Webview detect javascript errors, or url changes - ios

class ViewController: UIViewController, UIWebViewDelegate{
#IBOutlet var containerView: UIView!
#IBOutlet var webView: UIWebView!
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
if let url = URL(string: "http://www.google.com/") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("webview finished load")
print(webView.request?.url)
}
So What I need, is try to detect 1 - javascript errors. 2 - or If not, url changes. 3 or If not , can I via javascript call a function in my controller? and i call a function from my controller to javascript?
I need a solution for an issue either way, I got problems in my webview.

Web view can communicate with the Javascript code but the communication is limited and complicated. To answer your specific questions:
Javascript errors - you can detect them directly inside your webpage.
URL changes if successful - you can do that directly inside your webpage (window.location = ...).
View controller change on error - you can do that by changing the URL to a specific URL, e.g. window.location = 'http://myredirect?error=Execution+failed'. Your web view controller can easily detect the change in URL using its delegate and react accordingly.

Related

How to make an activity indecator with a WKwebView in Swift

I am trying to make an iOS app with Swift and I have sorted out the webView which works perfectly, however now i'd like to implement an activity indicator. It displays properly and is animated, however when the page is loaded, the activity indicator does not disappear.
This is my code:
import UIKit
import WebKit
class FirstViewController: UIViewController {
#IBOutlet var webview: WKWebView!
// #IBOutlet var bg: UIView!
// #IBOutlet var logo: UIImageView!
#IBOutlet var ActInd: UIActivityIndicatorView!
// #IBOutlet var LoadingLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.com")
let request = URLRequest(url: url!)
webview.load(request)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ : WKWebView) {
ActInd.startAnimating()
//LoadingLabel.isHidden = true
//logo.isHidden = true
//bg.isHidden = true
}
func webViewDidFinishLoad(_ : WKWebView){
ActInd.stopAnimating()
// LoadingLabel.isHidden = true
//logo.isHidden = true
//bg.isHidden=true
}
}
I'd appreciate it enormously if someone could help me get the activity indicator to disappear.
Thank you in advance
Try setting
ActInd.hidesWhenStopped = true // In viewDidLoad
Conform to WKNavigationDelegate, and you can track webView processes..
The methods of the WKNavigationDelegate protocol help you implement custom behaviors that are triggered during a web view's process of accepting, loading, and completing a navigation request.
There are two methods that you can use :
func webView(WKWebView, didStartProvisionalNavigation: WKNavigation!)
Called when web content begins to load in a web view.
Here you can show the activity indicator
func webView(WKWebView, didFinish: WKNavigation!)
Called when the navigation is complete.
For more info check WKNavigationDelegate documentation
So here are the steps :
1- Conform to WKNavigationDelegate.
2- Assign your view controller to be your web view’s navigationDelegate.
3- Implement didStartProvisionalNavigation and didFinish methods
Example :
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webview: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webview.navigationDelegate = self
/*
....
*/
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// hide/stop indicator
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
//show your activity
}
}
On view did load just start your indicator and on webViewDidFinishLoad just hide that loader.
The methods won't ever be called.
Because, you haven't set your ViewController as NavigationDelegate. It needs to conform to the WKNavigationDelegate. You can then set the delegate in the viewDidLoad() function:
webview.navigationDelegate = self
The 2 methods you're calling also don't seem to exist. These are the equivalent delegate methods: webView(:didCommit:) and webView(:didFinish:)
You can set "Hides when stopped" and "Animated" in the storyboard or do it programatically as it is explained in this answer.
You should also take a look at the API Design guidelines explaining how code should be written. Especially you should:
Follow case conventions. Names of types and protocols are UpperCamelCase. Everything else is lowerCamelCase.

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))
}

Web View not acting as UIWebView

I've scoured the internet for 3 hours now, and have just given up. I figured since no one else on this entire planet has had this problem before, maybe I should ask the stackoverflow community, and see what happens.
I'm familiar with xCode, and objective C, but am learning swift. For my first app I figured I'd build a super simple webview app that loads a page. Easy right? I can code this in about 15 seconds in objective c, and I did, it works fine. Maybe there's something I'm missing here.
When I attempt to connect the object to the UIWebView it won't connect.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
#IBOutlet var textBox: UITextField!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
let url = NSURL(string: "https://www.google.com")!
webView.loadRequest(NSURLRequest(URL: url))
webView.allowsBackForwardNavigationGestures = true
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Image of it not connecting. It's not connecting like it should
Change type from WKWebView to UIWebView.You have added UIWebView from object library which your are trying to connect to a WKWebView object so it will not connect. WKWebview has to be added programmatically as it is not present in object library.
These two classes have similar functionalities but they are not connected to each other in anyway.
You can use the following code to build a webView app that loads a page.
import UIKit
class ADKWebViewDemoViewController: UIViewController {
#IBOutlet weak var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webview.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")!))
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Trouble adding webpage to UIViewController (Swift)

I am trying to get a webpage to appear in my swift application. I have added the WebView and wrote the following code in the controller class but when I run my app, there is only a white screen. Am I missing something?
class WebViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let WebView = UIWebView(frame: view.bounds)
let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL:url!)
WebView.loadRequest(request)
view.addSubview(WebView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Run your code in viewDidLayoutSubviews. You are setting the WebView's frame to the view's bounds. The view's frame has not fully loaded yet.
Never mind, I'm an idiot and forgot the s in http thanks all.
You need to tell the webView who is its delegate, just binding the outlet won't be enough.
In viewDidLoad add this:
self.webView.delegate = self
Then you need to conform to UIWebViewDelegate (which you are already doing, but I suggest you extend your view controller instead):
extension WebViewController: UIWebViewDelegate {
func webViewDidStartLoad(_ webView: UIWebView) {
print("Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Finished")
}
}
And last but not least, you need to allow your application to load external URL's by adding a transport security, I suggest you check this post.
Please note I'm using Swift 3, you may need to make little changes.

XCODE swift - webview can't open ics link - in safari it works

I've made in Swift a webview & in that webview there are links to ics file on the same domain of the webview. Now, when you click this link in Safari, you can perfectly add this to your calendar, but when I try this in iOS simulator, or on my device, I can't 'open' the ICS file link.
Any idea what I am doing wrong? Thanks!
This is my code:
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var webAgenda: UIWebView!
#IBOutlet weak var activityagenda: UIActivityIndicatorView!
var url = "http://******.be/agenda.php"
func loadURL () {
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
webAgenda.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
loadURL()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(webView: UIWebView) {
activityagenda.hidden = false
activityagenda.startAnimating()
}
func webViewDidFinishLoad(webView: UIWebView) {
activityagenda.hidden = true
activityagenda.stopAnimating()
}
because you load a http site so in iOS9 will have problem with App Transport Security.
disable ATS here

Resources