美文网首页
iOS【动态修改第三方类方法】

iOS【动态修改第三方类方法】

作者: NJ_墨 | 来源:发表于2018-02-25 21:38 被阅读29次
    #import "WKWebViewJavascriptBridge+ReplaceMethod.h"
    
    @implementation WKWebViewJavascriptBridge (ReplaceMethod)
    
    - (void)webView:(WKWebView *)webView
    decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
    decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
        
        
        WKWebView *tempWeb;
        id<WKNavigationDelegate> _tempWebViewDelegate;
        WebViewJavascriptBridgeBase *_tempBase;
        
        unsigned int count = 0;
        Ivar *ivar = class_copyIvarList([self class], &count);
        for (int i = 0; i<count; i++) {
            Ivar var = ivar[i];
            const char *varName = ivar_getName(var);
            NSString *proname = [NSString stringWithUTF8String:varName];
            
    //        NSLog(@"-----web:   %@",proname);
    //        const char * name = ivar_getName(var);
    //        const char * type = ivar_getTypeEncoding(var);
    //        NSLog(@"-----web 类型为 %s 的 %s ",type, name);
            
            
            if ([proname isEqualToString:@"_webView"]) {
                tempWeb = object_getIvar(self, var);
            } else if([proname isEqualToString:@"_webViewDelegate"]) {
                _tempWebViewDelegate = object_getIvar(self, var);
            } else if([proname isEqualToString:@"_base"]) {
                _tempBase = object_getIvar(self, var);
            }
            
        }
    
        if(tempWeb != webView && tempWeb) {
            return;
        }
        
        NSURL *url = navigationAction.request.URL;
        __strong typeof(_tempWebViewDelegate) strongDelegate = _tempWebViewDelegate;
        
        if ([_tempBase isCorrectProcotocolScheme:url]) {
            
            if ([_tempBase isCorrectProcotocolScheme:url]) {
                if ([_tempBase isBridgeLoadedURL:url]) {
                    [_tempBase injectJavascriptFile];
                } else if ([_tempBase isQueueMessageURL:url]) {
                    
                    if (class_respondsToSelector([self class],@selector(WKFlushMessageQueue))) {
                        [self performSelector:@selector(WKFlushMessageQueue)];
                    }
                } else {
                    [_tempBase logUnkownMessage:url];
                }
                decisionHandler(WKNavigationActionPolicyCancel);
                
                return;//添加此句解决三方库里的崩溃
            }
        }
        
        if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
            [_tempWebViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
        } else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
        
    }
    @end
    

    相关文章

      网友评论

          本文标题:iOS【动态修改第三方类方法】

          本文链接:https://www.haomeiwen.com/subject/wykdxftx.html