According to apple office Demo: https://developer.apple.com/videos/play/wwdc2017/220/
1,add the code
+(NSDictionary *)cacheDicSchema {
return @{@"mwweb-local":@"http",
@"mwweb-locals":@"https"};
}
-(void)hk_setPreferences:(WKPreferences *)perferences {
[self hk_setPreferences:perferences];
if (@available(iOS 11.0, *)) {
for (NSString *key in [[[self class] cacheDicSchema] allKeys]) {
[self setURLSchemeHandler:[[WeakSchemeHandler alloc] init] forURLScheme:key];
}
}
}
2, the contentRuleList as the value, and compile into the wkwebview
[
{
"trigger":
{ "url-filter" : ".*"
},
"action": {
"type" : "make-https"
}
}
}
]
compile into the WKWebView
if (@available(iOS 11.0, *)) {
[[WKContentRuleListStore defaultStore]
compileContentRuleListForIdentifier:@"MWWKWebViewContentRules" encodedContentRuleList:contentRuleList
completionHandler:^(WKContentRuleList *contentRuleList, NSError *error) {
if (error == nil) {
[config.userContentController addContentRuleList:contentRuleList];
}else{
MWHYLog(@"compileContentRuleListForIdentifier Error == %@",[error description]);
}
}];
}
3,in html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta nemo name="basepath" content="/c_mboss/smartcloud">
<script nonce="abc">
var domDoc = document.documentElement;
var domMeta = document.querySelector('meta[name="viewport"]');
var dpr = window.devicePixelRatio || 1;
// var dpr = 1;
var scale = 1 / dpr;
var rem = domDoc.clientWidth * dpr / 7.5;
var content = 'width=' + domDoc.clientWidth * dpr +
', initial-scale=' + scale +
', maximum-scale=' + scale +
', minimum-scale=' + scale +
', user-scalable=no';
domMeta.setAttribute('content', content);
domDoc.style.fontSize = rem + 'px';
</script>
<title>XXX</title>
<link href="https://webresource.mwee.cn/c_mboss/smartcloud/v_20180515163218/css/dist/main.min.css" rel="stylesheet">
</head>
<body>
<div id="appview"></div>
<input id="umengId" value="1264335506" style="display:none;" />
<img> src="mwweb-locals://XXXXX/index/kdxz.png"/>
<script type="text/javascript" src="mwweb-locals://XXXXX/js/mmm.js"></script>
<script type="text/javascript" src="mwweb-locals://XXXXX/js/app.js"></script>
</body>
</html>
<img>
do work as the apple said.
<script>
never work, I try again and again ............
With the Error in safari
[Warning] The page at https://10.88.3.95:3334/c_mboss/smartcloud/index/index was allowed to display insecure content from mwweb-locals://XXXXX/index/kdxz.png. (index, line 44)
[Warning] [blocked] The page at https://10.88.3.95:3334/c_mboss/smartcloud/index/index was not allowed to run insecure content from mwweb-locals://XXXXX/js/mmm.js.
[Warning] [blocked] The page at https://10.88.3.95:3334/c_mboss/smartcloud/index/index was not allowed to run insecure content from mwweb-locals://XXXXX/js/app.js.
I guess the Content Securiy Policy do conflict with WKSchemeHandler in WKWebView, the Content Securiy Policy will block the process of loading static resouces before the WKSchemeHandler.
Here it is strange that why it blocked <img>
but not <script>
once you add contentRuleList into the WKWebView which loadRequest website https://XXXXX.