Intercept javascript file from loading in web view - ios

I am using WKWebView to make a web view app
let url = NSURL(string: "http://www.example.com")
let request = NSURLRequest(url: url! as URL)
homeWebView.load(request as URLRequest)
But I want to intercept some javaScript file of this website before it
loads to change functionality and make the app a bit native looking
Any help would be appreciated.
Thank You.

Following is the complete example. I hope it will help you.
import UIKit
class ViewController: UIViewController,UIWebViewDelegate {
private var webView: UIWebView!
override func loadView() {
webView = UIWebView(frame: .zero)
webView.delegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// set the view controller title, navigation etc
self.loadWebPage()
}
func loadWebPage() {
let url = URL(string: "https://www.google.com.pk/")
// let localFileURL = URL(fileURLWithPath: Bundle.main.path(forResource: "ExampleHtmlFileName", ofType: "html", inDirectory: "HtmlFilesFolderName")!)
let myRequest = URLRequest(url: url!)
webView.loadRequest(myRequest)
}
//MARK: UIWebView Delegate
func webViewDidFinishLoad(_ webView: UIWebView) {
let jsScript = "exampleJSMethod()"
_ = webView.stringByEvaluatingJavaScript(from: jsScript)
}
}

Related

Error : Cannot convert value of type 'URL?' to expected argument type 'URLRequest

import UIKit
import WebKit
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://www.google.com")!
webView.load(url)
webView.allowsBackForwardNavigationGestures = true
}
}
Hello, I am new to Swift programming. I am trying to load a simple website on navigation controller with the help of WKWebView. But I am unable to continue with it. As I am facing an error with line
let URL = let url = URL(string: "https://www.google.com")!
I need to know two things
What does the error mean?
How can I resolve it?
The error is pretty clear. You need to pass a URLRequest object as parameter.
let url = URL(string: "https://www.google.com")!
webView.load(URLRequest(url: url))

How to load excel / pdf server url in WKWebView Swift4

I wanted to load the excel sheet url/pdf url (document url) in WKWebView. As I am trying to open that url from browser then it directly downloads that document. So I am confused whether 1] I have to directly load the url into WKWebView or 2] first download those files and then load that local path in WKWebView.
If I go with the 2nd option then will my document download every time?
Following is the code which I have tried but it shows nothing
import UIKit
import WebKit
class OpenReportsFullScreenVC: UIViewController,WKUIDelegate {
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
//first way :- webView.load(URLRequest(url: URL(string: "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls")!))
// second way
let filePathURLData = "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls"
let fileURL = URL(fileURLWithPath: filePathURLData )
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
}
override func loadView() {
//initialise webview
let webViewConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
webView.uiDelegate = self
view = webView
}
}
Try with this code
import UIKit
import WebKit
class ViewController: UIViewController , **WKNavigationDelegate**{
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myBlog = "your URl"
let url = NSURL(string: myBlog)!
let request = NSURLRequest(url: url as URL)
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
}
}
File path cannot have blank characters like in your example.

Login details are not saving in UIWebView in Xcode 9.4?

I want to ask that I am making a UIWebView App in Xcode 9.4?
I didn't add any line of code in AppDelegate. I want to ask that how to implement save session time to keep user logged in on any website. Even if a user closes the app from task manager also...
This is my code in View Controller
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
swipeLeftRecognizer.direction = .left
swipeRightRecognizer.direction = .right
webView.addGestureRecognizer(swipeLeftRecognizer)
webView.addGestureRecognizer(swipeRightRecognizer)
loadAddress()
}
#objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) {
if (recognizer.direction == .left) {
if webView.canGoForward {
webView.goForward()
}
}
if (recognizer.direction == .right) {
if webView.canGoBack {
webView.goBack()
}
}
}
func loadAddress() {
let requestURL = NSURL(string: "https://uims.cuchd.in/uims/")
let request = NSURLRequest(url: requestURL! as URL)
webView.loadRequest(request as URLRequest)
}
func webViewDidStartLoad(_ :UIWebView) {
activity.startAnimating()
NSLog("website is loading")
}
func webViewDidFinishLoad(_ :UIWebView){
activity.stopAnimating()
NSLog("website finished loading")
}
}
Please anyone, help me
Thanks in advance
Set httpShouldHandleCookies = true
let requestURL = NSURL(string: "https://uims.cuchd.in/uims/")
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpShouldHandleCookies = true
Even you should use URL and URLRequest directly instead of converting NSURLRequest to URLRequest & NSURL to URL
let url = URL(string: "https://uims.cuchd.in/uims/")
var request = URLRequest(url: url!)
request.httpShouldHandleCookies = true

open webkit view in different viewcontroller

I want to make an app for my mothers books where in the first viewcontroller there are images with buttons next to them that open a certain book in fullscreen. As much as I know I need a new viewcontroller to open webkitview in fullscreen. I managed to open webkitview in fullscreen through viewDidLoad() but I want to insert more than one book and that in a neet looking way.
All I need is to know how to tell the button to open the next viewcontroller where I spread webkitview over the whole display to make it act like fullscreen. Here is the code I did until now...
class ViewController: UIViewController {
#IBOutlet weak var webViewDisplay: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "prayers", ofType: "pdf")
let url = URL(fileURLWithPath: path!)
let request = URLRequest(url: url)
webViewDisplay.load(request)
}
}
You should configure your WkWebView first but you can't do that from storyboard. Instead of outlet try to add WKWebView programmaticaly.
private var webView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
let webViewConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
view = webView
let path = Bundle.main.path(forResource: "prayers", ofType: "pdf")
let url = URL(fileURLWithPath: path!)
let request = URLRequest(url: url)
webView.load(request)
}

How i can fix this error :Cannot convert value of type 'NSURL' to expected argument type 'URLRequest'

i was use it solution but it's not work
let targetURL = URL(string: request)
let request = URLRequest(url: targetURL!)
this is my code
import UIKit
class WebUIViewController: UIViewController,UIWebViewDelegate {
#IBOutlet weak var smartWebView: UIWebView!
#IBOutlet weak var activityWebload: UIActivityIndicatorView!
var smart :String?
i am pass url by first view controller using smart variable and i print url in console but cant show in my web view
override func viewDidLoad() {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Web View Controller
func webScreen(){
let request = NSURL(fileURLWithPath: smart!)
smartWebView.loadRequest(request)
super.viewDidLoad()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activityWebload.startAnimating()
}
func webViewDidStartLoad(_ webView: UIWebView) {
activityWebload.stopAnimating()
}
#IBAction func refreshpage(_ sender: Any) {
smartWebView.reload()
}
}
Please check your URL is Correct or not after to open in webView
if let url = URL(string: "Your Url"){
if UIApplication.shared.canOpenURL(url) {
let request = URLRequest(url: url)
smartWebView.loadRequest(request)
}
}
This code is wrong:
let request = NSURL(fileURLWithPath: smart!)
smartWebView.loadRequest(request) //The parameter here should
//be of type URLRequest
The first line creates request as an NSURL. You name it request but it's not a URLRequest
The function UIWebView.loadRequest(_:) wants a parameter of type URLRequest, not URL or NSURL. You need to add a line to create a URL request from the URL:
//First create a URL object
let theURL = URL(fileURLWithPath: smart!)
//Now use the URL to create a ULRRequest object
let request = URLRequest(url: theURL)
//Use the URLRequest to submit the load request.
smartWebView.loadRequest(request)
(Note that you don't need to create an Objective-C NSURL. You can use the native Swift URL type.)

Resources