DZNWebViewController
DZNWebViewController copied to clipboard
Crashes when closing it as a modal
I have a tableview with 3 rows each with a button. When the user clicks on the button it opens up your view controller in a modal. What's weird is that it'll work fine if I open the modal when clicking on the button from the second or third row but when I try to click on the button on the first row it crashes with this error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x126162bf0 of class UINavigationBar was deallocated while key value observers were still registered with it.
<NSKeyValueObservationInfo 0x12607c780> ( <NSKeyValueObservance 0x126016bf0: Observer: 0x126166db0, Key path: hidden, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10023fb18, Property: 0x1261319f0> <NSKeyValueObservance 0x12606c9d0: Observer: 0x126166db0, Key path: center, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10023fb18, Property: 0x1261325a0> <NSKeyValueObservance 0x12606cae0: Observer: 0x126166db0, Key path: alpha, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10023fb18, Property: 0x126132ad0> )'
this is the code i'm using to open the URL:
var webModalNC: UINavigationController!
func showSource(url: String) {
let vc = DZNWebViewController(URL: NSURL(string: url)!)
self.webModalNC = UINavigationController(rootViewController: vc)
vc.supportedWebNavigationTools = DZNWebNavigationTools.All
vc.supportedWebActions = DZNsupportedWebActions.DZNWebActionAll
vc.showLoadingProgress = true
vc.hideBarsWithGestures = true
vc.showPageTitleAndURL = true
let closeButton = UIBarButtonItem(title: "Close", style: .Plain, target: self, action: #selector(HomeViewController.closeWebModal))
vc.navigationItem.rightBarButtonItem = closeButton
self.presentViewController(webModalNC, animated: true, completion: nil)
}
func closeWebModal(){
self.webModalNC.dismissViewControllerAnimated(true, completion: nil)
}
Do i need to remove observers manually before dismissing the modal?
Are these lines called when you dismiss the modal? https://github.com/dzenbot/DZNWebViewController/blob/e1731d96ffaa5cbdce289b5e2948b9f805d92a5c/Source/Classes/DZNWebViewController.m#L858-L862
PS: You shouldn't have to remove observers yourself. It should do it internally.
It crashes when I show the modal. And it happens at random times. Sometimes I can show the modal for a particular link 2 or 3 times before eventually crashing.
Unfortunately Xcode only says it's crashes at the appdelegate class, so I can't give you anymore information:
class AppDelegate: UIResponder, UIApplicationDelegate {
Try adding an exception breakpoint. https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html
Unless you're using a dynamic framework, it should still point out where the exception is being triggered.
It still showing that same line. I tried to debug step by step and it crashes when i present the viewcontroller.
self.presentViewController(webModalNC, animated: true, completion: nil)
You should try weak referencing webModalNC..
Doesn't look like webModalNC
is a property for him. Is it?
I declared webModalNC as weak at the top
'weak var webModalNC: UINavigationController?'
and now it's always nil even after initializing the UINavigationController in showSource().
Does it need to be a var
? Can't you just make it a let
in your implementation scope?
Try sharing a gist of your implementation as it is hard to help by guessing..
var webModalNC: UINavigationController?
func showSource(url: String) {
let vc = DZNWebViewController(URL: NSURL(string: url)!)
self.webModalNC = UINavigationController(rootViewController: vc)
vc.supportedWebNavigationTools = DZNWebNavigationTools.All
vc.supportedWebActions = DZNsupportedWebActions.DZNWebActionAll
vc.showLoadingProgress = true
vc.hideBarsWithGestures = true
vc.showPageTitleAndURL = true
let closeButton = UIBarButtonItem(title: "Close", style: .Plain, target: self, action: #selector(HomeViewController.closeWebModal))
vc.navigationItem.rightBarButtonItem = closeButton
self.presentViewController(webModalNC!, animated: true, completion: nil)
}
func closeWebModal() {
self.webModalNC!.dismissViewControllerAnimated(true, {
self.webModalNC = nil
})
}
not sure if i'm doing this right..
https://gist.github.com/jaybuangan/1a8c020ac2b2b213928bc2afa865be22
i took out all the tableview code.
basically there's a view in a tableviewcell that when clicked, calls a delegate function "showSource" which is defined in this view controller. It's just passing a URL string.
Also crashes for me, same scenario as above. I am showng it as a modal, crashes on exit with debugger pointing to AppDelegate.
i have the same issue while dismiss view controller
Same issue here, crashing on clients but i cant debug because crashlytics dont give enough info for this crash.
Fatal Exception: NSInternalInconsistencyException An instance 0x14dd45670 of class UINavigationBar was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x170229100> ( <NSKeyValueObservance 0x17005fd70: Observer: 0x14dd24c00, Key path: hidden, Options: <New: YES, Old: NO, Prior: NO> Context: 0x1002c76a8, Property: 0x170251ca0> <NSKeyValueObservance 0x1702585d0: Observer: 0x14dd24c00, Key path: center, Options: <New: YES, Old: NO, Prior: NO> Context: 0x1002c76a8, Property: 0x170251f40> <NSKeyValueObservance 0x170252660: Observer: 0x14dd24c00, Key path: alpha, Options: <New: YES, Old: NO, Prior: NO> Context: 0x1002c76a8, Property: 0x170251d00> )