更新时间:2025-03-14 09:28:47
日期 | 版本 | 修订内容摘要 |
---|---|---|
2024-07-10 | v2.0.0 | 更新接口文档 |
package com.chuanglan.webtest; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.annotation.TargetApi; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.webkit.CookieManager; import android.webkit.PermissionRequest; import android.webkit.SslErrorHandler; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; public class MainActivity extends AppCompatActivity { private WebView mWebView; private static final int PERMISSIONS_EXTERNAL_STORAGE = 801; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if ((ContextCompat.checkSelfPermission( this.getApplication(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this.getApplication(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSIONS_EXTERNAL_STORAGE); } else { this.initData(); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_EXTERNAL_STORAGE); } ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSIONS_EXTERNAL_STORAGE); initData(); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } this.initData(); super.onRequestPermissionsResult(requestCode, permissions, grantResults); } private void initData() { String url = "https://static2.253.com/sdk-demo/index22.html"; mWebView = findViewById(R.id.shanyan_view_baseweb_webview); WebSettings webSetting = mWebView.getSettings(); // 禁止 file 协议加载 JavaScript webSetting.setAllowFileAccess(true); webSetting.setJavaScriptEnabled(true); webSetting.setAllowFileAccessFromFileURLs(true); webSetting.setAllowUniversalAccessFromFileURLs(true); webSetting.setUseWideViewPort(true); webSetting.setLoadWithOverviewMode(true); if (Build.VERSION.SDK_INT < 17) { mWebView.removeJavascriptInterface("searchBoxJavaBridge_"); mWebView.removeJavascriptInterface("accessibility"); mWebView.removeJavascriptInterface("accessibilityTraversal"); } webSetting.setSupportZoom(true); webSetting.setSavePassword(false); webSetting.setBuiltInZoomControls(true); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); webSetting.setSupportMultipleWindows(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setDomStorageEnabled(true); webSetting.setPluginState(WebSettings.PluginState.ON); mWebView.setWebViewClient(new MyWebViewClient()); mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onPermissionRequest(final PermissionRequest request) { request.grant(request.getResources()); } }); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptThirdPartyCookies(mWebView, true); mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); mWebView.getSettings().setAllowFileAccessFromFileURLs(true); mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true); mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setSaveFormData(true); mWebView.getSettings().setSupportZoom(false); mWebView.getSettings().setDomStorageEnabled(true); initData(url); } private void initData(String url) { mWebView.loadUrl(url); } class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onReceivedSslError(WebView view, final SslErrorHandler handler, android.net.http.SslError error) { handler.cancel(); } @Override public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { return super.shouldOverrideKeyEvent(view, event); } } }
mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onPermissionRequest(final PermissionRequest request) { request.grant(request.getResources()); } });
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone; config.allowsInlineMediaPlayback = YES; /// 这里需要将属性设置在初始化的WKWebViewConfiguration对象上, /// 因为_webview. configuration为原始对象拷贝的原因,所以通过以下方式设置相应属性并不会生效: _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];