17
17
#define ZF_SCREEN_WIDTH self.view.frame.size.width
18
18
#define ZF_SCREEN_HEIGHT self.view.frame.size.height
19
19
20
+ #define SOURCE_BUDNLE [NSBundle bundleWithURL: [[NSBundle bundleForClass: [ZFWKWebVC class ]] URLForResource: @" ImageResource" withExtension: @" bundle" ]]
21
+
20
22
NSString * const ZFWKWebViewEventStartLoadKey = @" ZFWKWebViewEventStartLoadKey" ;
21
23
NSString * const ZFWKWebViewEventStartRecevicedKey = @" ZFWKWebViewEventStartRecevicedKey" ;
22
24
NSString * const ZFWKWebViewEventFinishRecevicedKey = @" ZFWKWebViewEventFinishRecevicedKey" ;
@@ -143,11 +145,9 @@ - (instancetype)init
143
145
_showCloseButton = YES ;
144
146
_callbacks = [NSMutableDictionary dictionaryWithCapacity: 100 ];
145
147
_progressBarHeight = 2.5 ;
148
+ _navigationButtonSpace = 0 ;
146
149
147
- NSBundle *bundle = [NSBundle bundleForClass: [ZFWKWebVC class ]];
148
- NSURL *url = [bundle URLForResource: @" ImageResource" withExtension: @" bundle" ];
149
- NSBundle *imageBundle = [NSBundle bundleWithURL: url];
150
-
150
+ NSBundle *imageBundle = SOURCE_BUDNLE;
151
151
152
152
_backButtonImage = [UIImage imageNamed: @" BackButtonIcon" inBundle: imageBundle compatibleWithTraitCollection: nil ];
153
153
_closeButtonImage = [UIImage imageNamed: @" CloseButtonIcon" inBundle: imageBundle compatibleWithTraitCollection: nil ];
@@ -170,6 +170,28 @@ - (void)addMethodName:(NSString *)name callback:(zf_wkWebViewEventCallBack)callb
170
170
171
171
}
172
172
173
+
174
+ + (NSString *)localizedStringForKey : (NSString *)key {
175
+ NSString *localizedStr = nil ;
176
+ static NSBundle *bundle = nil ;
177
+ if (bundle == nil ) {
178
+
179
+ NSString *language = [NSLocale preferredLanguages ].firstObject ;
180
+ if ([language hasPrefix: @" en" ]) {
181
+ language = @" en" ;
182
+ } else if ([language hasPrefix: @" zh" ]) {
183
+ language = @" zh-Hans" ;
184
+ } else {
185
+ language = @" en" ;
186
+ }
187
+
188
+ bundle = [NSBundle bundleWithPath: [SOURCE_BUDNLE pathForResource: language ofType: @" lproj" ]];
189
+ }
190
+ NSString *value = [bundle localizedStringForKey: key value: nil table: nil ];
191
+ localizedStr = [[NSBundle mainBundle ] localizedStringForKey: key value: value table: nil ];
192
+ return localizedStr;
193
+ }
194
+
173
195
@end
174
196
175
197
@interface ZFWKWebVC () <WKNavigationDelegate , WKScriptMessageHandler , WKUIDelegate > {
@@ -228,7 +250,6 @@ - (instancetype)initWithConf:(ZFWKWebVCConf *)conf {
228
250
scrollJudgeDistance = 100 ;
229
251
self.webView = ({
230
252
WKWebViewConfiguration *webViewConf = [[WKWebViewConfiguration alloc ] init ];
231
- // [webViewConf.userContentController addScriptMessageHandler:self name:@"notify"];
232
253
WKWebView *webView = [[WKWebView alloc ] initWithFrame: CGRectZero configuration: webViewConf];
233
254
webView.navigationDelegate = self;
234
255
webView.UIDelegate = self;
@@ -263,6 +284,7 @@ - (void)viewWillDisappear:(BOOL)animated {
263
284
- (void )viewDidDisappear : (BOOL )animated {
264
285
[super viewDidDisappear: animated];
265
286
[self .config.callbacks removeAllObjects ];
287
+ // fixed with iOS10,remove observe first
266
288
[self removeObserver ];
267
289
self.config = nil ;
268
290
}
@@ -387,7 +409,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
387
409
self.progressView .hidden = progress >= 1.0 ;
388
410
} else if ([keyPath isEqualToString: @" URL" ]) {
389
411
if (value) {
390
- self.originLabel .text = [NSString stringWithFormat: @" 此网页由 %@ 提供 " , ((NSURL *)value).host];
412
+ self.originLabel .text = [NSString stringWithFormat: @" %@%@%@ " , [ZFWKWebVCConf localizedStringForKey: @" Support by " ], ((NSURL *)value).host, [ZFWKWebVCConf localizedStringForKey: @" support " ] ];
391
413
self.previousURL = value;
392
414
} else {
393
415
self.previousURL = oldValue;
@@ -472,15 +494,19 @@ - (void)viewWillLayoutSubviews {
472
494
473
495
[self .navView setFrame: CGRectMake (0 , 0 , ZF_SCREEN_WIDTH, navHeight)];
474
496
{
475
- float btnW = 44 ;
476
- float nomalMargin = 0 ;
477
- [ self .backButton setFrame: CGRectMake ( nomalMargin, navHeight - btnW, btnW, btnW)] ;
478
- [self .closeButton setFrame: CGRectMake (nomalMargin + btnW + nomalMargin , navHeight - btnW , btnW, btnW )];
479
- float titleLabelWidth = 300 ;
497
+ float btnW = 44 - 10 ;
498
+ float btnH = 44 ;
499
+ float nomalMargin = self. config . navigationButtonSpace ;
500
+ [self .backButton setFrame: CGRectMake (nomalMargin, navHeight - btnH , btnW, btnH )];
501
+ [ self .closeButton setFrame: CGRectMake (nomalMargin + btnW + nomalMargin, navHeight - btnH, btnW, btnH)] ;
480
502
float rightButtonW = 80 ;
481
- [self .titleLabel setFrame: CGRectMake ((ZF_SCREEN_WIDTH - titleLabelWidth) * 0.5 , navHeight - btnW, titleLabelWidth, btnW)];
503
+ float left = nomalMargin + btnW + nomalMargin + btnW + nomalMargin;
504
+ float right = rightButtonW;
505
+ float maxDistance = MAX (left, right);
506
+ float titleLabelWidth = ZF_SCREEN_WIDTH - maxDistance * 2 - nomalMargin * 2 ;
507
+ [self .titleLabel setFrame: CGRectMake ((ZF_SCREEN_WIDTH - titleLabelWidth) * 0.5 , navHeight - btnH, titleLabelWidth, btnH)];
482
508
[self .progressView setFrame: CGRectMake (0 , navHeight - self .config.progressBarHeight, ZF_SCREEN_WIDTH, self .config.progressBarHeight)];
483
- [self .navigationRightButon setFrame: CGRectMake (ZF_SCREEN_WIDTH - 10 - rightButtonW, navHeight - btnW , rightButtonW, btnW )];
509
+ [self .navigationRightButon setFrame: CGRectMake (ZF_SCREEN_WIDTH - 10 - rightButtonW, navHeight - btnH , rightButtonW, btnH )];
484
510
}
485
511
y += navHeight;
486
512
[self .webView setFrame: CGRectMake (0 , y, ZF_SCREEN_WIDTH, ZF_SCREEN_HEIGHT - y)];
@@ -509,7 +535,7 @@ - (void)rightButtonClick:(UIButton *)button {
509
535
}
510
536
511
537
- (void )refresh {
512
- self.titleLabel .text = @" 加载中 ..." ;
538
+ self.titleLabel .text = [ZFWKWebVCConf localizedStringForKey: @" Loading ..." ] ;
513
539
zf_wkWebViewEventCallBack callback = self.config .callbacks [ZFWKWebViewEventRefreshKey];
514
540
if (callback) callback (self, self.config , nil );
515
541
[self .webView stopLoading ];
@@ -593,8 +619,8 @@ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSStrin
593
619
completionHandler ();
594
620
return ;
595
621
}
596
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle: @" 提示 " message: message preferredStyle: UIAlertControllerStyleAlert];
597
- [alertController addAction: [UIAlertAction actionWithTitle: @" 确认 " style: UIAlertActionStyleCancel handler: ^(UIAlertAction *action) { completionHandler ();
622
+ UIAlertController *alertController = [UIAlertController alertControllerWithTitle: [ZFWKWebVCConf localizedStringForKey: @" Tips " ] message: message preferredStyle: UIAlertControllerStyleAlert];
623
+ [alertController addAction: [UIAlertAction actionWithTitle: [ZFWKWebVCConf localizedStringForKey: @" Sure " ] style: UIAlertActionStyleCancel handler: ^(UIAlertAction *action) { completionHandler ();
598
624
}]];
599
625
if (self.config ) {
600
626
[self presentViewController: alertController animated: YES completion: ^{}];
@@ -634,14 +660,15 @@ - (void)showError:(NSError *)error {
634
660
zf_wkWebViewEventCallBack callback = self.config .callbacks [ZFWKWebViewEventLoadFailedKey];
635
661
if (callback) callback (self, self.config , error);
636
662
self.loadFailedView .hidden = NO ;
637
- NSString *text = [NSString stringWithFormat: @" 点击屏幕重试 \n\n 错误 :%@ " , error.localizedDescription];
663
+ NSString *text = [NSString stringWithFormat: @" %@ \n\n %@ :%@ " ,[ZFWKWebVCConf localizedStringForKey: @" Click to try again " ],[ZFWKWebVCConf localizedStringForKey: @" Error " ] , error.localizedDescription];
638
664
if (error.localizedFailureReason ) {
639
- text = [text stringByAppendingFormat: @" \n 原因 :%@ " , error.localizedFailureReason];
665
+ text = [text stringByAppendingFormat: @" \n %@ :%@ " , [ZFWKWebVCConf localizedStringForKey: @" Reason " ] , error.localizedFailureReason];
640
666
}
641
667
if (error.localizedRecoverySuggestion ) {
642
- text = [text stringByAppendingFormat: @" \n 解决 :%@ " , error.localizedRecoverySuggestion];
668
+ text = [text stringByAppendingFormat: @" \n %@ :%@ " ,[ZFWKWebVCConf localizedStringForKey: @" Solution " ] , error.localizedRecoverySuggestion];
643
669
}
644
- self.titleLabel .text = @" 无法打开此网页" ;
670
+
671
+ self.titleLabel .text = [ZFWKWebVCConf localizedStringForKey: @" Can't open this page" ];
645
672
self.loadFailedView .textLabel .text = text;
646
673
}
647
674
@@ -674,4 +701,6 @@ - (void)userContentController:(WKUserContentController *)userContentController d
674
701
675
702
}
676
703
704
+
705
+
677
706
@end
0 commit comments