ios - iPhone 应用程序在 WebThread MPVolumeSlider 上崩溃

标签 ios objective-c iphone uiwebview

问个问题好像很笼统,但是这个bug很烦人,很难修复。

这是我从 Crashlytics 获得的 WebThread 的崩溃日志。

Thread : Crashed: WebThread
0  libobjc.A.dylib                0x0000000193e97bd0 objc_msgSend + 16
1  UIKit                          0x0000000187f65dd8 +[UIViewAnimationState  popAnimationState] + 332
2  MediaPlayer                    0x0000000185953358 -[MPVolumeSlider volumeController:volumeValueDidChange:] + 92
3  MediaPlayer                    0x00000001859c5fc4 -[MPVolumeController updateVolumeValue] + 260
4  MediaPlayer                    0x0000000185952cb0 -[MPVolumeSlider didMoveToSuperview] + 144
5  UIKit                          0x0000000187f2c1dc -[UIView(Hierarchy) _postMovedFromSuperview:] + 484
6  UIKit                          0x0000000187f37cbc -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1764
7  MediaPlayer                    0x0000000185955f54 -[MPVolumeView _createSubviews] + 264
8  MediaPlayer                    0x00000001859549d4 -[MPVolumeView _initWithStyle:] + 240
9  MediaPlayer                    0x0000000185954a60 -[MPVolumeView initWithFrame:style:] + 88
10 WebCore                        0x0000000191ba4684 -[WebMediaSessionHelper initWithCallback:] + 132
11 WebCore                        0x0000000191ba3db8 WebCore::MediaSessionManageriOS::MediaSessionManageriOS() + 96
12 WebCore                        0x0000000191ba3d28 WebCore::MediaSessionManager::sharedManager() + 56
13 WebCore                        0x0000000191ba2890 WebCore::MediaSession::MediaSession(WebCore::MediaSessionClient&) + 44
14 WebCore                        0x00000001916e8604 WebCore::HTMLMediaSession::create(WebCore::MediaSessionClient&) + 36
15 WebCore                        0x00000001916d0fb0 WebCore::HTMLMediaElement::HTMLMediaElement(WebCore::QualifiedName const&, WebCore::Document&, bool) + 1100
16 WebCore                        0x000000019170a2b4 WebCore::HTMLVideoElement::create(WebCore::QualifiedName const&, WebCore::Document&, bool) + 68
17 WebCore                        0x00000001916bdd9c WebCore::videoConstructor(WebCore::QualifiedName const&, WebCore::Document&, WebCore::HTMLFormElement*, bool) + 92

我在开发过程中从未见过这种崩溃(如果我能通过断点和控制台日志捕获它,我会非常高兴),但只有在它上线时才会出现在用户面前。

只能由 crashlytics 报告。

可能的原因;

应用程序使用 MagicalRecord并在启动时在后台从服务器获取数据。 这使用多线程,当 webkit 使用 UIKit 部分并锁定时,另一个主线程似乎要访问它。 所以我试图删除所有 dispatch_sync 并将其更改为 dispatch_async 但是在进行了一些功能调用后崩溃再次发生。

我想知道的是,为什么 WebCore 正在运行,而我从未在 UIWebView 上请求 MPVolumeController。

由于某种原因即使它们可以在后台运行,为什么它会崩溃? 它经常发生,用户提示。

其他人有同样的问题吗?

最佳答案

这个错误发生在 iOS 8 之后。

加载包含 audiovideo 元素的 HTML 的 UIWebView 会随机崩溃。

我是这样修复的:

@interface H5WebKitBugsManager : NSObject

+ (void)fixAllBugs;

@end


#import "H5WebKitBugsManager.h"
#import <objc/runtime.h>

void H5Swizzle(Class c, SEL orig, SEL new)
{
    Method origMethod = class_getInstanceMethod(c, orig);
    Method newMethod = class_getInstanceMethod(c, new);
    if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
        class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, newMethod);
    }
}

@implementation H5WebKitBugsManager

+ (void)fixAllBugs
{
    [self fixBug_MediaPlayerVolumeView];
}

+ (void)fixBug_MediaPlayerVolumeView
{
    CGFloat systemVersion = [UIDevice currentDevice].systemVersion.floatValue;

    if (systemVersion < 8.0f || systemVersion > 9.1) {
    // below ios version 8.0 has no VolumeView
        return;
    }

    Class cls = NSClassFromString(@"WebMediaSessionHelper");
    NSString *allocateVolumeView = @"allocateVolumeView";
    SEL orig = NSSelectorFromString(allocateVolumeView);
    SEL new = @selector(H5WKBMAllocateVolumeView);
    Method newMethod = class_getInstanceMethod(self, new);

    if(class_addMethod(cls, new, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
        H5Swizzle(cls, orig, new);
    }
}

- (void)H5WKBMAllocateVolumeView
{
    // WebKit's MediaSessionManageriOS is a singleton,in MediaSessionManageriOS.m. svn version181,859.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
            // must be dispatch in background thread
            [self H5WKBMAllocateVolumeView];
        });
    });
}

@end

关于ios - iPhone 应用程序在 WebThread MPVolumeSlider 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29523902/

相关文章:

ios - 如何记录在 Xcode 中显示为运行时问题的警告?

iphone 开发 : how can I inherit from more than one class?

ios - 为什么当我从我转至的 View Controller 返回时,我的 UITableView 的格式完全出错?

iPhone我如何让两条连接线跟随到位置?

iPhone - 无法安装应用程序

ios - 如何使用 SwiftyJSON 在 Swift 中将字符串转换为 JSON 数组?

ios - UIView 不需要它的 UIViewController 吗?

ios - 与新应用版本一起提交的新 iOS 应用内购买是否会在以前的应用版本中可用?

ios - NSRegularExpression 在匹配字符串中搜索相同模式

ios - 编辑按钮未显示在 UITabBarController 的 MoreNavigationController 中