WKWebview adjustment to be in the background - swift

I've switched one of my apps to use WKWebview and it's the first time for me, I had a problem which is as you know I can't control it from the storyboard, I have a scrollview inside a sidebar that comes out when you click the button on the upper left, but it seems like it appears in the back of the view, how can I fix this please?
let configuration = WKWebViewConfiguration()
configuration.preferences = preference
webView = WKWebView(frame: view.bounds, configuration: configuration)
view.addSubview(webView)
please note that I already clicked the button in that screeshot but the scroll didn't show]1

Use this,
first import
import WebKit
set delegate of WKWebview
WKScriptMessageHandler , WKNavigationDelegate
declare webview
var webView: WKWebView!
var webConfiguration:WKWebViewConfiguration! = nil
override func loadView() {
webView = WKWebView(frame: .zero, configuration: self.webConfig())
webView.navigationDelegate = self
view = webView
}
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
/*if let url = URL(string: "http://192.168.1.122/arvee/piyush/mobileViewPages/owner/manager.html") {
let request = URLRequest(url: url)
webView.load(request)
}*/
let url = Bundle.main.url(forResource: "AddAppointment/bookApp", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
WKWebview Delegate
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print(error)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
let data = JSON(message.body)
}
func webConfig() -> WKWebViewConfiguration {
//if webConfiguration != false {
webConfiguration = WKWebViewConfiguration()
let userController = WKUserContentController()
userController.add(self, name: "buttonClicked")
userController.add(self, name: "pageLoaded")
webConfiguration.userContentController = userController
//}
return webConfiguration
}

Related

How to Autofill forms in WKWebView Using User Defaults or JavaScript

Hello Swift Developers!.
I am new to swift programming and need little help.
I am developing a very simple app, that should simply load the web using WkWebView and autofill the shipping form it have in on of its page.
I have successfully managed to fetch the page in webView(WkWebView).
First it loads this url https://www.adidas.com/us, after selecting the item in to cart it gets to this delivery page https://www.adidas.com/us/delivery where we have to fill this form. before this, no login information needed.
I am trying to make it done with both UIWebView and WkWebView but to no avail, here's my code, UIWebView part is commented.
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet weak var uiWebView: UIWebView!
var webView: WKWebView!
let url: String = "https://www.adidas.com/us/delivery"
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
//WKWebView programatically so it can run below iOS 11
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
if let safeUrl = URL(string: url){
let request = URLRequest(url: safeUrl)
webView.load(request)
}
// uiWebView.loadRequest(NSURLRequest(url: NSURL(string: url)! as URL) as URLRequest)
// let result = uiWebView.stringByEvaluatingJavaScript(from: "document.title")
// print("result is: \(result!)")
self.webView.evaluateJavaScript("document.getElementById('di-id-cab9a55c-9d253ce3').value = 'Hello'") { (result, error) in
print(result) //This will Print Hello
}
}
// func webViewDidFinishLoad(_ webView: UIWebView) {
//
// let email = defaults.string(forKey: "EMAIL")
// let password = defaults.string(forKey: "Pass")
//
// let fillForm = "document.getElementById('f_707d6a95-3ef9-4b76-a162-9361b4ef7d4d').value = \(password)"
// webView.stringByEvaluatingJavaScript(from: fillForm)
// }
}
And Here's the screenshot of inspect element of First Name field.
After this I would use user default for autofill data. that step I know how to do just stuck here!
Can I have any helpful code snippet or suggestion please? I am really stuck at this point!
Thanks in advance
First: stop using UIWebView since it's deprecated from one side and stop supporting by AppStore from Dec 2020.
You should set navigationDelegate to you WKWebView and set your values to the fields on webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) when the page is loaded e.g.:
override func viewDidLoad() {
super.viewDidLoad()
...
webView?.navigationDelegate = self
...
}
extension ViewController : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Set a delay for dynamic pages
//DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
let fillForm = "document.getElementsByName('firstName')[0].value = 'My Name'"
webView.evaluateJavaScript(fillForm, completionHandler: nil)
//}
}

The WKWebView was invalidated" UserInfo={NSLocalizedDescription=The WKWebView was invalidated

