简体   繁体   中英

Blank white screen with WKWebView

I'm trying to create a simple app that allows a user to login with their Twitch account using OAuth. I created a WKWebView to load the request but I'm getting a blank white screen when I start the app:

import UIKit
import OAuthSwift
import SafariServices
import WebKit

class LoginViewController: UIViewController, WKNavigationDelegate {

    // MARK: Variables
    // oauth swift object (retain)
    var oauthswift: OAuthSwift?
    var targetURL = URL(string: "https://id.twitch.tv/oauth2/validate")
    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    // MARK: Actions
    @IBAction func pressedLoginButton(_ sender: Any) {

        // create an instance and retain it
        let oauthswift = OAuth2Swift(
            consumerKey:    "xxxxx",
            consumerSecret: "xxxxx",
            authorizeUrl: "https://id.twitch.tv/oauth2/validate",
            responseType: "code"
        )

        self.oauthswift = oauthswift
        oauthswift.accessTokenBasicAuthentification = true

        //let codeVerifier = base64url("abcd...")
        //let codeChallenge = codeChallenge(for: codeVerifier)

        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
        let urlRequest:URLRequest = URLRequest(url: self.targetURL!)
        webView.load(urlRequest)

        getURLHandler()

        let handle = oauthswift.authorize(
            withCallbackURL: URL(string: "http://localhost")!,
            scope: "user:read:email", state:"TWITCH") { result in
                switch result {
                case .success(let (credential, response, parameters)):
                    print(credential.oauthToken)
                // Do your request
                case .failure(let error):
                    print(error.localizedDescription)
                }
        }
    }

    func handle(_ url: NSURL) {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView

        //let req = URLRequest(url: self.targetURL!)
        webView.load(URLRequest(url: self.targetURL!))
        webView.allowsBackForwardNavigationGestures = true
    }
}

The white screen goes away if I delete the loadView function, but I thought that was required for WKWebView. Any idea how to fix this?

Thanks!!

Since you're running this on the simulator, the network security protocol is heightened.

Add this to your info.plist , or alternatively run on an actual device to see your results.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Hope this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM