-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathSWBViewController.m
631 lines (528 loc) · 22.2 KB
/
SWBViewController.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//
// SWBViewController.m
// ShadowWeb
//
// Created by clowwindy on 2/16/13.
// Copyright (c) 2013 clowwindy. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "QRCodeViewController.h"
#import "SWBViewController.h"
#import "ShadowsocksRunner.h"
#import "ProxySettingsTableViewController.h"
#import "SWBAboutController.h"
#define kNewTabAddress @"shadowweb:newtab"
#define kAboutBlank @"shadowweb:blank"
@interface SWBViewController () {
AVAudioPlayer *player;
UIPopoverController *settingsPC;
UIBarButtonItem *actionBarButton;
}
@end
@implementation SWBViewController
#pragma mark - View lifecycle
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleDefault;
}
- (UIRectEdge) edgesForExtendedLayout {
return UIRectEdgeNone;
}
- (CGFloat) statusBarHeight {
return ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) ?
20 : 0;
}
- (void)viewDidLoad {
[super viewDidLoad];
// If don't do this, you'll see some white edge when doing the rotation
self.view.clipsToBounds = YES;
self.view.backgroundColor = [UIColor whiteColor];
currentTabTag = 0;
CGRect bounds = self.view.bounds;
self.tabBar = [[SWBTabBarView alloc] initWithFrame:CGRectMake(0, bounds.size.height - kTabBarHeight, bounds.size.width, kTabBarHeight)];
self.webViewContainer = [[SWBWebViewContainer alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height - kTabBarHeight)];
// _webViewContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_webViewContainer.delegate = self;
self.tabBar.delegate = self;
self.webViewContainer.delegate = self;
[self initPageManager];
[self initPagesAndTabs];
// init address bar
self.addrbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [self statusBarHeight], bounds.size.width, kToolBarHeight)];
// init bar buttons
CGRect urlFieldFrame = CGRectInset(_addrbar.bounds, 12 + kActionButtonWidth * 0.5f, 7);
urlFieldFrame = CGRectOffset(urlFieldFrame, -kActionButtonWidth * 0.5f, 0);
self.urlField = [[UITextField alloc] initWithFrame:urlFieldFrame];
[_urlField setBorderStyle:UITextBorderStyleRoundedRect];
[_urlField setKeyboardType:UIKeyboardTypeURL];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:_urlField];
[item setStyle:UIBarButtonItemStylePlain];
[_urlField setReturnKeyType:UIReturnKeyGo];
[_urlField setDelegate:self];
[_urlField addTarget:self action:@selector(textFieldDidEndEditing) forControlEvents:UIControlEventEditingDidEndOnExit];
[_urlField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_urlField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[_urlField setAutocorrectionType:UITextAutocorrectionTypeNo];
[_urlField setClearButtonMode:UITextFieldViewModeWhileEditing];
[_urlField setPlaceholder:@"URL"];
[_urlField setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
UIBarButtonItem *_cancelButton = [[UIBarButtonItem alloc] initWithTitle:_L(Cancel) style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
_cancelButton.width = kCancelButtonWidth;
UIBarButtonItem *_actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(addrBarViewMoreDidClick)];
_actionButton.width = kActionButtonWidth;
actionBarButton = _actionButton;
self.addrItemsInactive = [NSMutableArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithCustomView:_urlField],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
_actionButton,
nil];
self.addrItemsActive = [NSMutableArray arrayWithObjects:
[self.addrItemsInactive objectAtIndex:0],
_cancelButton,
nil];
[_addrbar setItems:_addrItemsInactive];
// [_addrbar setBarStyle:UIBarStyleBlack];
if ([_addrbar respondsToSelector:@selector(setBarTintColor:)]) {
[_addrbar setBarTintColor:[UIColor whiteColor]];
}
// add subviews
[self.view addSubview:_webViewContainer];
[self.view addSubview:_addrbar];
[self.view addSubview:_tabBar];
[self relayout:bounds];
// Keyboard hide notification
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardHiden:)
name:UIKeyboardWillHideNotification
object:nil];
// ActionSheet
[self initActionSheet];
// [self play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[_webViewContainer releaseBackgroundWebViews];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self scrollViewDidScroll:[self currentWebView].scrollView];
}
- (void)viewWillLayoutSubviews {
[self relayout:self.view.bounds];
}
- (void)relayout:(CGRect)bounds {
CGRect addrBarRect = CGRectMake(0, _addrbar.frame.origin.y, bounds.size.width, kToolBarHeight);
CGRect webViewContainerRect = CGRectMake(0, [self statusBarHeight], bounds.size.width, bounds.size.height - kTabBarHeight - [self statusBarHeight]);
CGRect tabBarRect = CGRectMake(0, bounds.size.height - kTabBarHeight, bounds.size.width, kTabBarHeight);
_addrbar.frame = addrBarRect;
_webViewContainer.frame = webViewContainerRect;
_tabBar.frame = tabBarRect;
}
- (void)viewDidAppear:(BOOL)animated {
if ([ShadowsocksRunner settingsAreNotComplete]) {
[self showSettings];
}
}
#pragma mark - webview
- (void)updateWebViewTitle:(UIWebView *)webView {
NSString *title = [_webViewContainer titleForWebView:(SWBWebView *) webView];
SWBPage *page = [_pageManager pageByTag:webView.tag];
if (title) {
page.title = title;
// [visitRecordManager setTitleForURL:[(AQWebView *)webView aqLocationHref] title:title];
}
if (title == nil || [title isEqualToString:@""]) {
if (webView.loading) {
title = NSLocalizedString(@"Loading", "Loading");
} else {
title = NSLocalizedString(@"Untitled", "Untitled");
}
}
[_tabBar setTitleForTab:webView.tag title:title];
}
- (void)openURL:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
if (!url || [urlString rangeOfString:@":"].length == 0) {
urlString = [@"http://" stringByAppendingString:urlString];
url = [NSURL URLWithString:urlString];
}
if (url) {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
if (request) {
[_webViewContainer.currentSWBWebView loadRequest:request];
}
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:_L(incorrect
URL) delegate:nil cancelButtonTitle:_L(OK) otherButtonTitles:nil];
[alert show];
}
}
- (SWBWebView *)currentWebView {
return [_webViewContainer currentSWBWebView];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
BOOL result = YES;
#ifdef DEBUG
// NSLog(@"shouldStartLoadWithRequest tag:%d navtype:%d %@", webView.tag, navigationType, [request URL]);
#endif
// NSString *scheme = [[request URL] scheme];
if ([[[request URL] absoluteString] caseInsensitiveCompare:kNewTabAddress] == NSOrderedSame) {
// [self openLinkInNewTab:[(SWBWebView *)webView lastClickedLink]];
}
else {
if (navigationType == UIWebViewNavigationTypeLinkClicked ||
navigationType == UIWebViewNavigationTypeBackForward) {
SWBPage *page = [_pageManager pageByTag:webView.tag];
NSString *url = [[request URL] absoluteString];
page.url = url;
}
}
return result;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self resetTabBarButtonsStatus];
[self updateWebViewTitle:webView];
[_tabBar setLoadingForTab:webView.tag loading:NO];
if ([[[[webView request] URL] absoluteString] caseInsensitiveCompare:[(SWBWebView *) webView locationHref]] == NSOrderedSame) {
NSString *url = [[[webView request] URL] absoluteString];
NSString *title = [((SWBWebView *) webView) pageTitle];
_urlField.text = url;
SWBPage *page = [_pageManager pageByTag:webView.tag];
page.url = url;
page.title = title;
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[_tabBar setLoadingForTab:webView.tag loading:YES];
}
#pragma mark - WebView Scrolling
- (void)initWebViewScrolling:(SWBWebView *)webView {
UIScrollView *scrollView = webView.scrollView;
scrollView.delegate = self;
[scrollView setContentInset:UIEdgeInsetsMake(kToolBarHeight, 0, 0, 0)];
[scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(kToolBarHeight, 0, 0, 0)];
[scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
[self scrollViewDidScroll:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self currentWebView].scrollView == scrollView) {
if (scrollView.contentOffset.y < 0) {
[scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0)];
} else {
[scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}
_addrbar.frame = CGRectMake(0, [self statusBarHeight] - kToolBarHeight - scrollView.contentOffset.y, _addrbar.frame.size.width, kToolBarHeight);
CGFloat offset = MIN(kToolBarHeight, MAX(0, (kToolBarHeight + scrollView.contentOffset.y)));
CGFloat opacity = offset / kToolBarHeight;
_urlField.alpha = (1 - opacity)*(1 - opacity);
// NSLog(@"offset: %f, contentInset top: %f", offset, scrollView.contentInset.top);
[scrollView setContentInset:UIEdgeInsetsMake(kToolBarHeight - offset, 0, 0, 0)];
}
}
#pragma mark - ActionSheet
- (void)initActionSheet {
self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:_L(Cancel) destructiveButtonTitle:nil otherButtonTitles:_L(New
Tab), _L(Back), _L(Forward), _L(Reload), _L(Settings), _L(Config via QRCode), _L(Help), _L(About), nil];
[_actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
QRCodeViewController *qrCodeViewController = [[QRCodeViewController alloc] initWithReturnBlock:^(NSString *code) {
if (code) {
NSURL *URL = [NSURL URLWithString:code];
if (URL) {
[[UIApplication sharedApplication] openURL:URL];
}
}
}];
switch (buttonIndex) {
case 0:
[self openLinkInNewTab:kNewTabAddress];
_urlField.text = @"";
[NSTimer scheduledTimerWithTimeInterval:0.20 target:_urlField selector:@selector(becomeFirstResponder) userInfo:nil repeats:NO];
break;
case 1:
[[self currentWebView] goBack];
break;
case 2:
[[self currentWebView] goForward];
break;
case 3:
[[self currentWebView] reload];
break;
case 4:
[self showSettings];
break;
case 5:
[self presentModalViewController:qrCodeViewController animated:YES];
break;
case 6:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://github.com/shadowsocks/shadowsocks-iOS/wiki/Help"]];
break;
case 7:
[self showAbout];
break;
default:
break;
}
}
- (void)showAbout {
SWBAboutController *settingsController = [[SWBAboutController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:settingsController];
// nav.navigationBar.tintColor = [UIColor blackColor];
// nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
settingsPC = [[UIPopoverController alloc] initWithContentViewController:nav];
settingsController.myPopoverController = settingsPC;
CGRect newTabRect = [self.tabBar aNewTabButton].frame;
newTabRect.size.width = newTabRect.size.height;
CGRect rect = [self.tabBar convertRect:newTabRect toView:self.view];
// [settingsPC presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[settingsPC presentPopoverFromBarButtonItem:actionBarButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentModalViewController:nav animated:YES];
}
}
- (void)showSettings {
ProxySettingsTableViewController *settingsController = [[ProxySettingsTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:settingsController];
// nav.navigationBar.tintColor = [UIColor blackColor];
// nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
settingsPC = [[UIPopoverController alloc] initWithContentViewController:nav];
settingsController.myPopoverController = settingsPC;
CGRect newTabRect = [self.tabBar aNewTabButton].frame;
newTabRect.size.width = newTabRect.size.height;
CGRect rect = [self.tabBar convertRect:newTabRect toView:self.view];
// [settingsPC presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[settingsPC presentPopoverFromBarButtonItem:actionBarButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentModalViewController:nav animated:YES];
}
}
#pragma mark - TabBar
- (void)resetTabBarButtonsStatus {
// SWBWebView *currentWebView = [self currentWebView];
// backButton.enabled = currentWebView.canGoBack;
// forwardButton.enabled = currentWebView.canGoForward;
// stopButton.enabled = currentWebView.loading;
// @try {
// NSString *urlString = [[self currentWebView] aqLocationHref];
// NSURL *url = [NSURL URLWithString:urlString];
// if (AQ_is_nonlocal_http([url scheme])) {
// actionButton.enabled = YES;
// // refreshButton.enabled = YES;
// } else {
// actionButton.enabled = NO;
// // refreshButton.enabled = NO;
// }
// }
// @catch (NSException *exception) {
// }
// @finally {
// }
}
- (void)tabBarViewNewTabButtonDidClick {
[self openLinkInNewTab:kNewTabAddress];
_urlField.text = @"";
[NSTimer scheduledTimerWithTimeInterval:0.20 target:_urlField selector:@selector(becomeFirstResponder) userInfo:nil repeats:NO];
}
- (void)addrBarViewMoreDidClick {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGRect newTabRect = [self.tabBar aNewTabButton].frame;
newTabRect.size.width = newTabRect.size.height;
CGRect rect = [self.tabBar convertRect:newTabRect toView:self.view];
// [self.actionSheet showFromRect:rect inView:self.view animated:YES];
[self.actionSheet showFromBarButtonItem:actionBarButton animated:YES];
} else {
[self.actionSheet showInView:self.view];
}
}
- (void)tabBarViewTabDidClose:(SWBTab *)tab {
// NSString *title = [[_webViewContainer webViewByTag:tab.tag] pageTitle];
// NSString *urlString =[[_pageManager pageByTag:tab.tag] url];
// if (urlString) {
// NSURL *url = [[NSURL alloc] initWithString:urlString];
// if (AQ_is_nonlocal_http([url scheme])) {
// [visitRecordManager closeAURL:urlString withTitle:title];
// }
// [url release];
// }
[_pageManager removePage:tab.tag];
[_webViewContainer closeWebView:tab.tag];
if (_tabBar.currentTab) {
[_webViewContainer switchToWebView:_tabBar.currentTab.tag];
}
if ([[_pageManager pages] count] == 0) {
// [self tabBarViewNewTabButtonDidClick];
[self openLinkInNewTab:@"http://www.twitter.com/"];
[NSTimer scheduledTimerWithTimeInterval:0.20 target:_urlField selector:@selector(becomeFirstResponder) userInfo:nil repeats:NO];
}
}
- (void)tabBarViewTabDidMove:(SWBTab *)tab toIndex:(NSInteger)index {
// [pageManager reorderPage:tabBar.tabs];
// 目前没有必要
}
- (void)tabBarViewTabDidSelect:(SWBTab *)tab {
[_webViewContainer switchToWebView:tab.tag];
}
#pragma mark - page manager
- (void)initPageManager {
lastTag = 0;
self.pageManager = [[SWBPageManager alloc] init];
[_pageManager load];
}
- (NSInteger)genTag {
return lastTag++;
}
- (void)initPagesAndTabs {
[_pageManager initMappingAndTabsByPages];
NSArray *pages = _pageManager.pages;
// NSInteger currentTabTag = 0;
NSUInteger count = [pages count];
for (int i = 0; i < count; i++) {
if ([[[pages objectAtIndex:i] selected] boolValue]) {
currentTabTag = i;
}
}
for (int i = 0; i < count; i++) {
[self addNewTab];
}
if ([pages count] == 0) {
// [self tabBarViewNewTabButtonDidClick];
[self openLinkInNewTab:@"http://www.google.com/"];
[NSTimer scheduledTimerWithTimeInterval:0.20 target:_urlField selector:@selector(becomeFirstResponder) userInfo:nil repeats:NO];
} else {
[_tabBar setCurrentTabWithTag:currentTabTag];
}
}
- (void)savePageIndex {
// save page index order
// NSArray *tabs = [tabBar tabs];
// for (int i = 0; i < [tabs count]; i++) {
// [[pageManager pageByTag:[[tabs objectAtIndex:i] tag]] setIndex:[NSNumber numberWithInt:i]];
// }
}
- (void)addNewEmptyTab {
NSInteger newTag = [self genTag];
[_webViewContainer newWebView:newTag];
}
- (void)addNewTab {
SWBTab *newTab = [_tabBar newTab];
[_webViewContainer newWebView:newTab.tag];
_tabBar.currentTab = newTab;
}
- (void)openLinkInNewTab:(NSString *)urlString {
SWBTab *newTab = [_tabBar newTab];
SWBPage *page = [_pageManager addPageWithTag:newTab.tag];
page.url = urlString;
page.title = @"";
[_webViewContainer newWebView:newTab.tag];
_tabBar.currentTab = newTab;
}
- (void)saveData {
[_pageManager save];
}
#pragma mark - WebViewContainer
- (void)syncPageManagerSelectionStatusWithSelectedTag:(NSInteger)tag {
NSNumber *yes = [NSNumber numberWithBool:YES];
NSNumber *no = [NSNumber numberWithBool:NO];
for (SWBPage *page in _pageManager.pages) {
page.selected = no;
}
[_pageManager pageByTag:tag].selected = yes;
}
- (void)webViewContainerWebViewDidCreateNew:(SWBWebView *)webView {
SWBPage *page = [_pageManager pageByTag:webView.tag];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:page.url]];
[webView loadRequest:request];
[self initWebViewScrolling:webView];
[self resetTabBarButtonsStatus];
[self syncPageManagerSelectionStatusWithSelectedTag:webView.tag];
}
- (void)webViewContainerWebViewDidSwitchToWebView:(SWBWebView *)webView {
[self resetTabBarButtonsStatus];
[self syncPageManagerSelectionStatusWithSelectedTag:webView.tag];
_urlField.text = webView.locationHref;
[self scrollViewDidScroll:webView.scrollView];
}
- (void)webViewContainerWebViewNeedToReload:(SWBWebView *)webView tag:(NSInteger)tag {
SWBPage *page = [_pageManager pageByTag:tag];
NSURL *url = [NSURL URLWithString:page.url];
if (url != nil) {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self resetTabBarButtonsStatus];
}
}
#pragma mark - Text Field
- (void)hideKeyboard {
[_urlField resignFirstResponder];
[self hideCancelButton];
}
- (void)hideCancelButton {
[_addrbar setItems:_addrItemsInactive animated:YES];
[UIView beginAnimations:nil context:NULL];
CGRect bounds = [_addrbar bounds];
bounds = CGRectInset(bounds, 12 + kActionButtonWidth * 0.5f, 7);
bounds = CGRectOffset(bounds, -kActionButtonWidth * 0.5f, 0);
[_urlField setFrame:bounds];
[UIView commitAnimations];
}
- (void)cancel {
[self hideKeyboard];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[_addrbar setItems:_addrItemsActive animated:YES];
[UIView beginAnimations:nil context:NULL];
CGRect bounds = [_addrbar bounds];
bounds = CGRectInset(bounds, 12 + kCancelButtonWidth * 0.5f, 7);
bounds = CGRectOffset(bounds, -kCancelButtonWidth * 0.5f, 0);
[_urlField setFrame:bounds];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing {
[self hideKeyboard];
[self openURL:_urlField.text];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (void)keyboardHiden:(NSNotification *)notification {
// [self hideCancelButton];
}
#pragma mark audio
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)play {
// Play music, so app can run in the backgound.
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"silence" withExtension:@"wav"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player prepareToPlay];
[player setVolume:0];
player.numberOfLoops = -1;
[player play];
[self becomeFirstResponder];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[player play];
break;
case UIEventSubtypeRemoteControlPause:
[player pause];
break;
default:
break;
}
}
@end