Created
February 8, 2024 13:44
-
-
Save nsdevaraj/d467f73b005049887d9fa4f75a2f1be4 to your computer and use it in GitHub Desktop.
Load any webpage
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
// | |
// ViewController.swift | |
// WebEmbed | |
// | |
// Created by Devaraj NS on 08/02/24. | |
// | |
import Cocoa | |
import WebKit | |
class ViewController: NSViewController { | |
let webView: WKWebView = { | |
let prefs = WKWebpagePreferences () | |
prefs.allowsContentJavaScript = true | |
let configuration = WKWebViewConfiguration() | |
configuration.defaultWebpagePreferences = prefs | |
let webView = WKWebView(frame: .zero, | |
configuration: configuration) | |
return webView | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(webView) | |
guard let url = URL(string: "https://www.google.com") else { | |
return | |
} | |
webView.load(URLRequest(url:url)) | |
webView.customUserAgent = "iPad/devOS" | |
// Do any additional setup after loading the view. | |
DispatchQueue.main.asyncAfter(deadline: .now()+5){ | |
self.webView.evaluateJavaScript("document.body.innerHTML") { result, error in | |
guard let html = result as? String, error == nil else { | |
return | |
} | |
print(html) | |
} | |
} | |
} | |
override var representedObject: Any? { | |
didSet { | |
// Update the view, if already loaded. | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment