duxfox-- duxfox-- - 1 year ago 106
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

session.disconnect()
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.

EDIT



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

@IBOutlet weak var webView: UIWebView!


the
loadApp()
function is called in 2 places, first in
viewDidLoad()
where it works fine, but then after calling
session.disconnect()
, a tokbox callback delegate method is called:

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


this is the second time/place where
loadApp()
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