How To Use Web View In iOS Using Swift
Contents
Objective of using Web View in iOS apps
Load a web page in the app using web view.
Steps
1. Design UI
2. Navigation
3. Load Web Page
4. Delegate (optional)
You will get final output like this:
A web view is what the Safari browser uses on iOS to load web content. You have the whole power of Safari in your iOS apps through the UIWebView class. All you have to do is place a web view on your UI and use one of its loading methods.
Step 1 Design UI
Create New Xcode project and select “Single View Application” template. Add UIWebView in your view controller from the object library. Now add navigation bar and bar button. Two buttons left side and two buttons right side.
After design step complete, now add connection of your view component to view controller class.
@IBOutlet weak var navigationTitle: UINavigationItem! @IBOutlet weak var webView: UIWebView!
To perform actions like back and forward page view, Add your button action connections to view controller.
@IBAction func backAction(sender: AnyObject) { if webView.canGoBack { webView.goBack() } } @IBAction func forwardAction(sender: AnyObject) { if webView.canGoForward { webView.goForward() } } @IBAction func refreshAction(sender: AnyObject) { webView.reload() } @IBAction func stopAction(sender: AnyObject) { webView.stopLoading() }
Property and Method | Description |
---|---|
canGoBack | A Boolean value indicating whether the web view can move backward. If true, able to move backward; otherwise, false. |
goBack() | Loads the previous location in the back-forward list. |
canGoForward | A Boolean value indicating whether the web view can move forward. If true, able to move forward; otherwise, false. |
goForward() | Loads the next location in the back-forward list. |
reload() | Reloads the current page. |
stopLoading() | Stops the loading of any web content managed by the web view. |
Step 3 Load Web Page
Add the following code to your viewDidLoad method in view controller file
override func viewDidLoad() { super.viewDidLoad() webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!)) }
If you want to load HTML page from file:
override func viewDidLoad() { super.viewDidLoad() self.webView.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource("index", withExtension: "html", subdirectory: "faq")!)) }
Step 4 Delegate (optional)
You can get more event by conforming UIWebViewDelegate.
func webViewDidStartLoad(webView: UIWebView) { UIApplication.sharedApplication().networkActivityIndicatorVisible = true } func webViewDidFinishLoad(webView: UIWebView) { UIApplication.sharedApplication().networkActivityIndicatorVisible = false navigationTitle.title = webView.stringByEvaluatingJavaScriptFromString("document.title") } func webView(webView: UIWebView, didFailLoadWithError error: NSError?) { UIApplication.sharedApplication().networkActivityIndicatorVisible = false }
Method | Description |
---|---|
webViewDidStartLoad | Sent after a web view starts loading a frame. |
webViewDidFinishLoad | Sent after a web view finishes loading a frame. |
didFailLoadWithError | Sent if a web view failed to load a frame. |
Download Xcode Project
[sociallocker]WebView Example[/sociallocker]
I hope you will find this post very useful regarding web view in iOS . Let me know if you have any question regarding web view in iOS in a comment . I will reply to you ASAP.
Have you got a cool idea about iPhone App Development? Contact us Now to get a free consultation on your idea. Alphanso Tech is rated as one of the best iPhone App Development Company in India.