forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDIWebViewController.m
224 lines (162 loc) · 7.4 KB
/
CDIWebViewController.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// CDIWebViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 7/28/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDIWebViewController.h"
#import "CDIHUDView.h"
#import "UIColor+CheddariOSAdditions.h"
@interface CDIWebViewController ()
- (void)_updateBrowserUI;
@end
@implementation CDIWebViewController {
NSURL *_url;
UIActivityIndicatorView *_indicator;
UIBarButtonItem *_backBarButton;
UIBarButtonItem *_forwardBarButton;
}
@synthesize webView = _webView;
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
_indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 22.0f, 22.0f)];
_indicator.hidesWhenStopped = YES;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_indicator];
_webView = [[SSWebView alloc] initWithFrame:self.view.bounds];
_webView.backgroundColor = [UIColor cheddarArchesColor];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_webView.delegate = self;
_webView.scalesPageToFit = YES;
[_webView loadURL:_url];
[self.view addSubview:_webView];
_backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-button"] landscapeImagePhone:[UIImage imageNamed:@"back-button-mini"] style:UIBarButtonItemStylePlain target:_webView action:@selector(goBack)];
_forwardBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward-button"] landscapeImagePhone:[UIImage imageNamed:@"forward-button-mini"] style:UIBarButtonItemStylePlain target:_webView action:@selector(goForward)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.navigationController.toolbar.tintColor = [UIColor colorWithRed:0.369f green:0.392f blue:0.447f alpha:1.0f];
self.toolbarItems = [NSArray arrayWithObjects:
_backBarButton,
flexibleSpace,
_forwardBarButton,
flexibleSpace,
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reload-button"] landscapeImagePhone:[UIImage imageNamed:@"reload-button-mini"] style:UIBarButtonItemStylePlain target:_webView action:@selector(reload)],
flexibleSpace,
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"safari-button"] landscapeImagePhone:[UIImage imageNamed:@"safari-button-mini"] style:UIBarButtonItemStylePlain target:self action:@selector(openSafari:)],
flexibleSpace,
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"action-button"] landscapeImagePhone:[UIImage imageNamed:@"action-button-mini"] style:UIBarButtonItemStylePlain target:self action:@selector(openActionSheet:)],
nil];
for (UIBarButtonItem *button in self.toolbarItems) {
button.imageInsets = UIEdgeInsetsMake(3.0f, 0.0f, 0.0f, 0.0f);
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(close:)];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (![_url isFileURL]) {
[self.navigationController setToolbarHidden:NO animated:animated];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setToolbarHidden:YES animated:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
#pragma mark - URL Loading
- (void)loadURL:(NSURL *)url {
_url = url;
}
- (NSURL *)currentURL {
NSURL *url = _webView.lastRequest.mainDocumentURL;
if (!url) {
url = _url;
}
return url;
}
#pragma mark - Actions
- (void)close:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
- (void)openSafari:(id)sender {
[[UIApplication sharedApplication] openURL:self.currentURL];
}
- (void)openActionSheet:(id)sender {
UIActionSheet *actionSheet = nil;
if ([MFMailComposeViewController canSendMail] == NO) {
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Copy URL", nil];
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Copy URL", @"Email URL", nil];
}
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.navigationController.view];
}
- (void)copyURL:(id)sender {
[[UIPasteboard generalPasteboard] setURL:self.currentURL];
}
- (void)emailURL:(id)sender {
if ([MFMailComposeViewController canSendMail] == NO) {
return;
}
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
viewController.subject = self.title;
viewController.mailComposeDelegate = self;
[viewController setMessageBody:_webView.lastRequest.mainDocumentURL.absoluteString isHTML:NO];
[self.navigationController presentModalViewController:viewController animated:YES];
}
#pragma mark - Private
- (void)_updateBrowserUI {
_backBarButton.enabled = [_webView canGoBack];
_forwardBarButton.enabled = [_webView canGoForward];
UIBarButtonItem *reloadButton = nil;
if ([_webView isLoadingPage]) {
[_indicator startAnimating];
reloadButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"stop-button"] landscapeImagePhone:[UIImage imageNamed:@"stop-button-mini"] style:UIBarButtonItemStylePlain target:_webView action:@selector(stopLoading)];
} else {
[_indicator stopAnimating];
reloadButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reload-button"] landscapeImagePhone:[UIImage imageNamed:@"reload-button-mini"] style:UIBarButtonItemStylePlain target:_webView action:@selector(reload)];
}
reloadButton.imageInsets = UIEdgeInsetsMake(3.0f, 0.0f, 0.0f, 0.0f);
NSMutableArray *items = [self.toolbarItems mutableCopy];
[items replaceObjectAtIndex:4 withObject:reloadButton];
self.toolbarItems = items;
}
#pragma mark - SSWebViewDelegate
- (void)webViewDidStartLoadingPage:(SSWebView *)aWebView {
NSURL *url = _webView.lastRequest.mainDocumentURL;
self.title = url.absoluteString;
[self _updateBrowserUI];
[self.navigationController setToolbarHidden:[url isFileURL] animated:YES];
}
- (void)webViewDidLoadDOM:(SSWebView *)aWebView {
NSString *title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
if (title && title.length > 0) {
self.title = title;
}
}
- (void)webViewDidFinishLoadingPage:(SSWebView *)aWebView {
[self _updateBrowserUI];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self copyURL:actionSheet];
} else if (buttonIndex == 1 && [MFMailComposeViewController canSendMail]) {
[self emailURL:actionSheet];
}
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissModalViewControllerAnimated:YES];
if (result == MFMailComposeResultSent) {
CDIHUDView *hud = [[CDIHUDView alloc] init];
[hud completeQuicklyWithTitle:@"Sent!"];
}
}
@end