Friday, August 22, 2014

iOS Tutorial - Part 19 - Create a Web Application, UIWebView

Create a Web Application, UIWebView


Video Description 
Web Application is an App that is written with HTML,CSS,JavaScript and has a wrapper to be able to show on a device. In this tutorial we will learn how to create a web app. In order to create a web application, follow the bellow steps:
1. create a new project
2. From storyboard, drag and drop WebView from pallet to the view controller
3. Control drag from web View to it's controller to create a property for the webView
4. Drag and drop your HTML files, inside of the project (Make sure you copy as a reference folder)
5. In ViewController inside of viewDidLoad add the following lines:
- (void)viewDidLoad
{
    [super viewDidLoad];
 self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"bootstrap" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:htmlFile];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0.0];
    
    [self.webView loadRequest:request];
}  
You are done. If you run the application it should show the HTML content.

Show a URL in the WebView

If you want to show a website, you need to change your request like bellow:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];

Add Back, Forward, Stop and Refresh button

You can easily add above buttons for your WebView without even writing any code for it. All you have to do is to add some buttons to the screen and from each individual button, control drag to the WebView. It shows a list containing reload, goBack, goForward, stopLoading. Based on your button click to any of these links and they will work.

Download

Download the Web App from here

No comments:

Post a Comment