YHInjectJsCordovaManager.m 3.17 KB
//
//  YHInjectJsCordovaManager.m
//  YH_Explorer
//
//  Created by 孙凯 on 2018/5/15.
//

#import "YHInjectJsCordovaManager.h"

@interface YHInjectJsCordovaManager()

@property (nonatomic,copy)NSString *jsCordova;

@end
@implementation YHInjectJsCordovaManager

/**
 * @abstract
 * 返回初始化好的单例
 *
 * @return 返回的单例
 */
+ (instancetype)sharedInstance{
    static YHInjectJsCordovaManager *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[YHInjectJsCordovaManager alloc] init];
    });
    return sharedInstance;
}
#pragma mark - Private
/**
 *  动态注入Js
 *
 *  @param resource js的名称
 */
- (BOOL)injectJavascript:(NSString *)resource webView:(UIWebView*)webView{
    if(!self.jsCordova) {
        NSString *jsPath = [[NSBundle mainBundle] pathForResource:resource ofType:@"js"];
        self.jsCordova = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];
    }
    
    //用cordova.version判断cordova是否注入成功
    NSString* jsCordovaVersion = [NSString stringWithFormat:@"cordova.version"];
    NSString* res = [webView stringByEvaluatingJavaScriptFromString:jsCordovaVersion];
    if (res != nil && res.length > 0) {
        return TRUE;
    }
    
    NSLog(@"injectJavascript uiwebview");
    //注入cordova js 代码
    [webView stringByEvaluatingJavaScriptFromString:self.jsCordova];
    
    //再次判定是否已经注入
    res = [webView stringByEvaluatingJavaScriptFromString:jsCordovaVersion];
    
    if (res != nil && res.length > 0) {
        return TRUE;
    }
    return FALSE;
}

/**
 @brief 循环注入直到成功
 
 @since 1.0.2
 */
- (void)cycleInject:(UIWebView*)webView {
    
    BOOL bSuc = NO;
    // 向网页中注入cordova
    bSuc = [self injectJavascript:@"cordova" webView:webView];
        
    if (!bSuc) {
        __weak typeof(self) weakSelf = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf cycleInject:webView];
        });
        
    }
}


- (void)cycleInjectWKWebView:(WKWebView *)webView
{
    if(!self.jsCordova) {
        NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"cordova" ofType:@"js"];
        self.jsCordova = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];
    }
    
    if (self.jsCordova.length == 0) {
        return;
    }
    
    WKUserContentController *userContentController = webView.configuration.userContentController;
    NSMutableArray<WKUserScript *> *array = [userContentController.userScripts mutableCopy];
    WKUserScript *injectUserScript = nil;
    for (WKUserScript *wkUScript in array) {
        if ([wkUScript.source isEqual:self.jsCordova]) {
            injectUserScript = wkUScript;
            break;
        }
    }
    
    if (!injectUserScript) {
        injectUserScript = [[WKUserScript alloc] initWithSource:self.jsCordova injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
        [userContentController addUserScript:injectUserScript];
        NSLog(@"injectJavascript wkwebview success");
    }
}


@end