@interface ViewController ()
@property(nonatomic, strong) IBOutlet UIWebView *webView;
@property(nonatomic, assign) IBOutlet UIBarButtonItem *backBarButton;
@end
Next we will add some code to load the webView and make the backBarButton usable.
=== Make your 'viewDidLoad' method look like this: ===
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// ** FIX FOR iOS 7 **
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
// ** SET NAVIGATION ITEM DISABLED **
_backBarButton.enabled = NO;
// ** DECORATE WEB-VIEW A BIT **
_webView.layer.borderWidth = 3.0f;
_webView.layer.borderColor = [[UIColor grayColor]CGColor];
// ** LOAD WEB-VIEW **
[self loadWebView];
}
=== Add a new IBAction-Method for the 'backBarButton': ===
- (IBAction)navigationButtonAction:(id)sender {
if ([sender isKindOfClass:[UIBarButtonItem class]]) {
UIBarButtonItem *buttonItem = (UIBarButtonItem *)sender;
switch (buttonItem.tag) {
case 0:
[self loadWebView];
_backBarButton.enabled = NO;
break;
default:
break;
}
}
}
=== Add one more method called 'loadWebView': ===
- (void)loadWebView {
// ** GET BUNDLE-PATH TO BUILD THE 'baseURL' **
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath];
// ** GET PATH-STRING OF 'index.html' FILE **
NSString *filePath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:bundlePath];
// ** GET HTML-STRING OF FILE **
NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
// ** START LOADING WEBVIEW WITH 'htmlString' AND 'baseURL' **
[_webView loadHTMLString:htmlString baseURL:baseURL];
}
Here is already the first thing to make refer to local files work. Instead loading the webView with a request, we're loading the webView with the UIWebView-Method 'loadHTMLString:(NSString*)htmlString baseURL:(NSURL*)baseURL'. The htmlString is a string we are getting out of a html-file. The baseURL is the bundlePath. A NSBundle object represents a location in the file system that groups code and resources that can be used in a program. Resources (images, css- and javaScript-files) inside your application bundle are at the root of the bundle, even if you place them in an separate group (folder) in your project.
=== Finally we add the following UIWebView-Delegate method and make it look like this: ===
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
_backBarButton.enabled = YES;
}
return YES;
}
If a link in the webView is clicked, this method enables the barButtonItem to make it work and possible to go back to the index.html, the first site we will load.
So now we need to go to the Interface-Builder or Storyboard and add a UIWebView and UIBarButtonItem. If you are not using Storyboard it is enough to add a UINavigationBar and on it place the UIBarButtonItem. If you are working with Storyboard add a UINavigationController and make the ViewController the initial(RootViewController) ViewController of the UINavigationController. This will cause that a UINavigationBar gets added to the ViewController. Add the UIBarButtonItem on it.
We created two IBOutlets(webView and backBarButton), connect them with the objects we just added to the view. Do not forget to connect the delegate of the UIWebView to the ViewController.\\
{{:objective-c:examples:bildschirmfoto_2014-08-19_um_01.34.49.png}}\\
The last thing to do is add some resources.\\
Add an image. Mine is vmax.jpg.\\
{{:objective-c:examples:vmax.jpg}}\\
Right-click or ctrl-click on a group or folder. In the pop-up click new file. Select iOS:Other->Empty and create and name like the following. Repeat and copy-paste below code to each file.
=== index.html ===
UIWebView Sample