Skip to content

Instantly share code, notes, and snippets.

@Fedenieto90
Last active April 3, 2019 13:45
Show Gist options
  • Save Fedenieto90/989b3cf83161700e179fd30439bcee81 to your computer and use it in GitHub Desktop.
Save Fedenieto90/989b3cf83161700e179fd30439bcee81 to your computer and use it in GitHub Desktop.
import UIKit
import WebKit
class TypeFormVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
loadHtml()
}
func loadHtml() {
// Load JS Script with onSubmit handler
let scriptSource = TypeFormHelper.getTypeFormScript(urlString: urlString, messageHandlerName: formSubmissionHandler)
let script = WKUserScript(source: scriptSource, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
// Create content controller to append JS Script
let contentController = WKUserContentController()
contentController.addUserScript(script)
// Create configuration and webView
let config = WKWebViewConfiguration()
contentController.add(self, name: formSubmissionHandler)
config.userContentController = contentController
let webView = WKWebView(frame: view.bounds, configuration: config)
view = webView
webView.navigationDelegate = self
// Load embedded HTML typeform file
if let embeddedHTML = TypeFormHelper.getTypeFormHtml() {
webView.loadHTMLString(embeddedHTML, baseURL: nil)
}
}
}
extension TypeFormVC: WKNavigationDelegate, WKScriptMessageHandler {
// Handle form submission
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "formSubmitted" {
print("Form Submitted")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment