/
WebViewController.m
232 lines (186 loc) · 7.68 KB
/
WebViewController.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
225
226
227
228
229
230
231
232
#import "WebViewController.h"
#import "HatenaTouchAppDelegate.h"
#import "InformationSheetController.h"
#import "JSON/JSON.h"
#import "Debug.h"
#import <objc/runtime.h>
@implementation WebViewController
@synthesize webView;
@synthesize backButton;
@synthesize forwardButton;
@synthesize pageURL;
@synthesize lastPageURL;
static NSObject *webViewcreateWebViewWithRequestIMP(id self, SEL _cmd, NSObject* sender, NSObject* request) {
return [sender retain];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
UIBarButtonItem *commentButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Comment.png"]
style:UIBarButtonItemStyleBordered target:self action:@selector(showInfoMenu)];
[[self navigationItem] setRightBarButtonItem:commentButton];
[commentButton release];
}
return self;
}
- (void)dealloc {
LOG_CURRENT_METHOD;
[lastPageURL release];
[pageURL release];
[forwardButton release];
[backButton release];
[webView setDelegate:nil];
[webView release];
[super dealloc];
}
- (void)_loadPageInfo {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *webServiceURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://b.hatena.ne.jp/entry/jsonlite/%@", pageURL]];
NSURLRequest *req = [NSURLRequest requestWithURL:webServiceURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSData *data;
NSHTTPURLResponse *res;
NSError *error;
data = [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&error];
NSString *json = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
pageInfo = [[json JSONValue] retain];
[[[self navigationItem] rightBarButtonItem] setEnabled:YES];
[pool release];
}
- (void)_loadPageInfo:(id)timer {
NSURL *aURL = [webView.request mainDocumentURL];
if (!aURL) {
return;
}
[NSThread detachNewThreadSelector:@selector(_loadPageInfo) toTarget:self withObject:nil];
[timer invalidate];
}
- (void)showInfoMenu {
isInfoMenuPresent = YES;
InformationSheetController *controller = [[InformationSheetController alloc] initWithNibName:@"InformationSheet" bundle:nil];
controller.pageInfo = pageInfo;
controller.bookmarks = [pageInfo objectForKey:@"bookmarks"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)startLoading {
if ([pageURL isEqualToString:lastPageURL] && loadFinishedSuccesefully) {
return;
}
self.lastPageURL = pageURL;
HatenaTouchAppDelegate *hatenaTouchApp = [HatenaTouchAppDelegate sharedHatenaTouchApp];
UserSettings *userSettings = hatenaTouchApp.userSettings;
NSURL *url;
if (userSettings.useMobileProxy) {
url = [NSURL URLWithString:[[NSString stringWithFormat:@"http://www.google.co.jp/gwt/n?u=%@", pageURL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
} else {
url = [NSURL URLWithString:pageURL];
}
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (IBAction)actionButtonPushed:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:NSLocalizedString(@"ReloadThisPage", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
destructiveButtonTitle:nil
otherButtonTitles:
NSLocalizedString(@"DirectAccess", nil),
NSLocalizedString(@"WithMobileProxy", nil),
NSLocalizedString(@"OpenSafari", nil), nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}
#pragma mark <UIActionSheetDelegate> Methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
LOG(@"action button pushed: %d", buttonIndex);
if (buttonIndex == 0) {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pageURL]]];
} else if (buttonIndex == 1) {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"http://www.google.co.jp/gwt/n?u=%@", pageURL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
} else if (buttonIndex == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:pageURL]];
}
}
#pragma mark <UIWebViewDelegate> Methods
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *aURL = [[request URL] absoluteString];
NSString *mainDocumentURL = [request.mainDocumentURL absoluteString];
if (mainDocumentURL == nil || ![mainDocumentURL isEqualToString:aURL]) {
return NO;
}
if (navigationType == UIWebViewNavigationTypeLinkClicked ||
navigationType == UIWebViewNavigationTypeFormSubmitted ||
navigationType == UIWebViewNavigationTypeBackForward ||
navigationType == UIWebViewNavigationTypeFormResubmitted) {
self.lastPageURL = mainDocumentURL;
}
LOG(@"<%@>", [request URL]);
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)aWebView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
backButton.enabled = (webView.canGoBack) ? YES : NO;
forwardButton.enabled = (webView.canGoForward) ? YES : NO;
LOG_CURRENT_METHOD;
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
loadFinishedSuccesefully = YES;
backButton.enabled = (webView.canGoBack) ? YES : NO;
forwardButton.enabled = (webView.canGoForward) ? YES : NO;
self.title = [aWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
LOG_CURRENT_METHOD;
}
- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
loadFinishedSuccesefully = NO;
backButton.enabled = (webView.canGoBack) ? YES : NO;
forwardButton.enabled = (webView.canGoForward) ? YES : NO;
LOG_CURRENT_METHOD;
}
#pragma mark <UIViewController> Methods
- (void)viewDidLoad {
[super viewDidLoad];
[webView setBackgroundColor:[UIColor whiteColor]];
Class UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate");
class_addMethod(UIWebViewWebViewDelegate, @selector(webView:createWebViewWithRequest:),
(IMP)webViewcreateWebViewWithRequestIMP, "@@:@@");
}
- (void)viewWillAppear:(BOOL)animated {
if (isInfoMenuPresent) {
isInfoMenuPresent = NO;
return;
}
[super viewWillAppear:animated];
backButton.enabled = (webView.canGoBack) ? true : false;
forwardButton.enabled = (webView.canGoForward) ? true : false;
[self startLoading];
[[[self navigationItem] rightBarButtonItem] setEnabled:NO];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(_loadPageInfo:) userInfo:nil repeats:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
if (isInfoMenuPresent) {
return;
}
[super viewWillDisappear:animated];
[webView stopLoading];
[pageInfo release];
pageInfo = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if (fromInterfaceOrientation == UIDeviceOrientationIsPortrait(fromInterfaceOrientation)) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
HatenaTouchAppDelegate *hatenaTouchApp = [HatenaTouchAppDelegate sharedHatenaTouchApp];
UserSettings *userSettings = hatenaTouchApp.userSettings;
return userSettings.shouldAutoRotation;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end