iOSwebview中讀取html,css,js語句
阿新 • • 發佈:2019-02-07
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" onclick="OCModel.callSystemCamera()"> <input type="button" value="Call ObjC system alert" onclick="OCModel.showAlertMsg('js title', 'js message')"> </div> <div> <input type="button" value="Call ObjC func with JSON " onclick="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." onclick="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 } }