Creating a simple browser with UIWebView in Swift.

UIWebView is used to load and diaplay web content in your application. and UIWebView is the part of UIKit.UIWebView class has been around iOS 2.0 and its is most successfully browser to display HTML content inside the apps.
and its very simple to open URL in your apps using UIWebView. 



 Another latest class of  WKWebView which is also to load web content in iOS app. Creating a simple browser with WKWebView in Swift.

This tutorial provide steps to load url in UIWebView in Swift.

Step 1 : Open the Xcode , Create new project and select Single View Application template as below.


Step 2 : After selecting, enter the product name as "WebviewSample". Organization name "GuruSoft" and Organization identifier "com.gurusoft", Then press the next and create the new project.


Step 3 : In project navigation, Select storyboard file and search UIWebView from Object Library and Drag and Drop in View. also set constrain of UIWebView.


Step 4 : Select UIWebView and Add to IBOutlet connection in UIViewController.swift class. 

After connect with IBOutlet , there is created instance of UIWebView.
@IBOutlet weak var webViewFullScreen: UIWebView!

Step 5 : Adding the UIWebViewDelegate in UIViewController.swift file.
class ViewController: UIViewController , UIWebViewDelegate {

Step 6 :  Take the default url which want to load in UIWebView. In this tutorial, I want to load my blog url MYURL in UIWebView. Define code in ViewDidLoad.
override func viewDidLoad() {
    super.viewDidLoad()
    let webUrl : NSURL = NSURL(string: "https://iosdevcenters.blogspot.com/")!
    let webRequest : NSURLRequest = NSURLRequest(URL: webUrl)
    webViewFullScreen.loadRequest(webRequest)
}

Step 7 : When loading the URL in UIWebView, The below UIWebViewDelegate method is Called.
//MARK:- UIWebViewDelegate Method

func webViewDidStartLoad(webView: UIWebView) {
    print("Strat Loading")
}
func webViewDidFinishLoad(webView: UIWebView) {
    print("Finish Loading")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
    print(error?.localizedDescription)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    return true
}

Loading Url in UIWebView:



Note :   After iOS 8 and Later, Apple adding WebKit. Creating a simple browser with WKWebView in Swift.

Download Sample Code

Thanks.

Creating a simple browser with UIWebView in Swift. Creating a simple browser with UIWebView in Swift. Reviewed by KIRIT MODI on 06:02:00 Rating: 5

No comments:

Powered by Blogger.