-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAWebViewController.m
162 lines (124 loc) · 5.33 KB
/
SAWebViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// SAWebViewController.m
// EditMyFields
//
// Created by SCOTT ASHMORE on 13/02/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "SAWebViewController.h"
@implementation SAWebViewController
@synthesize myWebView;
@synthesize myRow;
@synthesize loadingIndicator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
loadingIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-10, (self.view.frame.size.height/2)-10, 20,20)];
[loadingIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[loadingIndicator setHidesWhenStopped:YES];
[myWebView addSubview:loadingIndicator];
[myWebView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
myWebView.scalesPageToFit = YES;
NSURL *url = [NSURL URLWithString:myRow.myData];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:urlRequest];
UIBarButtonItem *backButton = [self barItemWithImage:[UIImage imageNamed:@"webViewBack.png"]
highlighted:[UIImage imageNamed:@"backButtonHighlighted.png"]
disabled:[UIImage imageNamed:@"backButtonDisabled.png"]
target:self
action:@selector(goBack)];
UIBarButtonItem *forwardButton = [self barItemWithImage:[UIImage imageNamed:@"webViewForward.png"]
highlighted:[UIImage imageNamed:@"forwardButtonHighlighted.png"]
disabled:[UIImage imageNamed:@"forwardButtonDisabled.png"]
target:self
action:@selector(goForward)];
UIBarButtonItem *fixedButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedButton.width = 10;
backButton.enabled = NO;
forwardButton.enabled = NO;
NSMutableArray *buttonItems = [NSMutableArray array];
[buttonItems addObject:fixedButton];
[buttonItems addObject:forwardButton];
[buttonItems addObject:backButton];
[self.navigationItem setRightBarButtonItems:buttonItems];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
myWebView = nil;
loadingIndicator = nil;
}
- (void)goBack
{
if (myWebView.canGoBack)
[myWebView goBack];
}
- (void)goForward
{
if (myWebView.canGoForward)
[myWebView goForward];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void) webViewDidStartLoad:(UIWebView *)webView
{
[loadingIndicator startAnimating];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView
{
[loadingIndicator stopAnimating];
UIBarButtonItem *itemGoBack = [self.navigationItem.rightBarButtonItems objectAtIndex:2];
UIBarButtonItem *itemGoForward = [self.navigationItem.rightBarButtonItems objectAtIndex:1];
if (!myWebView.canGoBack)
itemGoBack.enabled = NO;
else
itemGoBack.enabled = YES;
if (!myWebView.canGoForward)
itemGoForward.enabled = NO;
else
itemGoForward.enabled = YES;
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Webpage failed to load"
message:@"Please check you have network availability"
delegate:self
cancelButtonTitle: @"Cancel"
otherButtonTitles:nil];
[alert show];
}
- (UIBarButtonItem*)barItemWithImage:(UIImage*)image highlighted:(UIImage *)highlight disabled:(UIImage *)disable target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:highlight forState:UIControlStateHighlighted];
[button setBackgroundImage:disable forState:UIControlStateDisabled];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[v addSubview:button];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:v];
return item;
}
@end