iOS-阅读器常年崩溃问题记录

在APP上线以来友盟上一直检测到一个崩溃,但是无法找到准确的复现路径

友盟上的错误摘要

The number of view controllers provided (3) doesn't match the number required (1) for the requested transition (null)
The number of view controllers provided (0) doesn't match the number required (2) for the requested transition
(null)

完全看不出什么,网上找到各种解决方法都没有什么用,没有找到准确的错误复现路径。

后来查资料才知道这是pageview这个系统控件的坑,这个玩意有时候在翻页的时候会自己主动翻两页,而且是不在回调里面的,而且里面的缓存机制也很混乱。

所以,修改系统方法呗,在阅读器的基础控制器里面加上

#pragma mark - UIPageViewController Fix
@implementation UIPageViewController (fix)
+ (void)hyFix {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method dstMet = class_getInstanceMethod([UIPageViewController class], NSSelectorFromString(@"_setViewControllers:withCurlOfType:fromLocation:direction:animated:notifyDelegate:completion:"));
        Method srcMet = class_getInstanceMethod(self, @selector(hyFix_setViewControllers:withCurlOfType:fromLocation:direction:animated:notifyDelegate:completion:));
        method_exchangeImplementations(dstMet, srcMet);
    });
}

- (void)hyFix_setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers
                  withCurlOfType:(UIPageViewControllerTransitionStyle)type
                    fromLocation:(CGPoint)location
                       direction:(UIPageViewControllerNavigationDirection)direction
                        animated:(BOOL)animated
                  notifyDelegate:(BOOL)notifyDelegate
                      completion:(void (^ __nullable)(BOOL finished))completion {
    if (!viewControllers.count) return;
    
    [self hyFix_setViewControllers:viewControllers withCurlOfType:type fromLocation:location direction:direction animated:animated notifyDelegate:notifyDelegate completion:completion];
}

@end

调用

+ (void)initialize {
    [UIPageViewController hyFix];
}

完美!!

检测后新版本再也没有这个崩溃了。

原文地址:https://www.cnblogs.com/qiyiyifan/p/12955352.html