swift – Share textual content content material on iOS causes the app to crash

swift – Share textual content content material on iOS causes the app to crash


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?

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.