在移动应用开发中,WebView 是一个非常重要的组件,它允许应用内嵌一个完整的网页浏览器,从而实现混合开发。通过WebView,开发者可以充分利用原生应用的优势,同时也能够享受到Web开发的便捷性。本文将详细介绍手机浏览器WebView的调用技巧以及常见问题的解析。

WebView调用技巧

1. 初始化WebView

在Android和iOS平台上,初始化WebView的方式略有不同。

Android

WebView myWebView = new WebView(context);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");

iOS

let webView = UIWebView(frame: self.view.bounds)
webView.loadRequest(URLRequest(url: URL(string: "http://www.example.com")!))

2. 设置JavaScript接口

为了让JavaScript可以调用原生代码,需要设置JavaScript接口。

Android

webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidInterface");

iOS

”`swift webView.delegate = self webView.navigationDelegate = self webView.scrollView.bounces = false webView.scrollView.delegate = self webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled = false webView.scrollView.contentInsetAdjustmentBehavior = .never webView.scrollView.bouncesZoom = false webView.scrollView.decelerationRate = UIScrollView.DecelerationRate.fast webView.scrollView.showsVerticalScrollIndicator = false webView.scrollView.showsHorizontalScrollIndicator = false webView.scrollView.isDirectionalLockEnabled = true webView.scrollView.bounces = false webView.scrollView.isScrollEnabled