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 in iOS, 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.
Select Single View Application as the project type and click on next.
Write project name and other details.
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.
Add New file in project WebVC.swift
Set WebVC class to UIViewController with storyboardID.
Set outlet and Delegate of UIWebView.
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:
With the above steps, the UIWebView can be implemented in any iOS app. However, if you’re creating 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 WebView Example from Github.
You may also like,
- iOS Tutorial: How to create a simple browser using WKWebView
- How To Embed Android WebView to Load URLs Inside Android Apps
This page was last edited on March 26th, 2021, at 8:45.