In this post we will see a simple UIWebView Example. In many cases while building an app we sometimes need to load some other website or URL. In this case UIWebView is very useful. We can use UIWebView class to embed web content in your iOS App. And today in this UIWebView Example we will learn how we can do this.
If you are an absolute beginner then I would recommend you to go through the starting post Getting Started with Xcode and Swift.
Creating a new Xcode Project
- The first thing as always, creating a new Xcode Project.
- Select Single View Application template. I have just created an app named UIWebViewExample.
- Now you will get your Main.storyboard.
Adding UIWebView to Storyboard
- Find WebView and drag it to your Storyboard. (See the image for help).
Connecting WebView to Swift
- We have the WebView in our storyboard and now we need to connect it to ViewController.swift.
- So first we need to open the Assistant Editor, then press control and drag WebView to the code. Same as we done in the Xcode Button Tutorial.
Loading a URL to WebView
- For example lets take URL of Simplified iOS. So we have the URL https://www.simplifiedios.net
- Now come inside ViewController.swift. Right now the code is as below.
// // ViewController.swift // UIWebViewExample // // Created by Belal Khan on 15/10/16. // Copyright © 2016 Belal Khan. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
- We will load the URL inside viewDidLoad() method.
Loading URL
We can easily load any URL inside the WebView. The things needed to do this are:
- NSURL: It will take the URL that we need to load as a String.
//first we will create a NSURL with the url that we want to load in the webview let url = NSURL (string: "https://www.simplifiedios.net");
- NSURLRequest: It will take NSURL object that we created above.
//now we need an NSURLRequest and it will take the NSURL let request = NSURLRequest(URL: url!);
- loadRequest(): We can call this method with the webView object that we already created. And it will take the NSURLRequest as an argument.
Final Code
- So the final code we have for the ViewController.swift is.
// // ViewController.swift // UIWebViewExample // // Created by Belal Khan on 15/10/16. // Copyright © 2016 Belal Khan. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let url = NSURL (string: "https://www.simplifiedios.net"); let request = NSURLRequest(URL: url!); webView.loadRequest(request); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Running the App
- Now we can test the app, just run it on Simulator.
- So as you can see the URL is loaded in the App.
Loading Local HTML to WebView
- Its not always necessary that you need to load a URL only. Sometimes we need to load local HTML files as well.
- So I have an HTML file named index.html. And we will modify the code of viewDidLoad() as below.
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //getting local file let localHtmlFile = NSBundle.mainBundle().URLForResource("index", withExtension: "html"); //creating request let request = NSURLRequest(URL: localHtmlFile!); //loading request webView.loadRequest(request); }
Loading HTML Directly
- You can hardcode the HTML directly to the WebView as well. For this we have loadHTMLString() method.
override func viewDidLoad() { super.viewDidLoad() webView.loadHTMLString("<h1>HTML</h1>", baseURL: nil) }
So thats all for this UIWebView Example friends. This was a very basic UIWebView Example. But if you still having some queries or confusion don’t hesitate to leave you comments. And in upcoming post I will share a tutorial with you where you can convert your website to Simple iOS Application using WebView. Thank You 🙂
Talha says
Please help me , i have created an iphone applicaiton and it was working successfullt when deployed on device but after 1 week it is only showing splash screen and then close. i don’t know what is the reason and i don’t know how to identify the reason. please help me if you can, Thanks in advance
Steven says
You need to be MUCH more specific than that. You didn’t even explain what your app does, let alone provide the source code so someone could take a look and see where the error is.
Jack says
When you create an app, Apple gives you a temporary certificate. After a week, this certficate expires, so you will need to reload the app onto your iPhone again. You may notice that the trusted developer certificate disappears in the settings, which you trusted when you first downloaded the app. I had the same problem, and it appears many others do. I believe this is the cause.
Allan Pau says
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: “https://www.simplifiedios.net”);
let request = NSURLRequest(URL: url!);
webView.loadRequest(request);
but why i can’t use
(‘NSURL’ is not implicitly convertible to ‘URL’, did you mean to use ‘as’ to explicitly convert)
Shrinath Dhatrak says
Just some modifications need to be done in code as below, it will work:-
let request = NSURLRequest(rule: url! as URL);
webview.loadRequest(request as URLRequest);
Shrinath Dhatrak says
Sry its “url”, instead of “rule”.
sun says
In swift 3.x, these code works
let url = URL(string: “https://m.bmqb.com”);
let request = URLRequest(url: url!);
webView.loadRequest(request);
tommy says
This worked for me thanks! Spent way too much time trying to get this simple piece working!!!
david says
hello I just like to know I had an web view application in iOS using swift, this web view application have multiple buttons to go to different departments in the app with in each department there is a call us button if this button pressed for example then the app opens the dialer pa to call no.1 = “number1” yet again if a button pressed on another department a different number, “number2” should be called. any ideas of how to do this?
Svenja says
It doesn’t work on Xcode 9 and Swift 4
Shehryar Khan says
Hi Belal hope you are fine and doing great, i am really on the same track and found your blog very helpful, i am actually doing the two webViews on one view controller by placing one smaller view above the bigger view same as the skype or messenger video, but my app is not a video chatting app so its quite simple having one UI view controller with two webViews on each other with one small and one full screen.
your little help is needed. thanks.
Rob says
Nowadays one should replace Ll those `NSURL` and `NSURLRequest` references with `URL` and `URLRequest`, respectively.
Also, all of those semicolons should be removed. You should only use semicolons when you have multiple statements on one line of code. Sure, you *can* insert all of the semicolons, but it results in code that looks like the author didn’t know anything about Swift.
Sandeep says
Where should i put the index.html file? what if i have a folder containing html, js & css files. How will i give the path?
Josbal Cuevas says
Hi! very good work, it works for me. THANKS!!!
How can i add an activity indicator to load de url?
Iain M Guthrie says
Can someone post a working ViewController.swift that is compatible with Xcode 9.0.1?