How can i display only the content of a website? - ios

I need to display only a part of the website, basically the CONTENT part of the website.
I can display the website with uiwebkit but, i dont want to display the entire website but only the CONTENT part of the webpage. I have no idea how to do this , as i have never done this and i did some google search but could not find anything on ios / swift.
How can i do this? Thanks.

you can use WKNavigationDelegate which allow you to implement did finish the function of webview in this function you can hide specific content of website.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let removeelementid = "javascript:(function() { " + "document.getElementsByClassName('td-header-menu-wrap-full td-container-wrap')[0].style.display=\"none\"; " + "})()"
webView.evaluateJavaScript(removeelementid) { (res, error) in
if error != nil
{
print("Error")
}
else
{
//do any thing
}
}
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
as you can see i am removing specific content of web by its class name td-header-menu-wrap-full td-container-wrap
Go to that web site right click -> click inspect. pick class names of content and call in this function . hope this will help you.

Related

Select file button is not opening the files library in webView of iOS

I have select file button in website which is opening in webview in my iOS application but the button is not opening the files library of iOS device. but the same link is working fine for android and even in desktop browser
I just used the webView simply and tried some delegate methods to listen the click but I am not able to found any method to work in this situation.
if let myUrl: URL = URL(string: urlWeb){
webViewTerms.delegate = self
webViewTerms.loadRequest(URLRequest(url: myUrl))
}
I suppose you have the below code in your website to listen the click event.
Adding a href to your button event
"window.location.href = “ myapp://click”;"
then below code will work for you.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if((webView.url?.scheme == "myapp") && webView.url?.host == "click"){
print("user click the button")
// Write the code to open Files folder of device.
}
decisionHandler(WKNavigationActionPolicy.allow)
}
I hope this would work for you.

How to run Code after asynchronus code is finished, Swift

I have a asynchronus Funktion, loadHTMLString, that I call to load the Text out of an HTML File. The loading Process needs time, almost a second. The Problem is: I want to go on with my Code, if the File is loaded, but I dont know when the loading is finished. Is there a way to do that?
func generateAndLoadPDF() {
// Thats my HTML File
let html = HTML.get(from: "Vorlage.html")
// I load this HTML File in my WebView, that takes almost a second
wkWebView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
// I delayed the Following Code, so the HTML-String has time to load
// Actually I dont want to delay the Code, I want that the following Code runs after .loadHTMLString is finished.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
// I generate the PDF
let wkPDFData = PDF.generate(using: self.wkWebViewPrintFormatter())
self.loadIntoWKWebView(wkPDFData)
}
}
Thanks,
Boothosh
Conform to WKNavigationDelegate
webView.navigationDelegate = self
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
}

How to set localStorage item in webView before loading a URLRequest?

I have to access a web page that has token on its localStorage
let javascript = "localStorage.setItem('token', 'abc')"
let url = URL(string: "https://test.com/abc")
webView.evaluateJavaScript(javascript) { (_, err) in
print(err?.localizedDescription)
// This will return 'A JavaScript exception occurred'
}
let request = URLRequest(url: url!)
webView.load(request)
I dont think this is possible. You get this error:
Error Domain=WKErrorDomain Code=4
"A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0,
WKJavaScriptExceptionMessage=SecurityError: The operation is
insecure., WKJavaScriptExceptionColumnNumber=0,
NSLocalizedDescription=A JavaScript exception occurred}
And there nothing you can do about for the WKWebView.
i solve it but i think it's a bad practice and the right way was to be able to send token in request's header
the main problem was that you can't run javascript script that add localStorage item before the web view load so i had to wait utile that page is loaded then then run javascript script that add needed token then reload that page Here is my code but again it's bad practice i think and i believe that front end team should allow me to end that token in request header
fist there was that method with reload the web view only one time
var loaded = false
func load() {
if !loaded {
webView.reload()
}
loaded = true
}
then i had to confirm to WKNavigationDelegate delegate to reload that page when it's loaded and here is my code
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript(javascript) { (_, err) in
self.load()
}
}
}

Is there a way to determined what element is at a point in a WKWebView or SFSafariViewController view?

I would like to know what element is "under" a particular point in a web view. For example, in the screenshot below, I would like to know what element is at point 550x, 275y. I'm hoping to get the result:
<img alt=​"Typhoon Mangkhut approaching the Philippines on September 14" src=​"/​/​upload.wikimedia.org/​wikipedia/​commons/​thumb/​3/​30/​Mangkhut_2018-09-14_0750Z.jpg/​140px-Mangkhut_2018-09-14_0750Z.jpg" width=​"140" height=​"111" srcset=​"/​/​upload.wikimedia.org/​wikipedia/​commons/​thumb/​3/​30/​Mangkhut_2018-09-14_0750Z.jpg/​210px-Mangkhut_2018-09-14_0750Z.jpg 1.5x, /​/​upload.wikimedia.org/​wikipedia/​commons/​thumb/​3/​30/​Mangkhut_2018-09-14_0750Z.jpg/​280px-Mangkhut_2018-09-14_0750Z.jpg 2x" data-file-width=​"1219" data-file-height=​"963">​
If the webview supports it, you could have javascript determine what's there, and report it to the webview. I apologize but I haven't run this code, I don't have the means to test it at the moment.
If you don't have the ability to add javascript to the page then you'll have to do it all in WKWebView .evaluateJavaScript
find the element value with:
var elem = document.elementFromPoint(550, 275);
Then tell your webview about it by creating a custom scheme request. you can do it this way:
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", "customScheme://" + JSON.stringify(elem);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
Intercept it in the webview delegate.
public func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
if(url.range(of:"customScheme://") != nil){
// this is your reported info
}
return true
}
This approach will only tell you the element as it relates to the web browser coordinates, not it's position on the device's screen. If you want to do it via device position you'll have to translate that and pass the coordinates over to the webview by invoking some javascript on the device.

Use wkwebview back reload

I am using WKWebview,I want that when I click the back button, it refresh the page rather than reading the page cache.
if (self.webView.canGoBack) {
[self.webView goBack];
[self.webView reload];
}
But, when you return to certain pages these are loaded twice and can't return to the first page.
Do not know to have people meet the same requirements and how to solve? The premise is not use UIWebview.
I was also facing the same issue. After removing the reload method, it was working correctly.
if ([self.wkWebView canGoBack]) {
[self.wkWebView goBack];
}
var backNavigation: WKNavigation?
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if backNavigation?.isEqual(navigation) ?? false {
webView.reload()
backNavigation = nil
}
}
in my page's js file, I add this and it worked.
var solveLoadingProblem = function () {
window.addEventListener("popstate", function (e) {
location.reload(true);
});
};
solveLoadingProble();

Resources