DAKeyboardControl
DAKeyboardControl copied to clipboard
App dies in [self swizzled_addSubview:subview];
Hi there,
I was trying to use DAKeyboardControl, but my App crashes after the second load of DAKeyboardControl. To show what I mean, I modified the Example project. You can find it here: https://github.com/appcominteractive/DAKeyboardControl/tree/swizzeledError
To see the crash: push the new push button, go back, and press the push button again. Tested with Xcode 4.5.2 on iPhone 5.0 and iPhone 6.0 Simulator.
Regards, Christian
+1 Seeing this come in on my live app's crash logs quite often.
5352d1ff has resolved this issue.
Same here, for me it happened when I was presenting a modal view controller from a navigation controller. And the view was of class: UINavigationTransitionView and subview to be added was of class: UIViewControllerWrapperView
Per @kylef, 5352d1ff fixed @appcominteractive 's example. Can one of you guys provide a reproducible demo and post it here?
Using 2.1.0 and getting crashes after loading DAKeyboardControl the second time.
Getting messages being sent to deallocated instances.
*** -[UIView inputKeyboardWillChangeFrame:]: message sent to deallocated instance 0x1f3a1750
Hacky fix by calling removeKeyboardControl
just before the view disappears and only if the view controller has been removed from the navigation controller's stack (ie. popped off the stack).
Reproducible demo of the crash: https://github.com/emersonmalca/DAKeyboardControl
@emersonmalca, you need to remove keyboard control when your view goes away. This isn't an issue with DAKeyboardControl.
https://github.com/danielamitay/DAKeyboardControl#remove-the-nsnotification-observer-at-the-end-of-a-vcs-life-convenience-method
diff --git a/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m b/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m
index 3042579..8078d6a 100644
--- a/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m
+++ b/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m
@@ -97,4 +97,8 @@
[self dismissViewControllerAnimated:YES completion:NULL];
}
+- (void)dealloc {
+ [[self view] removeKeyboardControl];
+}
+
@end
thanks @kylef, so the comment in the demo in the action handler block is no valid anymore then, since calling that method is not optional:
/* Try not to call "self" inside this block (retain cycle). But if you do, make sure to remove DAKeyboardControl when you are done with the view controller by calling: [self.view removeKeyboardControl]; */
Yeah I should make it more explicit that DAKeyboardControl
really should be balanced by calling -(void) removeKeyboardControl
. I would certainly like to find a hook that can do this automatically, but haven't found a good way yet.
Of course, at the same time I would like to hunt down exactly why not removing DAKeyboardControl
is causing issues with the method swizzling, as I currently see no obvious reason why... =(
But yes, balancing the calls will fix this issue. Thanks @emersonmalca for the great demo, and @kylef again.
Was also confused as that comment makes it look optional (only required if you have a retain cycle?) Figured that wasn't the case. :)
Any updated here? We really want to use this control but it crashes in IOS7
same for me. I got a few crashes on my live app, and I have managed to reproduce this on app screens that doesn't event need DAKeyboardControl ... very useful control anyway!
Same thing here. Also see a lot of crash reports related to that issue. Stacktrace like this:
1 libobjc.A.dylib (anonymous namespace)::AutoreleasePoolPage::autoreleaseSlow(objc_object*) + 430
2 libsystem_malloc.dylib szone_malloc_should_clear + 2766
3 libobjc.A.dylib objc_object::rootAutorelease2() + 52
4 UIKit -[UIView(Hierarchy) subviews] + 78
5 UIKit -[UIView(Internal) _recursiveNotifyInteractionTintColorDidChangeForReasons:] + 188
…
12 MYAPP -[UIView(DAKeyboardControl) swizzled_addSubview:] (DAKeyboardControl.m:577)
13 UIKit -[UITableViewCellLayoutManager layoutSubviewsOfCell:] + 1640
...
22 UIKit __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 448
Thanks
+- (void)dealloc {
- [[self view] removeKeyboardControl]; +}
Worked for me :)
It is happening for ios 7 and ios 8.
I have tried with this also. It did not work out. App is getting crashed when I receive push notification and I have re intialize the root view and go the the main class using push view. Sometimes it is causing crash
(void)dealloc {
[[self view] removeKeyboardControl]; }
is there any way that swizzled_addSubview will not be called it instance is deallocated
We see this crash in our application as well. We do indeed call removeKeyboardControl
and the crash still happens.
Fatal Exception: NSInvalidArgumentException Can't add self as subview
I am suspecting that this crash is not related to DAKeyboardControl
but rather an unrelated bug. I think the only reason it is showing up this way is because addSubview
is swizzled. I have seen some weirdness with UINavigationController
where the same view is presented twice. I think it is an iOS bug because I have seen it in many applications. I believe this is the crash people are seeing.
Of course I could be wrong, but this is what our research came up with.
@joshuafeldman your suspicion is correct in my case. The crash is indirectly triggered by another bug in a different part of the code. Basically, my view shouldn't have been deallocated, and calling removeKeyboardControl on this view crashed the app.
Hi My app is crashing for the same issue. I have write [self.view removeKeyboardControl] but still getting crash. Kindly help.
Finally figured this one out, and it was really difficult. It seems like some of the views in the Apple keyboard window don't actually respond to UIResponder
properties and methods (no idea how this is possible, Apple does some crazy internal stuff). Pretty sure it is an Apple bug.
What we did to work around the bug is create a category on UIResponder
and swizzled the properties / methods that get accessed and wrapped the call to the original method inside of a respondsToSelector:
and it seems to have worked nicely.
@joshuafeldman i can't understand completely your last comment. can you make conclusion for this issue ? Finally can you give us a sample about you solution category on UIResponder ? but i think my problem a little different this is my stack trace in Fabric. this method have called over 500 time !!! http://uupload.ir/files/56g_screen_shot_2015-10-03_at_3.25.04_pm.png
is there any respond to my question ?
@mfarhand we ended up removing DAKeyboardControl
since iOS 7+ supports the pan gesture to dismiss the keyboard without requiring this library.
But while we had it implemented we used something like this to prevent that bug where a weird subclass of UIView
actually doesn't respond to the isFirstResponder
method.
-swizzleInstanceMethod:toMethod:
is a category method on NSObject
that I didn't include in the snippet below.
@implementation UIResponder (KeyboardViewResponderFix)
+ (void)load
{
SEL originalSelector = @selector(isFirstResponder);
SEL swizzledSelector = @selector(swizzled_isFirstResponder);
[self swizzleInstanceMethod:swizzledSelector toMethod:originalSelector];
}
- (BOOL)swizzled_isFirstResponder
{
if ( [self respondsToSelector:@selector(swizzled_isFirstResponder)] ) {
return [self swizzled_isFirstResponder];
}
return NO;
}
@end
@joshuafeldman thanks for your reply i have 2 question :
- i can't understand your mean about : -swizzleInstanceMethod:toMethod: is a category method on NSObject that I didn't include in the snippet below. is it enough if create a category from NSObject & swizling like your code or i need extra code ?
what is the best solution for handle without this library?(we we to set out table view on top of keyboard frame & dismiss it with panning gesture
@mfarhand yes if you have your own method to swizzle just use it there. If your applications supports iOS 7+ I highly suggest looking at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/keyboardDismissMode
@joshuafeldman tnx for your help , i will try it and notify you about this issue.