Hi,
I have installed Swift Playgrounds on my iPad and I would like to create an app that displays a webpage (html). I tried several code found on the web, like this below, but does not work.
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
Is it possible to create an app on Swift Playgrounds for iPad that lauch a html page in a webView ?
Thanks for your help.
The code does not compile.
Oh, wow, I’m glad I asked because that wasn’t the answer I was expecting.
I just dusted off Swift Playgrounds [1] on my iPad and tried this out:
-
I’m working on an iPad Pro (9.7-inch) running Swift Playgrounds 4.0.2 on iOS 15.3.1.
-
I created a playground (not an app) by clicking the Playground button at the bottom of My Playgrounds.
-
In that playground I entered the source code shown below.
-
I ran it. It showed the web view just fine (screen shot below).
Please try this out and confirm that it works for you. Presuming it does, you can then compare my steps to your steps to see where things are going wrong.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] I gotta say, the latest version is really nice even on my iPad, which is almost 6 years old.
import UIKit
import WebKit
import PlaygroundSupport
class WebViewController: UIViewController {
override func loadView() {
let config = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: config)
self.view = webView
}
var webView: WKWebView { self.view as! WKWebView }
override func viewDidLoad() {
super.viewDidLoad()
self.webView.loadHTMLString("<h1>Hello Cruel World!</h1>", baseURL: nil)
}
}
let vc = WebViewController()
PlaygroundPage.current.liveView = vc