Closed rlaferla closed 7 years ago
This isn't the first time I've gotten this request. It could probably be done. How easy the conversion would be, I'm not quite sure. Maybe this will be the next big breaking change?
When I first made it, we were still supporting iOS 7, which is why it was made with UIWebView.
Given that your component is in Swift, I think it's a natural and worthy next step.
Started working on this a little bit, but it turns out you can't synchronously get responses from JS with WKWebView. This isn't really a big deal in making the changes on my side, but it does make usability a bit awkward. Things as simple as editingEnabled
or getHTML()
, getText()
will need a completion block :confused:
For things like editingEnabled
, I could try to keep a variable updated when changes are made, but no actual interactions with the web view are going to be synchronous like they used to be. Although maybe it's not a huge issue.
This category on WKWebView should help albeit in ObjC:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
{
__block NSString *resultString = nil;
__block BOOL finished = NO;
[self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
}
finished = YES;
}];
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return resultString;
}
Here's an untested Swift version:
extension WKWebView {
func stringByEvaluatingJavaScriptFromString(script: String) -> String {
var resultString: String? = nil
var finished: Bool = false
self.evaluateJavaScript(script, completionHandler: {(result: AnyObject, error: NSError) -> Void in
if error == nil {
if result != nil {
resultString = "\(result)"
}
}
else {
NSLog("evaluateJavaScript error : %@", error.localizedDescription)
}
finished = true
})
while !finished {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture())
}
return resultString!
}
}
Yeah I found similar approaches when I was looking for solutions. There are a few things I don't like about it. If people want to lock up the current thread while it waits for the WebKit process to return with a result, then they are free to do it, but I don't think it belongs inside the library itself.
It might be a little awkward to require an asynchronous API, but I think it's the preferable option, to be honest.
I'm trying to write my own rich text editor by WKWebView. And I'd like to be able to customize both inputView and inputAccessoryView at the same time. Tried to follow what this library does but doesn't seem to work. Can anyone help? Thanks
I'm trying to write my own rich text editor by WKWebView. And I'd like to be able to customize both inputView and inputAccessoryView at the same time. Tried to follow what this library does but doesn't seem to work. Can anyone help? Thanks
That's not really what this thread is about. I'd be glad to see if I can help out in another thread, but this issue is about changing the underlying implementation of RichEditorView from UIWebView to WKWebView.
I'm going to close this as wontfix
, because refactoring this would basically be a rewrite, and it would merit being its own repo. Thanks for all the interest, everyone 👍
At some point Apple will drop support for UIWebView. Are there any reasons why this component can't use WKWebView currently?