从 tokbox 会话断开连接后加载 webview 始终为零

loading webview is always nil after disconnecting from tokbox session

我正在开发一个使用 webview 实现大部分功能的本机应用程序,它使用 tokbox video 进行本机视频通信。当我最初加载应用程序时,webview 加载正常(它是在界面生成器中创建的)。

当一个人进入视频页面时,我相信 tokbox API 会使 webview 为零。视频结束后,我调用 session.disconnect() 并尝试重新加载我的 webview,但在尝试解包可选时它一直说 found nil。

加载 webview 的代码如下所示:

func loadApp() {
    let htmlFile = Bundle.main.path(forResource: "index", ofType: "html")
    let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
    // this was added in hopes of initializing the webview again
    if webView == nil {
        webView = UIWebView(frame: self.view.bounds)
        self.view = webView
    }
    webView.delegate = self
    webView.frame = view.bounds
    webView.loadHTMLString(html!, baseURL: nil)
}

它在 webView.delegate = self 处一直失败并且实例变量显示即使在 if 条件之后 webView 仍然为 nil。

编辑

通过单击控件并拖动到 UIViewController 来挂接 webview,它看起来像这样:

@IBOutlet weak var webView: UIWebView!

loadApp() 函数在 2 个地方被调用,首先是在 viewDidLoad() 那里工作正常,但是在调用 session.disconnect() 之后,一个 tokbox 回调委托方法被调用:

func sessionDidDisconnect(_ session: OTSession) {
    print("The client disconnected from the OpenTok session.")
    loadApp()
}

这是第二个 time/place,其中 loadApp() 被调用,但失败了。

我认为将 webView 设置为 IBOutlet 时不应将其设置为 nil。尝试让 webView 成为 var webView: UIWebView? class 内部的变量,然后手动实例化它并调整约束并相应地定位它。