Skip to content

Commit

Permalink
Adding the toolbar to the WebElement, with back/forward/reload/out to…
Browse files Browse the repository at this point in the history
… safari
  • Loading branch information
escoz committed Aug 31, 2011
1 parent b1d1efd commit 56d4146
Showing 1 changed file with 101 additions and 4 deletions.
105 changes: 101 additions & 4 deletions quickdialog/QWebViewController.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,36 +14,89 @@


#import "QWebViewController.h" #import "QWebViewController.h"


@implementation QWebViewController @interface QWebViewController ()
- (CGImageRef)createBackArrowImageRef;
- (CGImageRef)createForwardArrowImageRef;



@end

@implementation QWebViewController {
UIBarButtonItem * _btBack;
UIBarButtonItem * _btForward;
BOOL _firstPageFinished;
BOOL _previousToolbarState;
}
- (QWebViewController *)initWithUrl:(NSString *)url { - (QWebViewController *)initWithUrl:(NSString *)url {


self = [super init]; self = [super init];
if (self!=nil){ if (self!=nil){
_webView = [[UIWebView alloc] init]; _webView = [[UIWebView alloc] init];
_webView.delegate = self; _webView.delegate = self;
_webView.scalesPageToFit = YES; _webView.scalesPageToFit = YES;

_url = url; _url = url;
self.view = _webView; self.view = _webView;

UIImage *backImage = [[UIImage alloc] initWithCGImage:[self createBackArrowImageRef]];
UIImage *forwardImage = [[UIImage alloc] initWithCGImage:[self createForwardArrowImageRef]];
_btBack = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(actionRewind)];
_btForward = [[UIBarButtonItem alloc] initWithImage:forwardImage style:UIBarButtonItemStylePlain target:self action:@selector(actionForward)];

_btBack.enabled = NO;
_btForward.enabled = NO;
} }
return self; return self;
} }


- (void)viewWillDisappear:(BOOL)animated { - (void)viewWillDisappear:(BOOL)animated {
[_webView stopLoading]; [_webView stopLoading];
[self.navigationController setToolbarHidden:_previousToolbarState animated:YES];
_webView = nil; _webView = nil;
} }



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES; return YES;
} }


- (void)actionRewind {
[_webView goBack];
_btForward.enabled = YES;

}

- (void)actionForward {
[_webView goForward];
}

- (void)actionRefresh {
[_webView reload];
}

- (void)actionGoToSafari {
[[UIApplication sharedApplication] openURL:[_webView.request mainDocumentURL]];
}


- (void)viewWillAppear:(BOOL)animated { - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; [super viewWillAppear:animated];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]]; [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
_previousToolbarState = self.navigationController.toolbarHidden;
self.navigationController.toolbarHidden = NO;


UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer1.width = 30;
UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer2.width = 30;
self.toolbarItems = [NSArray arrayWithObjects:
_btBack,
spacer1,
_btForward,
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(actionRefresh)],
spacer2,
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionGoToSafari)],
nil];

} }


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
Expand All @@ -55,11 +108,15 @@ - (void)webViewDidStartLoad:(UIWebView *)webView {
[indicator startAnimating]; [indicator startAnimating];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:indicator]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:indicator];
self.title = @"Loading"; self.title = @"Loading";
if (_firstPageFinished==YES){
_btBack.enabled = YES;
}
} }


- (void)webViewDidFinishLoad:(UIWebView *)webView { - (void)webViewDidFinishLoad:(UIWebView *)webView {
self.navigationItem.rightBarButtonItem = nil; self.navigationItem.rightBarButtonItem = nil;
self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"]; self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
_firstPageFinished = YES;
} }


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
Expand All @@ -69,4 +126,44 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
} }




- (CGContextRef)createContext
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil,27,27,8,0, colorSpace,kCGImageAlphaPremultipliedLast);
CFRelease(colorSpace);
return context;
}

- (CGImageRef)createBackArrowImageRef
{
CGContextRef context = [self createContext];
CGColorRef fillColor = [[UIColor blackColor] CGColor];
CGContextSetFillColor(context, (CGFloat *) CGColorGetComponents(fillColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 24.0f, 4.0f);
CGContextAddLineToPoint(context, 24.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
return image;
}

- (CGImageRef)createForwardArrowImageRef
{
CGContextRef context = [self createContext];
CGColorRef fillColor = [[UIColor blackColor] CGColor];
CGContextSetFillColor(context, (CGFloat *) CGColorGetComponents(fillColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 24.0f, 13.0f);
CGContextAddLineToPoint(context, 8.0f, 4.0f);
CGContextAddLineToPoint(context, 8.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
return image;
}

@end @end

0 comments on commit 56d4146

Please sign in to comment.