Last active
April 3, 2019 13:45
-
-
Save Fedenieto90/989b3cf83161700e179fd30439bcee81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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