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

#import "YHInjectConfigManager.h"


@implementation SchmeModel4wk

- (instancetype)copyWithZone:(NSZone *)zone {
    SchmeModel4wk *model = [[SchmeModel4wk allocWithZone:zone] init];
    model.className  = self.className;
    model.schemeName = self.schemeName;
    
    return model;
}

@end

@interface YHInjectConfigManager()

//设置自定义 scheme 处理
@property (nonatomic,strong) NSArray <SchmeModel4wk *> *schemeArray;
@property (nonatomic,copy)NSString *jsCordova;

@end
@implementation YHInjectConfigManager

/**
 * @abstract
 * 返回初始化好的单例
 *
 * @return 返回的单例
 */
+ (instancetype)sharedInstance{
    static YHInjectConfigManager *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[YHInjectConfigManager alloc] init];
    });
    return sharedInstance;
}
#pragma mark - Private

- (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");
    }
}


- (void)mockLocalURLResource:(NSArray <SchmeModel4wk *> *)array {
    self.schemeArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(SchmeModel4wk *  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([evaluatedObject isKindOfClass:[SchmeModel4wk class]]
             && NSClassFromString(evaluatedObject.className)) {
            return YES;
        } else {
            return NO;
        }
    }]];
}

- (void)unMockLocalURLResource {
    @synchronized (self.schemeArray) {
        if (self.schemeArray.count > 0) {
            self.schemeArray = [NSArray array];
        }
    }
}

- (void)registerURLSchemeHandler:(WKWebViewConfiguration *)config {

    NSArray *resultArray = nil;
    @synchronized (self.schemeArray) {
        resultArray = [self.schemeArray copy];
    }

    for (SchmeModel4wk *item in resultArray) {
        if (item.className.length > 0 && item.schemeName.length > 0) {
            Class class = NSClassFromString(item.className);
            if (class) {
                if (@available(iOS 11.0, *)) {
                    [config setURLSchemeHandler:[class new] forURLScheme:item.schemeName];
                } else {
                    // Fallback on earlier versions
                }
            }
        }
    }
}

- (void)updateWKWebViewSettingWithConfigureation:(WKWebViewConfiguration *)config
{
    config.allowsInlineMediaPlayback = self.allowsInlineMediaPlayback;
}

@end