Swift Playgrounds on iPad : load a html page

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.

Answered by DTS Engineer in 709888022

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:

  1. I’m working on an iPad Pro (9.7-inch) running Swift Playgrounds 4.0.2 on iOS 15.3.1.

  2. I created a playground (not an app) by clicking the Playground button at the bottom of My Playgrounds.

  3. In that playground I entered the source code shown below.

  4. 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

Hi,

No one knows if it is possible to create webView on Swift Playgrounds for iPad ?

You may get more responses if you describe the symptoms in more detail than “does not work”. What doesn’t work? Does the code fail to compile? Does the web view in general show up? Or is it perhaps a networking issue? If you ask the web view to load some basic HTML, using the loadHTMLString(_:baseURL:) method, does that show up?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi,

The code does not compile. "File Mach-O can't be generated".

Accepted Answer

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:

  1. I’m working on an iPad Pro (9.7-inch) running Swift Playgrounds 4.0.2 on iOS 15.3.1.

  2. I created a playground (not an app) by clicking the Playground button at the bottom of My Playgrounds.

  3. In that playground I entered the source code shown below.

  4. 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

Hi,

Thanks a lot for your answer. OK, your code works. I was choosing "new app", this is why it was no working. Great !

Swift Playgrounds on iPad : load a html page
 
 
Q