Simplified iOS

  • Home
  • About
  • Contact
  • Advertise
  • Write for Us

UIWebView Example to Load URL in iOS using Swift in Xcode

October 15, 2016 by Belal Khan 15 Comments

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).
uiwebview example

UIWebView Example

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.
UIWebView Example

UIWebView Example

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.
uiwebview example

UIWebView Example

  • 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);
    }
UiWebView Example

UiWebView Example

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 🙂

Share this:

  • Tweet
  • Share on Tumblr
  • WhatsApp

Related

Filed Under: iOS Development Tutorial Tagged With: UIWebView Example, UIWebView Load URL, UIWebView Tutorial

About Belal Khan

I am Belal Khan, I am currently pursuing my MCA. In this blog I write tutorials and articles related to coding, app development, iphone etc.

Comments

  1. Talha says

    December 1, 2016 at 9:09 am

    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

    Reply
    • Steven says

      December 18, 2016 at 12:33 am

      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.

      Reply
    • Jack says

      March 14, 2018 at 2:30 pm

      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.

      Reply
  2. Allan Pau says

    December 29, 2016 at 6:00 am

    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)

    Reply
    • Shrinath Dhatrak says

      June 22, 2017 at 6:50 am

      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);

      Reply
      • Shrinath Dhatrak says

        June 22, 2017 at 6:52 am

        Sry its “url”, instead of “rule”.

        Reply
  3. sun says

    February 10, 2017 at 2:26 am

    In swift 3.x, these code works

    let url = URL(string: “https://m.bmqb.com”);
    let request = URLRequest(url: url!);
    webView.loadRequest(request);

    Reply
    • tommy says

      July 15, 2017 at 6:39 pm

      This worked for me thanks! Spent way too much time trying to get this simple piece working!!!

      Reply
  4. david says

    March 27, 2017 at 8:02 am

    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?

    Reply
  5. Svenja says

    September 20, 2017 at 6:57 pm

    It doesn’t work on Xcode 9 and Swift 4

    Reply
  6. Shehryar Khan says

    November 17, 2017 at 1:41 pm

    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.

    Reply
  7. Rob says

    February 20, 2018 at 4:53 pm

    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.

    Reply
  8. Sandeep says

    April 11, 2018 at 11:34 am

    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?

    Reply
  9. Josbal Cuevas says

    June 26, 2018 at 11:20 pm

    Hi! very good work, it works for me. THANKS!!!
    How can i add an activity indicator to load de url?

    Reply
  10. Iain M Guthrie says

    July 19, 2018 at 6:28 pm

    Can someone post a working ViewController.swift that is compatible with Xcode 9.0.1?

    Reply

Leave a Reply Cancel reply

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




About Me

belal khan simplified ios

Hello I am Belal Khan, founder and owner of Simplified iOS. I am currently pursuing MCA from St. Xavier's College, Ranchi. Apart from my academic I am a blogger, I run various websites and majority of them are about coding and development.

Connect with Me

Follow Simplified iOS

Simplified iOS

Popular Posts

  • Swift SQLite Tutorial for Beginners – Using… (99,379)
  • Swift PHP MySQL Tutorial – Connecting iOS App… (98,247)
  • UIWebView Example to Load URL in iOS using Swift in Xcode (79,974)
  • Download Full High Sierra Installer to Create Bootable USB (70,273)
  • Xcode Login Screen Example using Swift 3, PHP and MySQL (67,256)
  • How to Format USB on Mac? Formatting External Hard… (61,373)
  • Swift JSON Tutorial – Fetching and Parsing… (59,402)
  • Firebase Realtime Database Tutorial for Swift using Xcode (53,697)
  • iOS Registration Form Example using PHP and MySQL (48,915)
  • Xcode Text Field Tutorial for iOS Application using Swift (41,145)




About

Simplified iOS is a blog where you can find latest tutorials related to coding and app development for iphone and MAC OS. Here you can get Simplified iPhone, iPad and Mac OS development tutorial to know the things about Apple Development from basics to advanced level.

Quick Links

  • Advertise
  • Contact
  • Disclaimer
  • Privacy Policy
  • Write for Us

Copyright © 2017 · Simplified iOS· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·