1. Select the storyboard and disable the Use Auto Layout and Use Size Classes option.
2. Drag a UIWebView to the storyboard.
3. Control-drag the UIWebView to ViewControl.swift
@IBOutlet weak var myWebView: UIWebView!
<HTML>
<HEAD>
<TITLE>My Title</TITLE>
</HEAD>
<BODY>
<H2><CENTER>Hello World</CENTER></H2>
</BODY>
</HTML>
5. Create a JavaScript.js file in the project:
6. Complete the ViewController.swift file as below:
alert('JavaScript!');
6. Complete the ViewController.swift file as below:
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var myPath = NSBundle.mainBundle().pathForResource("index", ofType: "html")
myWebView.delegate = self //For webViewDidFinishLoad to be called
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: myPath!)!))
}
func webViewDidFinishLoad(webView: UIWebView) {
let jsPath = NSBundle.mainBundle().pathForResource("JavaScript", ofType: "js")
let jsContent = NSString(contentsOfFile: jsPath!, encoding: NSUTF8StringEncoding, error: nil) as! String
myWebView.stringByEvaluatingJavaScriptFromString(jsContent)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
7. The alert window:
The HTML file is displayed in UIWebView:
Thank you! It helps me!!
ReplyDelete