How to load excel / pdf server url in WKWebView Swift4 - ios

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.

Related

how to open url with dynamic query string in WKWebView

I have a button on my website which has href https://live.example.net/?dynamic_encypted_query-string, So I wanted to open this url in my webview of ios app. Currently I'm building a web app through capacitor framework where I wrote a code to my WebViewController.swift file
class WebViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://live.example.net/?*")
let myRequest = URLRequest(url: url!)
webView.load(myRequest)
// Do any additional setup after loading the view.
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
This opens the domain(https://live.example.net) in the web view but i wanted to open it with the query string data.
let url = URL(string: "https://live.example.net/?*")
I think this doesn't work to fetch the query string data as well.

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

WebKit View not opening certain URLs

I want the webkit view to display my domain webpage but it won't work. It works for regular known websites but not for my own domain. I am using the code below. Also I added this to the pList file but no joy. Thanks in advance.
pList
NSAppTransportSecurity
NSAllowsArbitraryLoads
Code:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
webView.frame = view.bounds
webView.navigationDelegate = self
let url = URL(string: "https://www.MYDOMAIN.co.uk")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Try for this
class YourCalssviewController : UIViewController ,WKNavigationDelegate ,WKUIDelegate{ }
Write in viewDidLoad()
let url = NSURL(string: "https://www.MYDOMAIN.co.uk")
let request = NSURLRequest(url: url! as URL)
self.webView.load(request as URLRequest)
your code is working fine, the problem here with the URL itself, if you tried an other URL like "https://www.youtube.com/" it will work fine

Intercept javascript file from loading in web view

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

WKWebView show white screen when loading HTTPS URL of Angular App

apologies for the simple question:
I am following Apple's guide on WKWebView to the dot. Same code and everything.
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://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
Source
This is the URL I am trying to link to: https://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3
Except that I am getting a white screen when the viewcontroller tries to load the page.
Any reasons why? The code works fine for Google's homepage and this URL is secure so that isn't the issue.
Also, the website is not mine and I cannot edit it's files.

Resources