I am trying to retrieve html using webView. I don't want to write my webView code in the view file therefore trying to implement it in a different class. Below is my code.
class BlobHelper: NSObject,WKNavigationDelegate{
let webView = WKWebView()
func getLyrics(){
let url = URL(string: "https://Blob")!
let request = URLRequest(url: url)
webView.load(request)
webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML;", completionHandler: {(value,error)in
print(value)
print(error)
})
}
func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) { print("loaded") }
When I execute the code I get Optional(Error Domain=WKErrorDomain Code=3 "The WKWebView was invalidated" UserInfo={NSLocalizedDescription=The WKWebView was invalidated}).
What I tried
I was able to retrieve html using static but couldn't get the didFinish to call after loading.
Any help is appreciated.
class ViewController: UIViewController, WKNavigationDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let configuration = WKWebViewConfiguration()
self.webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = self
let url = URL(string: "https://stackoverflow.com")!
let request = URLRequest(url: url)
webView.load(request)
self.view = webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML;", completionHandler: { (value,error)in
print(value)
})
}}

WKWebview: how to prevent Laravel Page links from opening in safari?

I am new to swift.
I built a web application and serving it as an iOS app using WKWebView (this is for internal company use only).
In the web app, I used laravel pagination, this works properly on a browser and android webview.
what happens on iOS is, when I click the page number link inside the WKWebView, the link opens up in safari. What I want to do is open it on the same Webview.
This is my code inside ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = URL(string: "https://domain-online.asia")!
let urlRequest = URLRequest(url: url)
webView.load(urlRequest)
let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
toolbarItems = [refresh]
navigationController?.isToolbarHidden = false
}
override func loadView() {
let source: String = "var meta = document.createElement('meta');" +
"meta.name = 'viewport';" +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
"var head = document.getElementsByTagName('head')[0];" + "head.appendChild(meta);";
let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let userContentController: WKUserContentController = WKUserContentController()
let conf = WKWebViewConfiguration()
conf.userContentController = userContentController
userContentController.addUserScript(script)
webView = WKWebView(frame: CGRect.zero, configuration: conf)
webView.navigationDelegate = self
view = webView
}
func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}}
Implement decidePolicyFor and set policy as below,
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
guard navigationAction.navigationType == .linkActivated,
let url = navigationAction.request.url,
url.host?.contains("domain-online.asia") == false,
UIApplication.shared.canOpenURL(url) else {
decisionHandler(.allow)
return
}
UIApplication.shared.open(url)
decisionHandler(.cancel)
}

Open UIWebView Links In Safari

I have an iOS app using Swift 3. I have a uiwebview in this app. Whenever a user clicks on a link in the webview, it opens up inside the webview. I need it to where the links open up inside the Safari app on the iPhone. Here's the code:
class VideosViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://example.com")
webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Can you please give me some code that I can enter? I'm new at coding, so thanks a lot.
UPDATE: Thanks to an answer I got some code, but the links are still not opening in Safari. Here's the code so far...
class VideosViewController : UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView : UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://example.com") else { return }
let request = URLRequest(url: url)
webView.loadRequest(request)
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked {
guard let url = request.url else { return true }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
return false
}
return true
}
}
Using UIWebView:
Make sure your view controller conforms to UIWebViewDelegate and then implement this method in the controller:
class VideosViewController : UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView : UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://example.com") else { return }
webView.delegate = self
let request = URLRequest(url: url)
webView.loadRequest(request)
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked {
guard let url = request.url else { return true }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
return false
}
return true
}
}
Using WKWebView:
Use a WKWebView instead of a UIWebView and make sure your controller conforms to the WKNavigationDelegate protocol. So your implementation would look something like the following:
class VideosViewController : UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://example.com") else { return }
webView = WKWebView(frame: self.view.frame)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.isUserInteractionEnabled = true
webView.navigationDelegate = self
self.view.addSubview(self.webView)
let request = URLRequest(url: url)
webView.load(request)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
// Check if a link was clicked
if navigationAction.navigationType == .linkActivated {
// Verify the url
guard let url = navigationAction.request.url else { return }
let shared = UIApplication.shared
// Check if opening in Safari is allowd
if shared.canOpenURL(url) {
// Ask the user if they would like to open link in Safari
let alert = UIAlertController(title: "Open link in Safari?", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (alert: UIAlertAction) -> Void in
// User wants to open in Safari
shared.open(url, options: [:], completionHandler: nil)
}))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
decisionHandler(.cancel)
}
decisionHandler(.allow)
}
}
This way, when a user clicks a link in the web view, they will be prompted with an alert that will ask them whether or not they would like to open it in Safari, and if it is allowed, Safari will launch and the link will be opened.

How to load URL in UIWebView in Swift?

