-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniWebView.mm
953 lines (793 loc) · 35.9 KB
/
UniWebView.mm
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//
// UniWebView.mm
// UniWebView
//
// Created by 王 巍 on 13-9-22.
// Copyright (c) 2013年 OneV's Den. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "iPhone_Common.h"
#import "iPhone_OrientationSupport.h"
#import "BookmarkViewController.h"
#import "PassValueDelegate.h"
#import "SqlService.h"
#import "MenuData.h"
#import <ShareSDK/ShareSDK.h>
extern UIViewController *UnityGetGLViewController();
extern ScreenOrientation UnityCurrentOrientation();
extern NSString * const kUnityViewWillRotate;
NSString * const UWVPlayerWillExitFullScreenNotification = @"UIMoviePlayerControllerWillExitFullscreenNotification";
NSString * const UWVPlayerDidEnterFullScreenNotification = @"UIMoviePlayerControllerDidEnterFullscreenNotification";
extern "C" void UnitySendMessage(const char *, const char *, const char *);
@interface UniWebViewToolBar : UIToolbar
@property (nonatomic, retain) UIBarButtonItem *btnNext;
@property (nonatomic, retain) UIBarButtonItem *btnBack;
@property (nonatomic, retain) UIBarButtonItem *btnReload;
@property (nonatomic, retain) UIBarButtonItem *btnDone;
@end
@implementation UniWebViewToolBar
-(void)dealloc {
[_btnNext release];
[_btnBack release];
[_btnReload release];
[_btnDone release];
[super dealloc];
}
@end
@interface UniWebViewTopToolBar : UIToolbar
@property (nonatomic, retain) UIBarButtonItem *btnAddfav;
@end
@implementation UniWebViewTopToolBar
-(void)dealloc {
[_btnAddfav release];
[super dealloc];
}
@end
@interface UniWebSpinner : UIView
@property (nonatomic, retain) UIActivityIndicatorView *indicator;
@property (nonatomic, retain) UILabel *textLabel;
-(id) initWithFrame:(CGRect)frame;
-(void) show;
-(void) hide;
@end
@implementation UniWebSpinner
-(id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
self.clipsToBounds = YES;
self.layer.cornerRadius = 10.0;
_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
_indicator.frame = (CGRect){ frame.size.width / 2 - _indicator.frame.size.width / 2,
frame.size.height / 2 - _indicator.frame.size.height / 2 - 10,
_indicator.bounds.size.width,
_indicator.bounds.size.height};
[self addSubview:_indicator];
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 22 * 2, frame.size.width, 22)];
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.textColor = [UIColor whiteColor];
_textLabel.adjustsFontSizeToFitWidth = YES;
_textLabel.textAlignment = UITextAlignmentCenter;
_textLabel.text = @"Loading...";
[self addSubview:_textLabel];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[self addGestureRecognizer:tap];
[tap release];
}
return self;
}
-(void) show {
self.hidden = NO;
[self.indicator startAnimating];
}
-(void) hide {
[self.indicator stopAnimating];
self.hidden = YES;
}
-(void)dealloc {
[_textLabel release];
[_indicator release];
[super dealloc];
}
@end
@class UniWebView;
@interface UniWebViewManager : NSObject
+ (UniWebViewManager *) sharedManager;
- (void)webViewDone:(UniWebView *)webView;
@end
@interface UniWebView : UIWebView<PassValueDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UniWebViewToolBar *toolBar;
@property (nonatomic, retain) UniWebViewTopToolBar *topToolBar;
@property (nonatomic, retain) UniWebSpinner *spinner;
@property (nonatomic, assign) UIEdgeInsets insets;
@property (nonatomic, assign) BOOL showSpinnerWhenLoading;
@property (nonatomic, copy) NSString *currentUrl;
@property (nonatomic, copy) NSString *currentTitle;
@property (nonatomic, retain) NSMutableArray *schemes;
@property (nonatomic, retain) UIView *backgroundView;
@property (nonatomic, retain) UITableView *menuTableView;
-(id) initWithFrame:(CGRect)frame;
-(void) btnDonePressed:(id)sender;
-(void) updateToolBtn;
-(void) changeToInsets:(UIEdgeInsets)insets targetOrientation:(ScreenOrientation)orientation;
-(void) setBounces:(BOOL)bounces;
-(void) goBookmarks;
@end
@implementation UniWebView
-(id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGRect toolBarFrame = CGRectMake(0, frame.size.height - 44, frame.size.width, 44);
CGRect topToolBarFrame = CGRectMake(0, 0, frame.size.width, 44);
_toolBar = ({
UniWebViewToolBar *toolBar = [[UniWebViewToolBar alloc] initWithFrame:toolBarFrame];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-tool-backward-white.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-tool-forward-white.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goForward)];
UIBarButtonItem *reload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnReloadPressed:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-tool-home-white.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnDonePressed:)];
toolBar.items = @[back,space,forward,space,reload,space,done];
toolBar.btnBack = back;
toolBar.btnNext = forward;
toolBar.btnReload = reload;
toolBar.btnDone = done;
toolBar.hidden = YES;
[back release];
[forward release];
[reload release];
[done release];
[space release];
toolBar.barTintColor = [UIColor colorWithWhite:0.9f alpha:0.1f];
toolBar.tintColor = [UIColor colorWithWhite:0.2f alpha:0.75f];
toolBar;
});
_topToolBar = ({
UniWebViewTopToolBar *topToolBar = [[UniWebViewTopToolBar alloc] initWithFrame:topToolBarFrame];
UIBarButtonItem *addfav = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu-50.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnAddfavPressed:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
topToolBar.items = @[space, addfav];
topToolBar.btnAddfav = addfav;
[addfav release];
topToolBar.barTintColor = [UIColor colorWithWhite:0.9f alpha:0.1f];
topToolBar.tintColor = [UIColor colorWithWhite:0.2f alpha:0.75f];
topToolBar;
});
_schemes = [[NSMutableArray alloc] initWithObjects:@"uniwebview", nil];
_showSpinnerWhenLoading = YES;
_spinner = ({
UniWebSpinner *spinner = [[UniWebSpinner alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 65, frame.size.height / 2 - 65, 130, 130)];
//[spinner hide];
[[spinner indicator] startAnimating];
spinner;
});
[self setBounces:NO];
[self updateToolBtn];
[self topToolBar].btnAddfav.enabled = NO;
_backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
_backgroundView.opaque = NO;
UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
[_backgroundView addGestureRecognizer:gesture];
MenuData *menuData = [[MenuData alloc] init];
menuData.delegate = self;
_menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2, 44, 0, 0)
style:UITableViewStylePlain];
_menuTableView.rowHeight = TableViewCellHeight;
_menuTableView.separatorColor = [UIColor colorWithRed:220.f/255.0f green:220.f/255.0f blue:220.f/255.0f alpha:1.0];
_menuTableView.dataSource = menuData;
_menuTableView.delegate = menuData;
_menuTableView.autoresizesSubviews = NO;
self.scrollView.delegate = self;
}
return self;
}
-(void)setBounces:(BOOL)bounces {
UIScrollView* sv = nil;
for(UIView* view in self.subviews){
if([view isKindOfClass:[UIScrollView class]]){
sv = (UIScrollView*)view;
sv.bounces = bounces;
}
}
}
-(void) btnDonePressed:(id)sender {
[[UniWebViewManager sharedManager] webViewDone:self];
}
-(void) btnReloadPressed:(id)sender {
if (!self.loading) {
[self reload];
} else {
NSLog(@"UniWebView can not reload because some content is being loading right now.");
}
}
- (void) passValue:(NSString *)url {
NSLog(@"passValue url: %@!!!", url);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[self loadRequest:request];
}
- (void) passTappedBtn:(NSInteger)num {
if (self.menuTableView) {
NSLog(@"Tapped Gesture!!!!!");
[self.menuTableView removeFromSuperview];
[self.backgroundView removeFromSuperview];
}
switch (num) {
case 0:
{
NSLog(@"item0 tapped!!!");
UIViewController *unityViewController = UnityGetGLViewController();
BookmarkViewController *bookmarkController = [[BookmarkViewController alloc] init];
bookmarkController.delegate = self;
[unityViewController presentViewController:[[UINavigationController alloc] initWithRootViewController:bookmarkController] animated:YES completion:nil];
}
break;
case 1:
{
NSLog(@"item1 tapped!!!");
NSLog(@"add favorite: %@, %@", [self currentTitle], [self currentUrl]);
SqlService *sqlSer = [[SqlService alloc] init];
if ([sqlSer multiplyBM:[self currentUrl]]) {
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setMessage:@"已存在相同书签!"];
[alert addButtonWithTitle:@"确定"];
[alert show];
[alert release];
} else {
BookmarkItem *item = [[BookmarkItem alloc] init];
item.bName = [self currentTitle];
item.bURL = [self currentUrl];
if ([sqlSer insertBM:item]) {
NSLog(@"!!!!!!!!");
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setMessage:@"收藏成功!"];
[alert addButtonWithTitle:@"确定"];
[alert show];
[alert release];
}
[item release];
}
}
break;
case 2:
{
NSLog(@"item2 tapped!!!");
id<ISSContent> publishContent = [ShareSDK content:@"Shareing content"
defaultContent:@"testing!!"
image:[ShareSDK imageWithUrl:@"http://img.baidu.com/img/image/zhenrenmeinv0207.jpg"]
title:@"Hahahaha"
url:@"http://www.baidu.com"
description:@"hello!!"
mediaType:SSPublishContentMediaTypeNews];
id<ISSContainer> container = [ShareSDK container];
[ShareSDK showShareActionSheet:container
shareList:nil
content:publishContent
statusBarTips:YES
authOptions:nil
shareOptions:nil
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (state == SSResponseStateSuccess) {
NSLog(@"分享成功!!!!!");
} else if (state == SSResponseStateFail) {
NSLog(@"分享失败");
}
}];
}
break;
default:
break;
}
}
- (void) btnAddfavPressed:(id)sender {
[UnityGetGLViewController().view addSubview:self.backgroundView];
[UnityGetGLViewController().view addSubview:self.menuTableView];
NSLog(@"addsubview!!!!!!");
[UIView animateWithDuration:0.2 animations:^{
if (self.menuTableView) {
self.menuTableView.frame = CGRectMake(self.frame.size.width / 2, 44, self.frame.size.width / 2, 3 * TableViewCellHeight);
}
}];
}
- (void)backgroundTapped:(UITapGestureRecognizer *)paramSender
{
if (self.menuTableView) {
NSLog(@"Gesture!!!!!");
[self.menuTableView removeFromSuperview];
[self.backgroundView removeFromSuperview];
}
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
if (offset.y < 3.0f) {
NSLog(@"top!!!!");
if (self.topToolBar.hidden == YES) {
[UIView animateWithDuration:0.2 animations:^{
self.topToolBar.hidden = NO;
[self changeToInsets:UIEdgeInsetsMake(44, 0, 44, 0) targetOrientation:UnityCurrentOrientation()];
}];
}
} else {
NSLog(@"no top!!!");
if (self.topToolBar.hidden == NO) {
[UIView animateWithDuration:0.2 animations:^{
self.topToolBar.hidden = YES;
[self changeToInsets:UIEdgeInsetsMake(0, 0, 44, 0) targetOrientation:UnityCurrentOrientation()];
}];
}
}
}
-(void) updateToolBtn {
self.toolBar.btnBack.enabled = [self canGoBack];
self.toolBar.btnNext.enabled = [self canGoForward];
}
-(void) goBookmarks {
NSLog(@"goBookmarks straight");
UIViewController *unityViewController = UnityGetGLViewController();
BookmarkViewController *bookmarkController = [[BookmarkViewController alloc] init];
bookmarkController.delegate = self;
[unityViewController presentViewController:[[UINavigationController alloc] initWithRootViewController:bookmarkController] animated:NO completion:nil];
}
-(void)changeToInsets:(UIEdgeInsets)insets targetOrientation:(ScreenOrientation)orientation {
UIView *unityView = UnityGetGLViewController().view;
CGRect viewRect = unityView.frame;
if (orientation == landscapeLeft || orientation == landscapeRight) {
viewRect = CGRectMake(viewRect.origin.x, viewRect.origin.y, viewRect.size.height, viewRect.size.width);
self.toolBar.frame = CGRectMake(0, unityView.frame.size.width - 44, unityView.frame.size.height, 44);
self.topToolBar.frame = CGRectMake(0, 0, unityView.frame.size.height, 44);
self.spinner.frame = CGRectMake(unityView.frame.size.height / 2 - 65, unityView.frame.size.width / 2 - 65, 130, 130);
} else {
self.toolBar.frame = CGRectMake(0, unityView.frame.size.height - 44, unityView.frame.size.width, 44);
self.topToolBar.frame = CGRectMake(0, 0, unityView.frame.size.width, 44);
self.spinner.frame = CGRectMake(unityView.frame.size.width / 2 - 65, unityView.frame.size.height / 2 - 65, 130, 130);
}
CGRect f = CGRectMake(insets.left,
insets.top,
viewRect.size.width - insets.left - insets.right,
viewRect.size.height - insets.top - insets.bottom);
self.frame = f;
self.insets = insets;
}
-(void)dealloc {
[_toolBar release];
[_topToolBar release];
[_schemes release];
[_spinner release];
[super dealloc];
}
@end
@interface UniWebViewManager()<UIWebViewDelegate> {
NSMutableDictionary *_webViewDic;
ScreenOrientation _orientationBeforeFullScreen;
BOOL _multipleOrientation;
}
@end
@implementation UniWebViewManager
+ (UniWebViewManager *) sharedManager {
static dispatch_once_t once;
static UniWebViewManager *instance;
dispatch_once(&once, ^ { instance = [[UniWebViewManager alloc] init]; });
return instance;
}
-(instancetype) init {
self = [super init];
if (self) {
_webViewDic = [[NSMutableDictionary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:kUnityViewWillRotate object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoExitFullScreen:) name:UWVPlayerWillExitFullScreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoEnterFullScreen:) name:UWVPlayerDidEnterFullScreenNotification object:nil];
[self checkOrientationSupport];
}
return self;
}
-(void) checkOrientationSupport {
NSArray *arr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
__block BOOL portraitOrientation = NO;
__block BOOL landspaceOrientation = NO;
[arr enumerateObjectsUsingBlock:^(NSString *orientation, NSUInteger idx, BOOL *stop) {
if ([orientation rangeOfString:@"Portrait"].location != NSNotFound) {
portraitOrientation = YES;
} else if ([orientation rangeOfString:@"Landscape"].location != NSNotFound) {
landspaceOrientation = YES;
}
if (portraitOrientation && landspaceOrientation) {
_multipleOrientation = YES;
*stop = YES;
}
}];
}
-(void) addManagedWebView:(UniWebView *)webView forName:(NSString *)name {
if (![_webViewDic objectForKey:name]) {
[_webViewDic setObject:webView forKey:name];
} else {
NSLog(@"Duplicated name. Something goes wrong: %@", name);
}
}
-(void) addManagedWebViewName:(NSString *)name insets:(UIEdgeInsets)insets {
UIView *unityView = UnityGetGLViewController().view;
UniWebView *webView = [[UniWebView alloc] initWithFrame:unityView.frame];
webView.mediaPlaybackRequiresUserAction = NO;
[self changeWebView:webView insets:insets];
webView.delegate = self;
webView.hidden = YES;
[self addManagedWebView:webView forName:name];
[unityView addSubview:webView];
[unityView addSubview:webView.toolBar];
[unityView addSubview:webView.topToolBar];
[unityView addSubview:webView.spinner];
[webView release];
}
-(void) changeWebViewName:(NSString *)name insets:(UIEdgeInsets)insets {
UniWebView *webView = [_webViewDic objectForKey:name];
[self changeWebView:webView insets:insets];
}
-(void) changeWebView:(UniWebView *)webView insets:(UIEdgeInsets)insets {
[webView changeToInsets:insets targetOrientation:UnityCurrentOrientation()];
}
-(void) webviewName:(NSString *)name beginLoadURL:(NSString *)urlString {
UniWebView *webView = [_webViewDic objectForKey:name];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
-(void) webViewNameReload:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
[webView reload];
}
-(void) webViewNameStop:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
if ([webView isLoading]) {
[webView stopLoading];
}
}
-(void) webViewNameCleanCache:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:webView.request];
}
-(void) webViewNameCleanCookie:(NSString *)name forKey:(NSString *)key {
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
if (key.length) {
NSLog(@"Removing cookie for %@", key);
for (cookie in [cookieJar cookies]) {
if ([cookie.name isEqualToString:key]) {
[cookieJar deleteCookie:cookie];
NSLog(@"Found cookie for %@, removed.", key);
}
}
} else {
NSLog(@"Removing all cookies");
for (cookie in [cookieJar cookies]) {
[cookieJar deleteCookie:cookie];
}
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void) webViewName:(NSString *)name show:(BOOL)show {
UniWebView *webView = [_webViewDic objectForKey:name];
webView.hidden = !show;
}
-(void) removeWebViewName:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
webView.delegate = nil;
[webView removeFromSuperview];
[webView.toolBar removeFromSuperview];
[webView.topToolBar removeFromSuperview];
[_webViewDic removeObjectForKey:name];
}
-(void) updateBackgroundWebViewName:(NSString *)name transparent:(BOOL)transparent {
UniWebView *webView = [_webViewDic objectForKey:name];
webView.opaque = !transparent;
webView.backgroundColor = transparent ? [UIColor clearColor] : [UIColor whiteColor];
for (UIView* subView in [webView subviews]) {
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews]) {
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:transparent];
}
}
}
}
}
-(void) webViewName:(NSString *)name showToolBarAnimate:(BOOL)animate {
UniWebView *webView = [_webViewDic objectForKey:name];
if (webView.toolBar.hidden) {
if (animate) {
CGRect oldFrame = webView.toolBar.frame;
webView.toolBar.frame = CGRectOffset(oldFrame, 0, oldFrame.size.height);
webView.toolBar.hidden = NO;
[UIView animateWithDuration:0.4 animations:^{
webView.toolBar.frame = oldFrame;
}];
} else {
webView.toolBar.hidden = NO;
}
}
}
-(void) webViewName:(NSString *)name hideToolBarAnimate:(BOOL)animate {
UniWebView *webView = [_webViewDic objectForKey:name];
if (!webView.toolBar.hidden) {
if (animate) {
CGRect oldFrame = webView.toolBar.frame;
[UIView animateWithDuration:0.4 animations:^{
webView.toolBar.frame = CGRectOffset(oldFrame, 0, oldFrame.size.height);
} completion:^(BOOL finished) {
webView.toolBar.hidden = YES;
webView.toolBar.frame = oldFrame;
}];
} else {
webView.toolBar.hidden = YES;
}
}
}
-(void) goBackWebViewName:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
[webView goBack];
}
-(void) goForwardWebViewName:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
[webView goForward];
}
-(void) goBookmarksWebViewName:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
[webView goBookmarks];
}
-(void) webViewName:(NSString *)name setZoomEnable:(BOOL)enable {
UniWebView *webView = [_webViewDic objectForKey:name];
webView.scalesPageToFit = enable;
}
-(void) webViewName:(NSString *)name setBounces:(BOOL)bounces {
UniWebView *webView = [_webViewDic objectForKey:name];
[webView setBounces:bounces];
}
-(void) webViewName:(NSString *)name loadHTMLString:(NSString *)htmlString baseURLString:(NSString *)baseURL {
UniWebView *webView = [_webViewDic objectForKey:name];
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:baseURL]];
}
-(void) webViewName:(NSString *)name evaluatingJavaScript:(NSString *)javaScript shouldCallBack:(BOOL)callBack {
UniWebView *webView = [_webViewDic objectForKey:name];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:javaScript];
if (callBack) {
UnitySendMessage([name UTF8String], "EvalJavaScriptFinished", [result UTF8String]);
}
}
-(void) webViewName:(NSString *)name setSpinnerShowWhenLoading:(BOOL)show {
UniWebView *webView = [_webViewDic objectForKey:name];
webView.showSpinnerWhenLoading = show;
}
-(void) webViewName:(NSString *)name setSpinnerText:(NSString *)text {
UniWebView *webView = [_webViewDic objectForKey:name];
if (text) {
webView.spinner.textLabel.text = text;
}
}
-(NSString *) webViewName:(UniWebView *)webView {
NSString *webViewName = [[_webViewDic allKeysForObject:webView] lastObject];
if (!webViewName) {
NSLog(@"Did not find the webview: %@",webViewName);
}
return webViewName;
}
- (void)webViewName:(NSString *)name addUrlScheme:(NSString *)scheme {
UniWebView *webView = [_webViewDic objectForKey:name];
if (![webView.schemes containsObject:scheme]) {
[webView.schemes addObject:scheme];
}
}
- (void)webViewName:(NSString *)name removeUrlScheme:(NSString *)scheme {
UniWebView *webView = [_webViewDic objectForKey:name];
if ([webView.schemes containsObject:scheme]) {
[webView.schemes removeObject:scheme];
}
}
- (void)webViewDidStartLoad:(UniWebView *)webView {
if (webView.showSpinnerWhenLoading && !webView.hidden) {
[webView.spinner show];
[[webView topToolBar] btnAddfav].enabled = NO;
}
}
- (void)webViewDidFinishLoad:(UniWebView *)webView {
[webView.spinner hide];
NSString *webViewName = [self webViewName:webView];
[webView updateToolBtn];
webView.currentUrl = webView.request.mainDocumentURL.absoluteString;
UnitySendMessage([webViewName UTF8String], "LoadComplete", "");
webView.currentTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"title: %@", webView.currentTitle);
[[webView topToolBar] btnAddfav].enabled = YES;
}
- (void)webView:(UniWebView *)webView didFailLoadWithError:(NSError *)error {
[webView.spinner hide];
NSString *webViewName = [self webViewName:webView];
[webView updateToolBtn];
webView.currentUrl = webView.request.mainDocumentURL.absoluteString;
UnitySendMessage([webViewName UTF8String], "LoadComplete", [error.localizedDescription UTF8String]);
}
- (void)webViewDone:(UniWebView *)webView {
[webView.spinner hide];
NSString *webViewName = [self webViewName:webView];
UnitySendMessage([webViewName UTF8String], "WebViewDone", "");
}
-(NSString *) webViewNameGetCurrentUrl:(NSString *)name {
UniWebView *webView = [_webViewDic objectForKey:name];
return webView.currentUrl ?: @"";
}
-(BOOL)webView:(UniWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *webViewName = [self webViewName:webView];
__block BOOL canResponse = NO;
[webView.schemes enumerateObjectsUsingBlock:^(NSString *scheme, NSUInteger idx, BOOL *stop) {
if ([[request.URL absoluteString] rangeOfString:[scheme stringByAppendingString:@"://"]].location == 0) {
canResponse = YES;
*stop = YES;
}
}];
if (canResponse) {
NSString *rawMessage = [NSString stringWithFormat:@"%@",request.URL];
UnitySendMessage([webViewName UTF8String], "ReceivedMessage", [rawMessage UTF8String]);
return NO;
} else {
UnitySendMessage([webViewName UTF8String], "LoadBegin", [request.URL.absoluteString UTF8String]);
}
return YES;
}
-(void) orientationChanged:(NSNotification *)noti {
[_webViewDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
UniWebView *webView = (UniWebView *)obj;
[webView changeToInsets:webView.insets targetOrientation:UnityCurrentOrientation()];
}];
}
-(void) videoEnterFullScreen:(NSNotification *)noti {
UIInterfaceOrientation toInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
_orientationBeforeFullScreen = ConvertToUnityScreenOrientation(toInterfaceOrientation, 0);
}
-(void) videoExitFullScreen:(NSNotification *)noti {
ScreenOrientation orientation = portrait;
if (_multipleOrientation) {
UIInterfaceOrientation toInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
orientation = ConvertToUnityScreenOrientation(toInterfaceOrientation, 0);
} else {
orientation = UnityCurrentOrientation();
}
if (_orientationBeforeFullScreen == landscapeLeft || _orientationBeforeFullScreen == landscapeRight) {
if (orientation == portrait) {
orientation = _orientationBeforeFullScreen;
} else {
orientation = portrait;
}
}
[_webViewDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
UniWebView *webView = (UniWebView *)obj;
[webView changeToInsets:webView.insets targetOrientation:orientation];
}];
}
@end
// Helper method to create C string copy
NSString* UniWebViewMakeNSString (const char* string) {
if (string) {
return [NSString stringWithUTF8String: string];
} else {
return [NSString stringWithUTF8String: ""];
}
}
char* UniWebViewMakeCString(NSString *str) {
const char* string = [str UTF8String];
if (string == NULL) {
return NULL;
}
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
extern "C" {
void _UniWebViewInit(const char *name, int top, int left, int bottom, int right);
void _UniWebViewChangeSize(const char *name, int top, int left, int bottom, int right);
void _UniWebViewLoad(const char *name, const char *url);
void _UniWebViewReload(const char *name);
void _UniWebViewStop(const char *name);
void _UniWebViewShow(const char *name);
void _UniWebViewDismiss(const char *name);
void _UniWebViewCleanCache(const char *name);
void _UniWebViewCleanCookie(const char *name, const char *key);
void _UniWebViewDestroy(const char *name);
void _UniWebViewTransparentBackground(const char *name, BOOL transparent);
void _UniWebViewShowToolBar(const char *name, bool animate);
void _UniWebViewHideToolBar(const char *name, bool animate);
void _UniWebViewGoBack(const char *name);
void _UniWebViewGoForward(const char *name);
void _UniWebViewGoBookmarks(const char *name);
void _UniWebViewLoadHTMLString(const char *name, const char *html, const char *baseUrl);
void _UniWebViewEvaluatingJavaScript(const char *name, const char *javascript, BOOL callback);
void _UniWebViewSetSpinnerShowWhenLoading(const char *name, bool show);
void _UniWebViewSetSpinnerText(const char *name, const char *text);
const char * _UniWebViewGetCurrentUrl(const char *name);
void _UniWebViewSetBounces(const char *name, bool bounces);
void _UniWebViewSetZoomEnable(const char *name, bool enable);
void _UniWebViewAddUrlScheme(const char *name, const char *scheme);
void _UniWebViewRemoveUrlScheme(const char *name, const char *scheme);
}
void _UniWebViewInit(const char *name, int top, int left, int bottom, int right) {
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
[[UniWebViewManager sharedManager] addManagedWebViewName:UniWebViewMakeNSString(name) insets:insets];
}
void _UniWebViewChangeSize(const char *name, int top, int left, int bottom, int right) {
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
[[UniWebViewManager sharedManager] changeWebViewName:UniWebViewMakeNSString(name) insets:insets];
}
void _UniWebViewLoad(const char *name, const char *url) {
[[UniWebViewManager sharedManager] webviewName:UniWebViewMakeNSString(name)
beginLoadURL:UniWebViewMakeNSString(url)];
}
void _UniWebViewReload(const char *name) {
[[UniWebViewManager sharedManager] webViewNameReload:UniWebViewMakeNSString(name)];
}
void _UniWebViewStop(const char *name) {
[[UniWebViewManager sharedManager] webViewNameStop:UniWebViewMakeNSString(name)];
}
void _UniWebViewShow(const char *name) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name) show:YES];
}
void _UniWebViewDismiss(const char *name) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name) show:NO];
}
void _UniWebViewCleanCache(const char *name) {
[[UniWebViewManager sharedManager] webViewNameCleanCache:UniWebViewMakeNSString(name)];
}
void _UniWebViewCleanCookie(const char *name, const char *key) {
[[UniWebViewManager sharedManager] webViewNameCleanCookie:UniWebViewMakeNSString(name) forKey:UniWebViewMakeNSString(key)];
}
void _UniWebViewDestroy(const char *name) {
[[UniWebViewManager sharedManager] removeWebViewName:UniWebViewMakeNSString(name)];
}
void _UniWebViewTransparentBackground(const char *name, BOOL transparent) {
[[UniWebViewManager sharedManager] updateBackgroundWebViewName:UniWebViewMakeNSString(name) transparent:transparent];
}
void _UniWebViewShowToolBar(const char *name, bool animate) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name) showToolBarAnimate:animate];
}
void _UniWebViewHideToolBar(const char *name, bool animate) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name) hideToolBarAnimate:animate];
}
void _UniWebViewGoBack(const char *name) {
[[UniWebViewManager sharedManager] goBackWebViewName:UniWebViewMakeNSString(name)];
}
void _UniWebViewGoForward(const char *name) {
[[UniWebViewManager sharedManager] goForwardWebViewName:UniWebViewMakeNSString(name)];
}
void _UniWebViewGoBookmarks(const char *name) {
[[UniWebViewManager sharedManager] goBookmarksWebViewName:UniWebViewMakeNSString(name)];
}
void _UniWebViewLoadHTMLString(const char *name, const char *html, const char *baseUrl) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name)
loadHTMLString:UniWebViewMakeNSString(html)
baseURLString:UniWebViewMakeNSString(baseUrl)];
}
void _UniWebViewSetBounces(const char *name, bool bounces) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name) setBounces:bounces];
}
void _UniWebViewEvaluatingJavaScript(const char *name, const char *javascript, BOOL callback) {
NSString *webViewName = UniWebViewMakeNSString(name);
NSString *jsString = UniWebViewMakeNSString(javascript);
NSLog(@"webViewName:%@, eval js:%@",webViewName,jsString);
[[UniWebViewManager sharedManager] webViewName:webViewName evaluatingJavaScript:jsString shouldCallBack:callback];
}
void _UniWebViewSetSpinnerShowWhenLoading(const char *name, bool show) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name)
setSpinnerShowWhenLoading:show];
}
void _UniWebViewSetSpinnerText(const char *name, const char *text) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name)
setSpinnerText:UniWebViewMakeNSString(text)];
}
const char *_UniWebViewGetCurrentUrl(const char *name) {
return UniWebViewMakeCString([[UniWebViewManager sharedManager] webViewNameGetCurrentUrl:UniWebViewMakeNSString(name)]);
}
void _UniWebViewSetZoomEnable(const char *name, bool enable) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name) setZoomEnable:enable];
}
void _UniWebViewAddUrlScheme(const char *name, const char *scheme) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name)
addUrlScheme:UniWebViewMakeNSString(scheme)];
}
void _UniWebViewRemoveUrlScheme(const char *name, const char *scheme) {
[[UniWebViewManager sharedManager] webViewName:UniWebViewMakeNSString(name)
removeUrlScheme:UniWebViewMakeNSString(scheme)];
}