How To Use Web View In iOS Using Swift

Last updated on Aug 19, 2016

Milan Sanathara

Swift

0

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.

How to use web view in ios - Design UI


Step 2 Navigation


After design step complete, now add connection of your view component to view controller class.

To perform actions like back and forward page view, Add your button action connections to view controller.

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

If you want to load HTML page from file:

This is very helpful for giving user help on your app that how the user can use your app or explain your app features.

Step 4 Delegate (optional)


You can get more event by conforming UIWebViewDelegate.

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

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.

Post by Milan Sanathara

I am iOS Application Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *