duxfox-- duxfox-- - 1 year ago 113
iOS Question

loading webview is always nil after disconnecting from tokbox session

I'm working on a native app that uses a webview for most of the functionality, and it uses tokbox video for native video communication. When I initially load the app, the webview loads fine (its created in interface builder).

When a person goes to the video page, I believe the tokbox API makes the webview nil. Once the video is over, I call

and try to reload my webview, but it keeps saying found nil while trying to unwrap optional.

The code that loads the webview looks like this:

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)

it keeps failing at
webView.delegate = self
and the instance variables show that webView is still nil even after the if condition.


the webview was hooked by clicking control and dragging to the UIViewController and it looks like this:

@IBOutlet weak var webView: UIWebView!

function is called in 2 places, first in
where it works fine, but then after calling
, a tokbox callback delegate method is called:

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

this is the second time/place where
is called, and where it fails.

Answer Source

I don't think webView should be set to nil when you have it setup as a IBOutlet. Try just having webView be var webView: UIWebView? a variable inside of the class and then instantiate it manually and adjust the constraints and position it accordingly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download