Last active
November 25, 2022 16:20
-
-
Save PaulRosset/cea982e09d09e609679b7e848e397674 to your computer and use it in GitHub Desktop.
Spawning a IOS fullscreen Webview view
This file contains 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 ViewController: UIViewController, WKUIDelegate, WKScriptMessageHandler { | |
var webView: WKWebView! | |
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | |
print("Hello world") | |
} | |
override func loadView() { | |
let wkPreferences = WKPreferences() | |
wkPreferences.javaScriptCanOpenWindowsAutomatically = true | |
let webConfiguration = WKWebViewConfiguration() | |
// Allow the webpage to perform events from Webpage to Mobile App | |
// Known as JSBridge | |
let contentController = WKUserContentController() | |
contentController.add(self, name: "nativeBridge") | |
webConfiguration.userContentController = contentController | |
webConfiguration.preferences = wkPreferences | |
webView = WKWebView(frame: .zero, configuration: webConfiguration) | |
webView.uiDelegate = self | |
view = webView | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// You can also replace by localhost URL! | |
let url = URL(string: "http://...") | |
let myRequest = URLRequest(url: url!) | |
webView.load(myRequest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment