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

  • 复现步骤 (UIWebView)

    微博:

未命名.gif

美团:

meituan.gif
  • 堆栈信息
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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,864评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,803评论 1 32
  • OC语言基础 1.类与对象 类方法 OC的类方法只有2种:静态方法和实例方法两种 在OC中,只要方法声明在@int...
    奇异果好补阅读 9,794评论 0 11
  • 三公主 你轻轻轻轻唤出静的名字 在异乡的夜半梦里 春风自窗外溜来 耳语了你的秘密 我的眼里荡起粉红色的涟漪 一寸寸...
    三公主的心灵小栈阅读 929评论 2 3
  • 赵朴初,1907年11月5日生于安徽省安庆,2000年5月21日因病在北京逝世,享年93岁。中国社会活动家、宗教领...
    居安_拾遗阅读 1,150评论 0 0