0

I have a button inside a web view.
When the button is clicked, I would like to add the item to my native code array.
Then, when it's pressed again, I would like to remove the item in the array.

My question is: What's missing in the logic? Neither userContentController or decidePolicyForNavigationAction is called when the button is pressed.

The JavaScript Code

<script type = "text/javascript">
 function someFunction(id) {
  if(id = true) {
    document.getELementById("someid").innerText = " some title state 1"
  } else {
     document.getELementById(someid).innerText = " some title state 2"
  }
 }
<script>

The Html Button

<button class=someclass id="someid" onclick= "Some.listCallBack()">Add List</button>

The Objective C code

TableViewCell.h
@interface TableViewCell : UITableViewCell <WKNavigationDelegate, WKScriptMessageHandler>
@property (nonatomic,strong) IBOutlet WKWebView *webView;
 
TableViewCell.m

- (void)awakeFromNib {
 [[[self.webView configuration] userContentController]addScriptMessageHandler:selfname:@"someid"];
}
​
- (void)userContentController:(nonnull WKUserContentController *)userContentController 
didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
// i will handle event here 

}
2
  • What’s Some.listCallback()? Whatever is missing (or isn’t missing) seems to be in there. Commented Mar 29, 2020 at 1:12
  • its a javascript callcack Commented Mar 29, 2020 at 3:28

1 Answer 1

1

Since the actual click handler seems to be omitted from the question, it’s hard to tell what the exact problem is. I might therefore be stating the obvious, but here we go.

The handler (the one registered with -addScriptMessageHandler:name:) is exposed to JavaScript as a property on webkit.messageHandlers. From there, the interprocess communication looks more or less like messaging another window, except instead of window.postMessage() we use webkit.messageHandlers.handlerID.postMessage(). In other words,

function clickHandler() {
    webkit.messageHandlers.someid.postMessage("The message we’d like to pass to our WKScriptMessageHandler");
}

should do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.