Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_YYTextKeyboardViewFrameObserver崩溃 #978

Open
zhazhaYang opened this issue Nov 8, 2022 · 7 comments
Open

_YYTextKeyboardViewFrameObserver崩溃 #978

zhazhaYang opened this issue Nov 8, 2022 · 7 comments

Comments

@zhazhaYang
Copy link

zhazhaYang commented Nov 8, 2022

__unsafe_unretained UIView *_keyboardView;

推测:_keyboardView在_YYTextKeyboardViewFrameObserver对象dealloc前被释放掉了,导致访问了野指针。很可能两者的关联处理有关系。
场景:并未使用YY相关组件,而是使用了UITextField,调起了键盘。YYTextKeyboardManager单例收到了UIKeyboardDidChangeFrameNotification通知,最后导致的崩溃。

@zhazhaYang
Copy link
Author

zhazhaYang commented Nov 8, 2022

查了issues似乎有两个相关问题,麻烦看看呢。
#842
#924

@yasinn
Copy link

yasinn commented Nov 22, 2022

查了issues似乎有两个相关问题,麻烦看看呢。 #842 #924

这个有解决方法吗?

@zhazhaYang
Copy link
Author

查了issues似乎有两个相关问题,麻烦看看呢。 #842 #924

这个有解决方法吗?

我用分类覆盖了他有问题的地方

@duoshankui
Copy link

哈喽,能分享一下你的处理方案吗,我也遇到这个问题了

@zhazhaYang
Copy link
Author

哈喽,能分享一下你的处理方案吗,我也遇到这个问题了

@implementation YYTextKeyboardManager (Helper)

  • (void)_initFrameObserver {
    UIView *keyboardView = self.keyboardView;
    if (!keyboardView) {
    return;
    }
    UIView *lastKeyboard = [[self weakPropertyMap] objectForKey:@"keyboardView"];
    if(lastKeyboard == keyboardView) {
    return;
    }
    [[self weakPropertyMap] setObject:keyboardView forKey:@"keyboardView"];
    __weak typeof(self) weakSelf = self;
    __weak UIView *weakKeyboardView = keyboardView;
    SEL sel = NSSelectorFromString(@"_keyboardFrameChanged:");
    void(^block)(id x) = ^(id x) {
    if(weakKeyboardView && [weakSelf respondsToSelector:sel]) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [weakSelf performSelector:sel withObject:weakKeyboardView];
    #pragma clang diagnostic pop
    }
    };
    [RACObserve(keyboardView, frame) subscribeNext:block];
    [RACObserve(keyboardView, center) subscribeNext:block];
    [RACObserve(keyboardView, bounds) subscribeNext:block];
    [RACObserve(keyboardView, transform) subscribeNext:block];
    }

@duoshankui
Copy link

哈喽,能分享一下你的处理方案吗,我也遇到这个问题了

@implementation YYTextKeyboardManager (Helper)

  • (void)_initFrameObserver {
    UIView *keyboardView = self.keyboardView;
    if (!keyboardView) {
    return;
    }
    UIView *lastKeyboard = [[self weakPropertyMap] objectForKey:@"keyboardView"];
    if(lastKeyboard == keyboardView) {
    return;
    }
    [[self weakPropertyMap] setObject:keyboardView forKey:@"keyboardView"];
    __weak typeof(self) weakSelf = self;
    __weak UIView *weakKeyboardView = keyboardView;
    SEL sel = NSSelectorFromString(@"_keyboardFrameChanged:");
    void(^block)(id x) = ^(id x) {
    if(weakKeyboardView && [weakSelf respondsToSelector:sel]) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [weakSelf performSelector:sel withObject:weakKeyboardView];
    #pragma clang diagnostic pop
    }
    };
    [RACObserve(keyboardView, frame) subscribeNext:block];
    [RACObserve(keyboardView, center) subscribeNext:block];
    [RACObserve(keyboardView, bounds) subscribeNext:block];
    [RACObserve(keyboardView, transform) subscribeNext:block];
    }

哈哈,好惭愧,我是这么解决的,
@implementation YYTextKeyboardManager (FixCrash)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

  • (instancetype)defaultManager {
    static YYTextKeyboardManager *mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    if (![UIApplication isAppExtension]) {
    mgr = [[self alloc] _initWithFixCrash];
    }
    });
    return mgr;
    }
    #pragma clang diagnostic pop
  • (instancetype)_initWithFixCrash {
    self = [super init];
    return self;
    }

@EnD

@460467069
Copy link

哈喽,能分享一下你的处理方案吗,我也遇到这个问题了

@implementation YYTextKeyboardManager (Helper)

  • (void)_initFrameObserver {
    UIView *keyboardView = self.keyboardView;
    if (!keyboardView) {
    return;
    }
    UIView *lastKeyboard = [[self weakPropertyMap] objectForKey:@"keyboardView"];
    if(lastKeyboard == keyboardView) {
    return;
    }
    [[self weakPropertyMap] setObject:keyboardView forKey:@"keyboardView"];
    __weak typeof(self) weakSelf = self;
    __weak UIView *weakKeyboardView = keyboardView;
    SEL sel = NSSelectorFromString(@"_keyboardFrameChanged:");
    void(^block)(id x) = ^(id x) {
    if(weakKeyboardView && [weakSelf respondsToSelector:sel]) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [weakSelf performSelector:sel withObject:weakKeyboardView];
    #pragma clang diagnostic pop
    }
    };
    [RACObserve(keyboardView, frame) subscribeNext:block];
    [RACObserve(keyboardView, center) subscribeNext:block];
    [RACObserve(keyboardView, bounds) subscribeNext:block];
    [RACObserve(keyboardView, transform) subscribeNext:block];
    }

weakPropertyMap 这个实现方法是啥呀?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants