I’ve an app that permits a consumer to share textual content from inside a webwiew.
The next code works on all gadgets I may check, however I’ve customers that report {that a} click on on the button within the webview closes the appliance consitently.
Javascript click on handler:
perform onClick() {
let textual content="..."
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.shareText) {
window.webkit.messageHandlers.shareText.postMessage(textual content)
}
}
Swift ViewController:
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler {
var webView : WKWebView!
func WKWebViewInit(){
let configuration = WKWebViewConfiguration()
let preferences = WKPreferences()
preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
configuration.preferences = preferences
let contentController = WKUserContentController();
contentController.add(self, identify: "shareText")
configuration.userContentController = contentController;
webView = WKWebView(body: self.view.body, configuration: configuration)
webView.navigationDelegate = self
webView.uiDelegate = self
let url = Bundle.fundamental.url(forResource: "index", withExtension: "html", subdirectory: "html") {
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
self.view.addSubview(webView)
self.view.sendSubviewToBack(webView)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if (message.identify == "shareText") {
let objects = [message.body]
let ac = UIActivityViewController(activityItems: objects, applicationActivities: nil)
current(ac, animated: true)
}
}
}
The place is that this code undecided, which means, the place is it topic to make the appliance crash?
Or perhaps the reporting consumer’s gadget has a extra basic drawback?