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
}
}