WF _WebFilterIsActive returning NO并且Can't Add Self as Subview 崩溃解决办

3,230 阅读3分钟

错误提示如下:

 WF: _userSettingsForUser mobile: {
filterBlacklist =     (
);
filterWhitelist =     (
);
restrictWeb = 1;
useContentFilter = 0;
useContentFilterOverrides = 0;
whitelistEnabled = 0;
}

WF: _WebFilterIsActive returning: NO
WF: _userSettingsForUser mobile: {
filterBlacklist =     (
);
filterWhitelist =     (
);
restrictWeb = 1;
useContentFilter = 0;
useContentFilterOverrides = 0;
whitelistEnabled = 0;
}
 WF: _WebFilterIsActive returning: NO
 [Bugly]  Trapped uncaught exception  'NSInvalidArgumentException', reason: 'Can't add self as subview' `

这段可以跳过,是我问题出现的方式: (程序启动后,快速的点击屏幕,因为有一个广告页,会跳转广告页,但是在跳转广告页之前,我会先切换程序的根控制器,在延时1秒跳转广告详情页,这时会显示程序的首页,首页我做了缓存处理,正好点击的位置是首页的滚动广告,这时又跳转一个广告页,最终的结果是只跳转了时候首页的广告,但是点击返回的时候,无法返回,并崩溃,报如上错误!)

现在说一下这个排查思路:

  1. 拿这些打印去找度娘,首先我拿的是WF: _WebFilterIsActive returning: NO ,(为啥拿这段,因为这个打印我没见过,比较特殊,我一般都是拿那些不常见的打印去查,你也可以这样),搜索出来的结果其中一个链接,问题的矛头都指向了UIWebview,
    AA4A486A-4573-4241-801E-777D6FD7805F.png
    解决办法也很简单,就是把UIWebView更换为WKWebView,我当然按照文档的指示,换好之后,继续测试,发现问题还是存在
  2. 既然问题还存在,说明不是UIWebView的问题,我把关注点放在 'Can't add self as subview'这句话上,查下来的结果认为是连续push两个控制器导致,这和我的问题是吻合的,直接按照文档的指示开始操作,链接 我其实也看其他人写的了,对于我来说改起来比较麻烦,这个是最简单的办法,运用了运行时!

现在问题完美的解决了,我也把自己的代码分享出来,希望帮助遇到同样问题的你! 下面贴上代码!

  1. 首先创建一个分类(分类名为UINavigationController+SafePushing,可以随便起哈)

  2. 复制下面的代码到.m文件

  3. 创建UINavigationController的时候引入分类 大功告成!

    #import "UINavigationController+SafePushing.h" #import <objc/runtime.h>

    /// This char is used to add storage for the isPushingViewController property. static char const * const ObjectTagKey = "ObjectTag";

    @interface UINavigationController () @property (readwrite,getter = isViewTransitionInProgress) BOOL viewTransitionInProgress; @end

    @implementation UINavigationController (SafePushing)

    • (void)setViewTransitionInProgress:(BOOL)property { NSNumber *number = [NSNumber numberWithBool:property]; objc_setAssociatedObject(self, ObjectTagKey, number , OBJC_ASSOCIATION_RETAIN); }

    • (BOOL)isViewTransitionInProgress { NSNumber *number = objc_getAssociatedObject(self, ObjectTagKey); return [number boolValue]; }

    #pragma mark - Intercept Pop, Push, PopToRootVC /// @name Intercept Pop, Push, PopToRootVC

    • (NSArray *)safePopToRootViewControllerAnimated:(BOOL)animated { if (self.viewTransitionInProgress) return nil; if (animated) { self.viewTransitionInProgress = YES; } //-- This is not a recursion, due to method swizzling the call below calls the original method. return [self safePopToRootViewControllerAnimated:animated]; }

    • (NSArray *)safePopToViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.viewTransitionInProgress) return nil; if (animated) { self.viewTransitionInProgress = YES; } //-- This is not a recursion, due to method swizzling the call below calls the original method. return [self safePopToViewController:viewController animated:animated]; }

    • (UIViewController *)safePopViewControllerAnimated:(BOOL)animated { if (self.viewTransitionInProgress) return nil; if (animated) { self.viewTransitionInProgress = YES; } //-- This is not a recursion, due to method swizzling the call below calls the original method. return [self safePopViewControllerAnimated:animated]; }

    • (void)safePushViewController:(UIViewController *)viewController animated:(BOOL)animated { self.delegate = self; //-- If we are already pushing a view controller, we dont push another one. if (self.isViewTransitionInProgress == NO) { //-- This is not a recursion, due to method swizzling the call below calls the original method. [self safePushViewController:viewController animated:animated]; if (animated) { self.viewTransitionInProgress = YES; } } }

    // This is confirmed to be App Store safe. // If you feel uncomfortable to use Private API, you could also use the delegate method navigationController:didShowViewController:animated:.

    • (void)safeDidShowViewController:(UIViewController *)viewController animated:(BOOL)animated { //-- This is not a recursion. Due to method swizzling this is calling the original method. [self safeDidShowViewController:viewController animated:animated]; self.viewTransitionInProgress = NO; }

    // If the user doesnt complete the swipe-to-go-back gesture, we need to intercept it and set the flag to NO again.

    • (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { id tc = navigationController.topViewController.transitionCoordinator; [tc notifyWhenInteractionEndsUsingBlock:^(id context) { self.viewTransitionInProgress = NO; //--Reenable swipe back gesture. self.interactivePopGestureRecognizer.delegate = (id)viewController; [self.interactivePopGestureRecognizer setEnabled:YES]; }]; //-- Method swizzling wont work in the case of a delegate so: //-- forward this method to the original delegate if there is one different than ourselves. if (navigationController.delegate != self) { [navigationController.delegate navigationController:navigationController willShowViewController:viewController animated:animated]; } }
    • (void)load { //-- Exchange the original implementation with our custom one. method_exchangeImplementations(class_getInstanceMethod(self, @selector(pushViewController:animated:)), class_getInstanceMethod(self, @selector(safePushViewController:animated:))); // method_exchangeImplementations(class_getInstanceMethod(self, @selector(didShowViewController:animated:)), class_getInstanceMethod(self, @selector(safeDidShowViewController:animated:))); method_exchangeImplementations(class_getInstanceMethod(self, @selector(popViewControllerAnimated:)), class_getInstanceMethod(self, @selector(safePopViewControllerAnimated:))); method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToRootViewControllerAnimated:)), class_getInstanceMethod(self, @selector(safePopToRootViewControllerAnimated:))); method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToViewController:animated:)), class_getInstanceMethod(self, @selector(safePopToViewController:animated:))); }

    @end