I'm developing an app that is basically a Web View displaying a website designed to mimic an app. The majority of URLs will be internal however there will be a few which will redirect to other sites. All these links will have target="_blank".
I've spent days browsing this site and others to find a solution that will open _blank URLs in safari however everything I've tried has failed.
Any help with this would be much appreciated. I've added below the code from my View Controller
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet weak var webView:WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
let myURL = URL(string:"https://example.com")
let myRequest = URLRequest(url: myURL!)
webView?.load(myRequest)
}}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if let frame = navigationAction.targetFrame,
frame.isMainFrame {
return nil
}
webView.load(navigationAction.request)
return nil
}
replace your createWebViewWith with this one
extension ViewController: WKUIDelegate {
//This opens maps or any other url given in new tab
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
}
Related
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.
I'm using WKWebView to create a browser instance of my Angular webapp. In this webapp I use jspdf to generate and download PDF files. When trying to download this file, I get no feedback in the console but nothing is happening. Does anyone have a solution for this problem? This is my code:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var mWebKit: WKWebView!
let url = URL(string: "foo.bar")
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: url!)
mWebKit.load(request)
}
}
Allow mWebKit to load pdf files. By default the wkWebView cancels the urls containing pdfs.
override func viewDidLoad() {
super.viewDidLoad()
mWebKit.uiDelegate = self // set the delegate to call decidePolicyfor.
let request = URLRequest(url: url!)
mWebKit.load(request)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
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.
I have assigned delegate to wkwebview uiDelegate and implemented it's method create web view with configuration, but it is not being called. I have even placed breakpoint but it is not hitting.
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.uiDelegate = self
let requestURL = URL(string: "https://www.google.com")!
let request = URLRequest(url: requestURL)
webView.load(request as URLRequest)
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let config = WKWebViewConfiguration()
return WKWebView(frame: webView.frame, configuration: config)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
}
It seems like it doesnt call the delegate on the initial init of the webview ... I recommend you to just create the webview from code
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
configuration.allowsAirPlayForMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = []
webView = WKWebView(frame: containerView.frame, configuration: configuration)
webView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(webView)
NSLayoutConstraint.activate([
webView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
webView.topAnchor.constraint(equalTo: containerView.topAnchor),
webView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
the containerview is just a basic UIView set in my xib so just set this uiview in your xib instead of a wkwebview and you should be good to go
I am trying to create a WKWebView app with a UIActivityIndicator (in Xcode 9). Below is my code, but when I try to simulate it, the activity indicator doesn't stop, neither hide.
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var loader: UIActivityIndicatorView!
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://www.amritvani.nl")
let request = URLRequest(url: url!)
webView.navigationDelegate = self as? WKNavigationDelegate
webView.load(request)
}
func webViewDidStartLoad(webView: WKWebView){
loader.startAnimating()
}
func webViewDidFinishLoad(webView: WKWebView){
loader.stopAnimating()
loader.hidesWhenStopped = true
}
}
Could someone help me with this problem?
Your delegate methods are wrong. Replace webViewDidStartLoad(webView:) with this:
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
loader.startAnimating()
}
...and webViewDidFinishLoad(webView:) with this:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
loader.stopAnimating()
}
Also, you should conform your class to WKNavigationDelegate:
class ViewController: UIViewController, WKNavigationDelegate
There are two more problems with your code. Firstly, you shouldn't cast your class to WKNavigationDelegate with as? if it already conforms to it:
webView.navigationDelegate = self
Secondly, you should set the hidesWhenStopped attribute of your activity indicator to true before stopping it, preferably in Interface Builder or in viewDidLoad. This is not crucial, but it makes much more sense if someone else reads your code and it's also superfluous if webView(_:didFinish:) executes multiple times.