iOSwebview中读取html,css,js语句

本文详细探讨了如何在iOS应用中使用WebView加载和处理HTML、CSS和JavaScript代码,包括如何实现页面与原生代码的交互,以及优化WebView性能的关键技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

           webview里面经常要使用前端的内容,下面我来拉出一段代码,看看iOS里面是如何读取的。
<!DOCTYPE html>
<html>
<head>
	<title>测试IOS与JS之前的互调</title>
	<style type="text/css">
	  * {
	  	font-size: 40px;
	  }
	</style>
  <script type="text/javascript">

  var jsFunc = function() {
    alert('Objective-C call js to show alert');
  }
  
  var jsParamFunc = function(argument) {
    document.getElementById('jsParamFuncSpan').innerHTML
    = argument['name'];
  }
  
  </script>
  
</head>

<body>
  
<div style="margin-top: 100px">
	<h1>Test how to use objective-c call js</h1>
	<input type="button" value="Call ObjC system camera" οnclick="OCModel.callSystemCamera()">
	<input type="button" value="Call ObjC system alert" οnclick="OCModel.showAlertMsg('js title', 'js message')">
</div>

<div>
	<input type="button" value="Call ObjC func with JSON " οnclick="OCModel.callWithDict({'name': 'testname', 'age': 10, 'height': 170})">
	<input type="button" value="Call ObjC func with JSON and ObjC call js func to pass args." οnclick="OCModel.jsCallObjcAndObjcCallJsWithDict({'name': 'testname', 'age': 10, 'height': 170})">
</div>
<div>
  <a href="test1.html">Click to next page</a>
</div>

<div>
	<span id="jsParamFuncSpan" style="color: red; font-size: 50px;"></span>
</div>


</body>
</html>


namespace Webview
{
	public partial class ViewController : UIViewController
	{
		public ViewController (IntPtr handle) : base (handle)
		{
		}
		UIWebView webView;
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			webView = new UIWebView (new CoreGraphics.CGRect (0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height));
			webView=new UIWebView();
			webView.Frame = this.View.Bounds;
			webView.BackgroundColor = UIColor.Red;
			webView.ScalesPageToFit = true; 
			NSUrl url = NSBundle.MainBundle.GetUrlForResource("test","html");
			string htmlString = NSString.FromData (NSData.FromUrl (url), NSStringEncoding.UTF8);
			NSUrlRequest request = new NSUrlRequest (url);
			this.View.AddSubview (webView);
			webView.LoadHtmlString (htmlString, url);
//			webView.Delete = this;
			this.View.AddSubview(webView);

		}

		public override void DidReceiveMemoryWarning ()
		{
			base.DidReceiveMemoryWarning ();
			// Release any cached data, images, etc that aren't in use.
		}
//		void webViewDidFinishLoad
	}
}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值