개발/swift

Swift wkwebview message handler

lemonade 2020. 10. 17. 15:11
반응형

WKScriptMessageHandler in swift

 

Today, we'll talk about WKScriptMessageHandler. The way of communicate with javascript.

 

Sometimes we want to get message from javascript.

You can catch user event so that handle event properly for example close webView.

You can do this by add userContentController to webView.

First. Add content controller to webView

 


Second, implement WKScriptMessageHandler

 

Note message.name should be same contentController’s name. message.body can be any type like string or json.

On javascript code, call postmessage to send message

 


Finally, Test with this function.


Happy coding :)

 

반응형
그리드형

'개발 > swift' 카테고리의 다른 글

Swift divide result 0 issue  (0) 2020.10.20
Swift property observer  (0) 2020.10.20
Swift codable protocol  (0) 2020.10.17
Swift How to get range of substring?  (0) 2020.10.17
Swift frame vs bounds  (0) 2020.10.14