Getting Title from Wkwebview Using Evaluatejavascript

Getting title from WKWebView using evaluateJavaScript

You shouldn't need to use javascript to retrieve the page title. WKWebView has a title property that will retrieve this for you

https://developer.apple.com/library/ios/documentation/WebKit/Reference/WKWebView_Ref/#//apple_ref/occ/instp/WKWebView/title

title The page title. (read-only)

SWIFT
var title: String? { get }

The WKWebView class is key-value observing (KVO) compliant for this property.

Available in iOS 8.0 and later.

How do I get WKWebView.evaluateJavaScript to return data in a function call

you should use a completion handler, something like this:

func checkTitle (forWebView webView: WKWebView, title: String, completion: @escaping (_ titleString: String?) -> Void) {

webView.evaluateJavaScript("document.title", completionHandler: { (innerHTML, error ) in

// Logic here
completion(innerHTML as? String)
})
}

evaluateJavaScript() in WKWebView works only the first time

Updating the value of var in javascript is rather dangerous in my opinion. The script could have been already executed at the time, not even mentioning other Javascript quirks with var. Ideally, you should pass the values using a javascript function that would take the new values as parameters, for example:

function update(oneWeek, oneMonth) { // add other parameters
document.querySelector('.oneWeek').innerHTML = oneWeek;
}

and then:

webView.evaluateJavaScript("update(\"\(oneWeek)\", \"\(oneMonth)\");") { (result, error) in

Another part of the problem is the fact that loadHTMLString starts the loading but we don't know whether the content has already been loaded or not when calling evaluateJavaScript. Ideally, you should call the update in one of the WKNavigationDelegate methods.

As an alternative approach, if you have the HTML, you could directly replace the values there before loading, e.g. putting some replacement templates there (e.g. $ONE_WEEK). That would be probably much easier to handle.

Grab HTML from WKWebView using evaluateJavascript and then store it in a variable (using Swift)

The value of htmlString is "initial value" because the block is executed after the print statement is getting executed!

If you do print the htmlString inside the block you can see the actual value. You have to do your task inside the completion block. Also the completion block will be executed in the main thread so you need to make sure that you don't block the main thread.

Getting A JavaScript exception occurred When evaluateJavaScript on WKWebView in Objective-C

You treat an arbitrary byte array as a C-string and that might not be null-terminated or contain stuff like ' which you don't want in your JavaScript. So it may as well crash when you try to create your string.

The problematic part is using the %s formatter here:

NSString *strForEvaluate = [NSString stringWithFormat:@"PDFViewerApplication.open(new Uint8Array('%s'));",myArray];

(Also, uint8_t myArray[len]; may cause a stack overflow [ha!] for large data.)

What you want instead is to create a string that represents a JavaScript array of numbers, like [25, 243, 0, 123]. Here's a way that might work (untested):

NSMutableArray<NSString *> *bytes = [NSMutableArray array];
const uint8_t *rawBytes = data.bytes;
for (NSUInteger i = 0; i < data.length; i++) {
[bytes addObject:[NSString stringWithFormat:@"%d", (int)rawBytes[i]]];
}

NSString *javaScriptArray = [bytes componentsJoinedByString:@","];

NSString *strForEvaluate = [NSString stringWithFormat:
@"PDFViewerApplication.open(new Uint8Array([%@]));", javaScriptArray];


Related Topics



Leave a reply



Submit