2

I want to convert a UIWebView library to use WkWebview. The remaining piece is switching out JSContext because the valueForKeyPath doesn't work anymore. So how do I rewrite something like the following to use WKScriptMessage as the other SO link suggests? (swift or ObjC answer is fine) How to get JSContext from WKWebView

JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];


ctx[@"contentPasteCallback"] = ^(JSValue *msg) {
    __weak typeof(weakSelf) StrongSelf = weakSelf;
    StrongSelf.editorPaste = YES;
};

[ctx evaluateScript:@"document.getElementById('zss_editor_content').addEventListener('paste', contentPasteCallback, false);"];
MadeByDouglas
  • 2,509
  • 1
  • 18
  • 22

2 Answers2

2

I have converted UIWebView to WKWebView for Editor. I have created fork from this Github Link. The link to my demo can be found here.

Drashti Javiya
  • 519
  • 1
  • 3
  • 14
  • thats awesome! unfortunately doesn't answer my exact problem. The library you forked doesn't seem to have this component so its still not clear to me what the fix is. Also its hard to just use either of these swift libraries because they have code written in swift 3 I don't want to migrate everything to swift 5 – MadeByDouglas Oct 23 '19 at 18:45
1

Ok I figured it out. See the PR https://github.com/nnhubbard/ZSSRichTextEditor/pull/243

Basically you inject javascript to start the listeners. The key here is to pass the function which calls webkit using postMessage and use the same name, in my case 'jsm' as what was setup when you create the WKUserContentController object

    NSString *pasteListener = @"document.getElementById('zss_editor_content').addEventListener('paste', function() {window.webkit.messageHandlers.jsm.postMessage('paste');});";

    [self.editorView evaluateJavaScript:pasteListener completionHandler:^(NSString *result, NSError *error) {
        if (error != NULL) {
            NSLog(@"%@", error);
        }
    }];

and then you listen for the response in the userContentController: didReceiveScript delegate method from WKScriptMessageHandler

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {

    NSString *messageString = (NSString *)message.body;
    if ([messageString isEqualToString:@"paste"]) {
        self.editorPaste = YES;
    }
MadeByDouglas
  • 2,509
  • 1
  • 18
  • 22