david david - 24 days ago 25
iOS Question

Swift: UIWebView in PlayGround not visible

My Code:

import UIKit
import WebKit
//import PlaygroundSupport
import XCPlayground

let url = URL(string: "https://github.com")!
let request = URLRequest(url: url)

let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 1200.0, height: 1200.0))
webView.loadRequest(request)


But I see empty white frame, without content from github. I have no idea :-(

Thanks!

Answer

You need to tell the Playground to make the live view the webview as below:

    import UIKit
    import WebKit
    import PlaygroundSupport

    let url = URL(string: "https://github.com")!
    let request = URLRequest(url: url)

    let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 1200.0, height: 1200.0))
    webView.loadRequest(request)
    PlaygroundPage.current.liveView = webView

Also UIWebview is old (since iOS 2) and super buggy. Consider WKWebView (new in iOS 8) unless you really require UIWebView.

    import UIKit
    import WebKit
    import PlaygroundSupport

    let url = URL(string: "https://github.com")!
    let request = URLRequest(url: url)

    let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1200.0, height: 1200.0))
    webView.load(request)
    PlaygroundPage.current.liveView = webView
Comments