UIWebFileUploadPanel网页中选取上传图片野指针crash问题

  • 复现步骤 (UIWebView)

    微博:

美团:

  • 堆栈信息
Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: 0x00000000 at 0x000000098363da90
Crashed Thread:  0
 
Thread 0 name:  Dispatch queue: com.apple.main-thread
 
Thread 0 Crashed:
0   libobjc.A.dylib                 objc_msgSend + 28
1   UIKit                           +[UIViewController _viewControllerForFullScreenPresentationFromView:] + 40
2   UIKit                           -[UIWebFileUploadPanel _presentFullscreenViewController:animated:] + 88
3   UIKit                           __51-[UIDocumentMenuViewController _dismissWithOption:]_block_invoke.220 + 92
4   UIKit                           -[UIPresentationController transitionDidFinish:] + 1324
5   UIKit                           __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 188
6   UIKit                           -[_UIViewControllerTransitionContext completeTransition:] + 116
7   UIKit                           -[UITransitionView notifyDidCompleteTransition:] + 252
8   UIKit                           -[UITransitionView _didCompleteTransition:] + 1240
9   UIKit                           -[UITransitionView _transitionDidStop:finished:] + 124
10  UIKit                           -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 312
11  UIKit                           -[UIViewAnimationState animationDidStop:finished:] + 160
12  QuartzCore                      CA::Layer::run_animation_callbacks(void*) + 260
13  libdispatch.dylib               _dispatch_client_callout + 16
14  libdispatch.dylib               _dispatch_main_queue_callback_4CF + 1000
15  CoreFoundation                  __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
16  CoreFoundation                  __CFRunLoopRun + 1660
17  CoreFoundation                  CFRunLoopRunSpecific + 444
18  GraphicsServices                GSEventRunModal + 180
19  UIKit                           -[UIApplication _run] + 684
20  UIKit                           UIApplicationMain + 208
21  moma                            main (main.m:15)
22  libdyld.dylib                   start + 4
复制代码
  • 原因

    在一个可以上传图片照片的wenView页面中唤起UIWebFileUploadPanel的同时将web页面pop掉,这时UIWebFileUploadPanel对象中持有的documentView(documentView是一个assign类型)就会变为野指针,再去访问这个野指针就会crash。

  • 解决方案

    hook住UIWebFileUploadPanel中documentView的set方法,用一个weak对象持有它,理论上当web页面消失之后这个weak对象会自动被置为nil。通过这个原理来判断documentView对象是否已经被销毁,如果已经被销毁则不再做后续操作,避免野指针crash的发生。

  • Code

#import <objc/runtime.h>

__weak id privateDocumentView = nil;

@implementation NSObject (DEFFixWebFileUploadPannelCrash)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *fileUploadPanel = @"UIWebFileUploadPanel";
        Class fileUploadCls = NSClassFromString(fileUploadPanel);
        SEL panelSetterSwizzedSel = NSSelectorFromString(@"setDocumentView:");
        SEL panelSwizzedSel = NSSelectorFromString(@"_presentFullscreenViewController:animated:");
        
        SEL overrideSelector = @selector(p_presentFullscreenViewController:animated:);
        SEL orerrideSetDocumentSel = @selector(p_setDocumentView:);
        
        SwizzledMethod(fileUploadCls, self, panelSwizzedSel, overrideSelector);
        SwizzledMethod(fileUploadCls, self, panelSetterSwizzedSel, orerrideSetDocumentSel);
    });
}

- (void)p_presentFullscreenViewController:(id)controller animated:(BOOL)animated
{
    if (!privateDocumentView) {
        return;
    }
    [self p_presentFullscreenViewController:controller animated:animated];
}

- (void)p_setDocumentView:(id)documentView
{
    privateDocumentView = documentView;
    [self p_setDocumentView:documentView];
}

@end
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值