Xam.Plugin.Webview
Xam.Plugin.Webview copied to clipboard
js 注入时机太迟,做APP时必须提早注入。
internal async Task
var response = string.Empty;
try
{
var script = new WKUserScript(new NSString(js), WKUserScriptInjectionTime.AtDocumentStart, false);
if (!_contentController.UserScripts.Contains(script)) {
_contentController.AddUserScript(script);
_configuration.UserContentController = _contentController;
}
//var obj = await Control.EvaluateJavaScriptAsync(js).ConfigureAwait(false);
//if (obj != null)
// response = obj.ToString();
}
catch (Exception) { /* The Webview might not be ready... */ }
return response;
}
代码直接写入到WKUserScriptInjectionTime.AtDocumentStart
然后在html 中,这样$(function(){ alert(jsCallAPP));} 则可以响应,如果用Control.EvaluateJavaScriptAsync(js)则在onready 事件中也未注入,所以为undefine 或报错。