
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to create a WebView in an iOS App using Swift?
As an iOS developer, you will come across multiple scenarios where you have to display something in web, for that we use WebView.
As per Apple, − It is an object that displays interactive web content, such as for an in-app browser.
So in this post, we will be seeing how to create WebView and load the data in it.
So let’s start
Step 1 − Open Xcode and create a single view application and name it WebViewSample.
Step 2 − Open ViewController.swift file and import the WebKit module. import WebKit
Step 3 − Add a property of WebKit in ViewController.swift.
var webView: WKWebView!
Step 4 − Add WKUIDelegate delegate to ViewController.swift
Step 5 − In ViewController.swift add the below method, add override loadView function.
override func loadView() { let webConfiguration = WKWebViewConfiguration() webView = WKWebView(frame: .zero, configuration: webConfiguration) webView.uiDelegate = self view = webView }
Step 6 − In viewDidLoad create the URL request which you want to load and load the URL
override func viewDidLoad() { super.viewDidLoad() let myURL = URL(string:"https://www.apple.com") let myRequest = URLRequest(url: myURL!) webView.load(myRequest) }
Step 7 − Run the application,
Complete code
import UIKit import WebKit class ViewController: UIViewController, WKUIDelegate { var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() let myURL = URL(string:"https://www.apple.com") let myRequest = URLRequest(url: myURL!) webView.load(myRequest) } override func loadView() { let webConfiguration = WKWebViewConfiguration() webView = WKWebView(frame: .zero, configuration: webConfiguration) webView.uiDelegate = self view = webView } }
- Related Articles
- How to create a WebView in an Android App using Kotlin?
- How to create a Custom Dialog box on iOS App using Swift?
- How to create a WebView in iOS/iPhone?
- How to make an HTTP request on iOS App using Swift?
- How to create a WebView in android app?
- How to load and display an image on iOS App using Swift?
- How to make an HTTP POST request on iOS App using Swift?
- How to hide the status bar in a iOS App using Swift?
- How to rotate an image in imageview by an angle on iOS App using Swift?
- How to create CollectionView Layout in an iOS App?
- How to display an image with rounded corners on iOS App using Swift?
- How to create Tab Bar Layout in an iOS App?
- How to integrate a facebook login in swift for iOS App?
- How to send an attachment in email using Swift(ios)?
- How to create scrollable TextView on iOS App?