I have the following code:
UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")))
I am getting the following error:
'NSURLRequest' is not convertible to UIWebView.
Any idea what the problem is?
loadRequest: is an instance method, not a class method. You should be attempting to call this method with an instance of UIWebview as the receiver, not the class itself.
webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
However, as #radex correctly points out below, you can also take advantage of currying to call the function like this:
UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "google.ca")!))
Swift 5
webviewInstance.load(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
UIWebView in Swift
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL (string: "url here")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
// Do any additional setup after loading the view.
}
/////////////////////////////////////////////////////////////////
if you want to use webkit
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.webView.frame.size.height))
self.view.addSubview(webView)
let url = URL(string: "your URL")
webView.load(URLRequest(url: url!))
}
Try this:
Add UIWebView to View.
Connect UIWebview outlet using assistant editor and name your "webview".
UIWebView Load URL.
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Your webView code goes here
let url = URL(string: "https://www.example.com")
let requestObj = URLRequest(url: url! as URL)
webView.load(requestObj)
}
And run the app!!
Swift 3 doesn't use NS prefix anymore on URL and URLRequest, so the updated code would be:
let url = URL(string: "your_url_here")
yourWebView.loadRequest(URLRequest(url: url!))
In Swift 3 you can do it like this:
#IBOutlet weak var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Web view load
let url = URL (string: "http://www.example.com/")
let request = URLRequest(url: url!)
webview.loadRequest(request)
}
Remember to add permission at Info.plist, add the following values:
In Swift 4 or 4.2 You can use like:
Add WKWebView & connect to you view comtroller.
Your view is like bellow:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var wkwebview: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: URL(string: "**your URL**")!)
wkwebview?.load(request)
}
}
Allow Allow Arbitrary Loads true info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>Allow Arbitrary Loads</key>
<true/>
</dict>
Note info.plist will look like bellow
Swift 3 - Xcode 8.1
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL (string: "https://ir.linkedin.com/in/razipour1993")
let requestObj = URLRequest(url: url!)
myWebView.loadRequest(requestObj)
}
Swift 3
#IBOutlet weak var webview: UIWebView!
webview.loadRequest(URLRequest(url: URL(string: "https://www.yourvideo.com")!))
Easy,Tested and working 100%
Import webkit :
import WebKit
Assign IBOutlet to webview:
var webView : WKWebView!
set delegate:
class ViewController: UIViewController , WKNavigationDelegate{
Write code on viewDidLoad():
// loading URL :
let myBlog = "https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile"
let url = NSURL(string: myBlog)
let request = NSURLRequest(url: url! as URL)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
Write delegate methods:
//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")
}
look like:
in swift 5
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.
let url = NSURL (string: "https://www.simplifiedios.net");
let request = NSURLRequest(URL: url!);
webView.loadRequest(request);
As simple as that only 3 lines of codes :)
Ref: UIWebView Example
UIWebView loadRequest: Create NSURLRequest using NSURL object, then passing the request to uiwebview it will load the requested URL into the web view.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "http://www.google.com");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
self.view.addSubview(myWebView)
}
For more reference:
https://sourcefreeze.com/uiwebview-example-using-swift-in-ios/
Used Webview in Swift Language
let url = URL(string: "http://example.com")
webview.loadRequest(URLRequest(url: url!))
For swift 4.2, 5+
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//Func Gose Here
let url = URL(string: link)
let requestObj = URLRequest(url: url! as URL)
webView.loadRequest(requestObj)
}
For Swift 3.1 and above
let url = NSURL (string: "Your Url")
let requestObj = NSURLRequest(url: url as! URL);
YourWebViewName.loadRequest(requestObj as URLRequest)
Swift 4 Update Creating a WebView programatically.
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://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.loadRequest(myRequest)
}}
import UIKit
class ViewController: UIViewController{
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//Func Gose Here
loadUrl()
}
func loadUrl(){
let url = URL(string: "Url which you want to load(www.google.com)")
let requestObj = URLRequest(url: url! as URL)
webView.load(requestObj)
}
}
You can load a page like this :
let url: URL = URL(string:"https://google.com")!
webView.loadRequest(URLRequest.init(url: url))
Or the one-line approach :
webView.loadRequest(URLRequest.init(url: URL(string: "https://google.com")!))
webView is your outlet var.
Try This
let url = NSURL (string: "http://www.apple.com");
myWebView.loadRequest(NSURLRequest(URL: url!));