In this tutorial I’m going to show you how to build a Universal iPhone / iPad app that displays a self-contained Web site in a UIWebView component. Why would you want to do that? Well because you could then design and build entire apps with nothing more than HTML5, CSS and JavaScript.
There are plenty of books on building apps that work in the UIWebView – which uses the same technology behind the iPhone / iPad Safari Web browser. I would suggest searching for online tutorials and books with the keyword “WebKit.” You could narrow down your search by also searching on “Safari”.
A good place to start is here:
http://developer.apple.com/safari/
This tutorial is not going to cover WebKit. It’s just going to show you how to build a wrapper in Objective-C for the browser component. The design of the Web app is up to you. Though first, I would like to offer a few tips on writing your HTML.
Bundled HTML Issues
When bundling HTML to be run locally in a UIWebView, you need to consider a few issues:
- There is no server. So server-side scripts like PHP will not work. Though if your user is connected to the Internet you could always provide links to a Web site running PHP. But the focus here is on self-contained apps.
- Because there is no server there is nothing to interpret links like “test/” to mean “test/index.html.” So when linking to sub-folders you need to append “/index.htm,” “index.html,” etc.
- When copying HTML folders into your Xcode project be sure to check “Create Folder References for any added folders.” Otherwise Xcode will bundle your files into groups and relative HTML links to sub-folders won’t work.
- JavaScript calls can not take too long or your script may be stopped.
- When using the new HTML5 video tag, the iPad needs the controls attribute to be set to true (or anything which is interpreted as true).
- The iPad will use the first frame of a video as a poster frame. The iPhone will not and you should provide it.
- The iPhone and iPad support SVG (Scalable Vector Graphics) – a technology that I started writing about a few years ago – but didn’t have a lot of traction. Nice to see it making a comeback.
You should look for updated tutorials. - If you plan to also write a wrapper for Android and use your HTML there too, keep it simple. Their HTML5 implementation needs work. They currently do not support SVG and the video tag didn’t work for me in their latest emulator.
Now on to the tutorial:
Create a Project in Xcode
1. Select: File / New Project …
2. Select: [ iPhone OS / Applications ] / Window-based Application
3. Select: Product: Universal
4. Click: Choose
5. Name your project (I named this one TestWebApp01)
6. Click: Save
Create a simple Web Site
It is important that you create your initial Web site outside of Xcode.
1. Create a folder somewhere called “www”
2. Create an index.html file with this content.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!-- Change this if you want to allow scaling -->
<meta name="viewport"
content="width=default-width; user-scalable=no" />
<meta http-equiv="Content-type"
content="text/html; charset=utf-8">
<title>Web App Demo</title>
</head>
<body>
<h1>Test</h1>
<p>This is a test.</p>
</body>
</html>3. Back in Xcode expand the Shared folder
4. Right-click and select: Add / Existing Files …
5. Navigate to the “www” folder and click Add.
6. Check: Copy items into destination group’s folder (if needed)
7. IMPORTANT: Select: Create Folder References for any added folders
Create the App
1. Modify iPad / AppDelegate_Pad.h so that it looks like this:
#import @interface AppDelegate_Pad : NSObject { UIWindow *window; UIWebView *webView; // Add UIWebView and outlet below } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UIWebView *webView; @end
2. Modify iPhone / AppDelegate_Phone.h so that it looks like this:
#import @interface AppDelegate_Phone : NSObject { UIWindow *window; UIWebView *webView; // Add UIWebView and outlet below } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UIWebView *webView; @end
3. Make the following changes to AppDelegate_Pad.m
#import "AppDelegate_Pad.h" @implementation AppDelegate_Pad @synthesize window; @synthesize webView; // Synthesize webView #define WWW_ROOT @"www" // Define the root of the Web folder - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Add code to open bundled Web site NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:WWW_ROOT ]; NSURL *url = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; // Override point for customization after application launch [window makeKeyAndVisible]; return YES; }
4. Make the following changed to AppDelegate_Phone.m
#import "AppDelegate_Phone.h" @implementation AppDelegate_Phone @synthesize window; @synthesize webView; // Synthesize webView #define WWW_ROOT @"www" // Define the root of the Web folder - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Add code to open bundled Web site NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:WWW_ROOT ]; NSURL *url = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; // Override point for customization after application launch [window makeKeyAndVisible]; return YES; }
Interface Builder
1. In Xcode expand the iPad folder
2. Double click: MainWindow_Pad.xib to launch Interface Builder
3. Double click Window to bring up the iPad layout
4. Drag a Tool Bar to the bottom of the window
5. Click on the Tool Bar’s Item button and press Command-D three times so you end up with four buttons.
6. Change the first two buttons to be labeled “Back” and “Forward”
7. Click on the third button and press Command-1 to bring up the Attributes Inspector panel
8. Change the Identifier of the button to “Refresh”
9. Do the same for the fourth button, make its Identifier “Stop”
10. Drag a Flexible Space Bar Button Item between the two middle buttons
11. Drag a UIWebView from the Library above the toolbar and let if fill the rest of the window
Wire it Up
12. Click on App Delegate Pad
13. Press: Command-2 to bring up the Connections Inspector panel
14. Under Outlets drag from the circle next to webView to the UIWebView in the Window
15. Click on the UIWebView
16. Press: Command-2 to bring up the Connections Inspector panel
17. Under Received Actions drag from the circles next to the actions to the matching buttons (goBack to Back button, etc).
18. Select: File / Save
19. Repeat all these steps for the iPhone folder
Test It
If you set things for Simulator 3.2 the iPad simulator should come up.
If you set things for Simulator 3.1 the iPhone simulator should come up.
Source Code
If you have git installed, you can obtain a copy of the project in this tutorial by executing the following command:
git clone git@github.com:mitchallen/TestWebApp01.git

