How To Integrate UIWebView to Load URLs Within Your iOS App

0
Shares
How To Integrate UIWebView to Load URLs Within Your iOS App
2.2 (44%) 15 votes

Have you ever noticed that whenever we click on a link in Facebook app, it automatically loads the URL within the in-app browser only?

Well, it’s for keeping the users within app only.

And with UIWebView, iPhone app developers can now integrate this feature in their own mobile apps.

Today, in our iOS app tutorial, we’ll share the implementation process our development team follow with demonstration.

Let’s Get Started

Open your XCode and Create a new project under file menu.

webview1

Select Single View Application as the project type and click on next.webview2

Write project name and other details.

webview3

Once you create new project, Load a UITableView using following link in ViewController.swift class.

https://github.com/hiteshtrivedi/SampleTableView

with two options

1) Open Url

2) Open PDF File

Now go to Main.storyboard and drag and drop a new UIViewController from object library.

Search UIWebView object library and drag and drop it into new Controller in storyboard.

webview4

webview5

Add New file in project WebVC.swift

webview9

Set WebVC class to UIViewController with storyboardID.

webview6

webview7

Set outlet and Delegate of UIWebView.

webview8

class WebVC: UIViewController, UIWebViewDelegate {

@IBOutlet weak var web: UIWebView!
var strURL = "" //For get String URL with navigation

When user click on any of option from UITableView, it will navigate to WebVC controller by following code snippet.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let objWeb = self.storyboard?.instantiateViewControllerWithIdentifier("WebVCID") as! WebVC
if indexPath.row == 0 {
objWeb.strURL = //Pass any website URL
} else {
objWeb.strURL = //Pass Any PDF URL
}
self.navigationController?.pushViewController(objWeb, animated: true)
}

And in WebVC.swift it will load in UIWebView.

override func viewDidLoad() {
super.viewDidLoad()

let url = NSURL(string: strURL)
let request = NSURLRequest(URL: url!)
web.loadRequest(request)

// Do any additional setup after loading the view.
}

We have integrated UIWebView Delegate for performing action during loading start, end or failure.

//MARK: WebView Delegate Methods
func webViewDidStartLoad(webView: UIWebView) {
print("webViewDidStartLoad")
}

func webViewDidFinishLoad(webView: UIWebView) {
print("webViewDidFinishLoad")
}

func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
print("didFailLoadWithError")
}

And done!

Here’s the output:ss1

ss2

With the above steps, the UIWebView can be implemented in any iOS app. However, if you’re developing an iOS app from ground zero, and have never done it before, then it’s recommended to consult with an expert first, prepare a plan, Hire iPhone app developer to get it developed in the right way.

Grab a free copy of iOS Web View Example demo from Github.

 

 
0
Shares
 

Have an iOS App Idea in Mind? Let's Discuss!