NavigationBar isn't appear when using WebViewKit - swift

I've a ViewControllerWeb to show a WebView. I need nav bar to show some options to user, so I need that NavigationBar is showed.
Problem: The navigation bar is not showed.
ViewControllerWeb.swift
import UIKit
import WebKit
class ViewControllerWeb : UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var progress: UIActivityIndicatorView!
var url : String?
override func viewDidLoad() {
self.webView.allowsBackForwardNavigationGestures = true
self.webView.navigationDelegate = self
let request = URLRequest(url: URL(string: url!)!)
self.navigationController?.navigationBar.isHidden = false
self.webView.load(request)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didFinish")
self.progress.stopAnimating()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("didiFail")
self.progress.stopAnimating()
}
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
print("webViewWebContentProcessDidTerminate")
self.progress.stopAnimating()
}
func webView(_ webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation!) {
print("didStartProvisionalNavigation")
self.progress.startAnimating()
}
func webView(_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error) {
print("didFailProvisional")
self.progress.stopAnimating()
}
}
Storyboard
Result on simulator iPhone7
The NavigationBar isn't showed.

Present ViewControllerWeb with UINavigationController.
// Storyboard name
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// Storyboard withIdentifier
let viewController = storyBoard.instantiateViewController(withIdentifier: "ViewControllerWeb")
let navBarController = UINavigationController(rootViewController: viewController)
self.present(navBarController, animated: true, completion: nil)

Related

WKWebView not displaying after successful load, only seeing black screen

I'm new to swift & am attempting to open a login page, login, use the WKNavigationDelegate to get the redirect uri and save the response. I was able to open the url using UIApplication.shared.open but attempting to hit the url using WKWebview, I am getting a loaded response but no view appears.
Am I not declarling the view correctly or is WKWebView not supposed to be used in that manner? This is the code i'm running, its building succesfully and responding that its 'loaded'. Any help would be be appreciated thanks.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
// The webview
var webViewObj: WKWebView!
var webNav: WKNavigation!
// this calls the dbhelper to create a db and a table
let db = DBHelper()
var list = [DBGrade]()
override func loadView() {
super.loadView()
let webConfiguration = WKWebViewConfiguration()
webViewObj = WKWebView(frame: .zero, configuration: webConfiguration)
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.hackingwithswift.com")
let request = URLRequest(url: url!)
webViewObj.navigationDelegate = self
self.webViewObj.uiDelegate = self
webViewObj.load(request)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("loaded")
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
let hostStr = String(describing: navigationAction.request.url)
let host = hostStr
if host.range(of: "hackingwithswift.com") != nil {
decisionHandler(.allow)
print("did it")
return
}
else{
decisionHandler(.cancel)
}
}
}
So moving my delcaration of 'self.webViewObj.uiDelegate = self' & adding 'view = webViewObj' to the loadView() function worked & I'm able to see the webpage appear.

Issue with programmatically adding ActivityIndicator with WKWebView, not appearing

My code successfully loads the web page that I want it to; however, I'm having issues adding my activity indicator for while it is loading.
I am relatively new to swift, so I was reading other stackexchange examples. I created a UIActivityIndicator, set it in the override func, but it doesn't show up.
The webpage loads fine. Here is the runnable code.
import UIKit
import WebKit
class FeedbackWebViewController: UIViewController, WKNavigationDelegate {
let indicator = UIActivityIndicatorView(style: .gray)
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
indicator.center = webView.center
indicator.hidesWhenStopped = true
webView.addSubview(indicator)
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.apple.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
self.indicator.startAnimating()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
indicator.stopAnimating()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
indicator.stopAnimating()
}
}
DONT override loadView. bad things may happen.
move code in didLoad. it should work.
try to put the related 3 lines to viewDidAppear rather than viewDidLoad. It works for me.

WebView Content size issue in swift

I have a web view in a view controller which is loaded from a url coming from server. The content coming inside the webview is very small and unreadable.
I've implemented all the code but I'm confused as to why it is not showing properly. This is how content is shown, content start from under the header image,
This is the code used to load the url in the webview:
func loadWeb(webURL: String) {
DispatchQueue.main.async {
let url = URL(string: webURL)
self.webView.navigationDelegate = self
self.webView.load(URLRequest(url: url!))
self.view.makeToastActivity(.center)
self.webView.uiDelegate = self
self.view.hideToastActivity()
}
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
showActivityIndicator(show: false)
self.view.hideToastActivity()
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
showActivityIndicator(show: false)
self.view.hideToastActivity()
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
showActivityIndicator(show: false)
self.view.hideToastActivity()
}

WKNavigationDelegate methods not called

I'm trying to write a controller with a webview and I need WKNavigationDelegate methods, the problem is that these methods are not being executed at all.
This is my controller:
import UIKit
import WebKit
public class WebViewController: UIViewController {
private let webView: WKWebView = WKWebView()
override public func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.isUserInteractionEnabled = true
view = webView
}
}
extension WebViewController: WKNavigationDelegate {
private func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
debugPrint("didCommit")
}
private func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
private func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
debugPrint("didFail")
}
}
This controller is being added to a top view controller using a container view, also the webview loads the website correctly. Has anyone had a similar problem or knows if I'm doing something wrong
It looks like you are adding private with WKWebViewDelegate Methods, Remove those like this and it will work.
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
debugPrint("didCommit")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
debugPrint("didFail")
}
}
Try this out, Also don't forgot to load URL in WebView or whatever you want to load.

Open a link using the button and textfield

Can I open a link using the button and textfield on which the link is listed on another page within the app itself and not with Safari?
I'm using Swift 4
You have to open webview in destination view controller and send url string from first view controller to destination like below:
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBAction func action(_ sender: Any) {
if let mainVC = storyboard?.instantiateViewController(withIdentifier: "second") as? DestinationViewController {
mainVC.urlStr = textField.text
self.navigationController?.pushViewController(mainVC, animated: true)
}
}
}
Destination View controller:
class DestinationViewController: UIViewController {
var urlStr: String?
override func viewDidLoad() {
super.viewDidLoad()
let webView = UIWebView(frame: self.view.frame)
self.view.addSubview(webView)
if let urlString = urlStr, let url = URL(string: urlString) {
webView.loadRequest(URLRequest(url: url))
}
}
}
Just Use WKWebView to open the link on Button action using this
Import WebKit
import WebKit
In your FirstViewController pass the URL while pushing to
SecondViewController
FirstViewController
if let secondVC = (UIStoryboard.init(name: "Main", bundle: nil)).instantiateViewController(withIdentifier: "secondVCID") {
secondVC.url = textFiled.text // OR send the URL you want to send
self.navigationController?.pushViewController(secondVC, animated: true)
}
In SecondViewController
Import Webkit
declare the delegate
class SecondViewController: UIViewController,WKNavigationDelegate {
declare the URL var so that u cab access it n FirstVC
var url: String?
#IBOutlet weak var webView: WKWebView!
In ViewDidLoad Of SecondVC
let webUrl = NSURL(string: url!)
let request = NSURLRequest(URL: webUrl)
// load request in webview.
webView.navigationDelegate = self
webView.loadRequest(request)
and Implement all delegate methods of WKWebview
//MARK:- WKNavigationDelegate
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}