Skip to content

Commit 7ab2fee

Browse files
committed
1.1.3
1. localizable
1 parent a62dcda commit 7ab2fee

File tree

14 files changed

+391
-31
lines changed

14 files changed

+391
-31
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ source 'https://github.com/CocoaPods/Specs.git'
1818
platform :ios, '9.0'
1919
2020
target 'TargetName' do
21-
pod 'ZFWKWebView', '~> 1.1.2'
21+
pod 'ZFWKWebView', '~> 1.1.3'
2222
end
2323
```
2424
Then, run the following command:

ZFWKWebView.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
Pod::Spec.new do |s|
1111
s.name = "ZFWKWebView"
12-
s.version = "1.1.2"
12+
s.version = "1.1.3"
1313
s.summary = "No short description of ZFWKWebView."
1414
s.homepage = "https://github.com/FranLucky/ZFWKWebView"
1515
s.license = { :type => "MIT", :file => "LICENSE"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
localizable.strings
3+
ZFWKWebView
4+
5+
Created by 张帆 on 2019/10/24.
6+
Copyright © 2019 张帆. All rights reserved.
7+
*/
8+
9+
"support" = " ";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
ZFWKWebView
3+
4+
Created by 张帆 on 2019/10/24.
5+
Copyright © 2019 张帆. All rights reserved.
6+
*/
7+
8+
9+
"Support by " = "此网页由";
10+
"Can't open this page" = "无法打开此网页";
11+
"Reason" = "原因";
12+
"Solution" = "解决";
13+
"Click to try again" = "点击屏幕重试";
14+
"Error" = "错误";
15+
"Tips" = "提示";
16+
"Sure" = "确定";
17+
"Loading..." = "加载中...";
18+
"support" = "提供";

ZFWKWebView/ZFWKWebVC.h

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ UIKIT_EXTERN ZFWKWebViewEventKey const ZFWKWebViewEventGoForwardKey;
4646

4747
@property(nonatomic, assign) int timeoutDuration; // default 15s
4848
@property(nonatomic, assign) float progressBarHeight; // default 2.5
49+
@property(nonatomic, assign) float navigationButtonSpace; // default 0;
4950

5051
@property (nonatomic, strong) UIColor *progressBackgroundColor; // [UIColor clearColor]
5152
@property (nonatomic, strong) UIColor *progressTintColor; // [UIColor colorWithRed:86/255.0 green:187/255.0 blue:59/255.0 alpha:1]
@@ -82,8 +83,16 @@ UIKIT_EXTERN ZFWKWebViewEventKey const ZFWKWebViewEventGoForwardKey;
8283
*/
8384
- (void)addMethodName:(NSString *)name callback:(zf_wkWebViewEventCallBack)callback;
8485

86+
+ (NSString *)localizedStringForKey:(NSString *)key;
8587
@end
8688

89+
90+
91+
92+
93+
94+
95+
8796
@interface ZFWKWebVC : UIViewController
8897
- (instancetype)initWithDefaultConfig;
8998
- (instancetype)initWithConf:(ZFWKWebVCConf *)conf;
@@ -96,6 +105,7 @@ UIKIT_EXTERN ZFWKWebViewEventKey const ZFWKWebViewEventGoForwardKey;
96105
@property (nonatomic, strong, readonly) ZFWKWebVCConf *config;
97106

98107

108+
99109
@end
100110

101111
NS_ASSUME_NONNULL_END

ZFWKWebView/ZFWKWebVC.m

+49-20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define ZF_SCREEN_WIDTH self.view.frame.size.width
1818
#define ZF_SCREEN_HEIGHT self.view.frame.size.height
1919

20+
#define SOURCE_BUDNLE [NSBundle bundleWithURL:[[NSBundle bundleForClass:[ZFWKWebVC class]] URLForResource:@"ImageResource" withExtension:@"bundle"]]
21+
2022
NSString * const ZFWKWebViewEventStartLoadKey = @"ZFWKWebViewEventStartLoadKey";
2123
NSString * const ZFWKWebViewEventStartRecevicedKey = @"ZFWKWebViewEventStartRecevicedKey";
2224
NSString * const ZFWKWebViewEventFinishRecevicedKey = @"ZFWKWebViewEventFinishRecevicedKey";
@@ -143,11 +145,9 @@ - (instancetype)init
143145
_showCloseButton = YES;
144146
_callbacks = [NSMutableDictionary dictionaryWithCapacity:100];
145147
_progressBarHeight = 2.5;
148+
_navigationButtonSpace = 0;
146149

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;
151151

152152
_backButtonImage = [UIImage imageNamed:@"BackButtonIcon" inBundle:imageBundle compatibleWithTraitCollection:nil];
153153
_closeButtonImage = [UIImage imageNamed:@"CloseButtonIcon" inBundle:imageBundle compatibleWithTraitCollection:nil];
@@ -170,6 +170,28 @@ - (void)addMethodName:(NSString *)name callback:(zf_wkWebViewEventCallBack)callb
170170

171171
}
172172

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+
173195
@end
174196

175197
@interface ZFWKWebVC () <WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate> {
@@ -228,7 +250,6 @@ - (instancetype)initWithConf:(ZFWKWebVCConf *)conf {
228250
scrollJudgeDistance = 100;
229251
self.webView = ({
230252
WKWebViewConfiguration *webViewConf = [[WKWebViewConfiguration alloc] init];
231-
// [webViewConf.userContentController addScriptMessageHandler:self name:@"notify"];
232253
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webViewConf];
233254
webView.navigationDelegate = self;
234255
webView.UIDelegate = self;
@@ -263,6 +284,7 @@ - (void)viewWillDisappear:(BOOL)animated {
263284
- (void)viewDidDisappear:(BOOL)animated {
264285
[super viewDidDisappear:animated];
265286
[self.config.callbacks removeAllObjects];
287+
// fixed with iOS10,remove observe first
266288
[self removeObserver];
267289
self.config = nil;
268290
}
@@ -387,7 +409,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
387409
self.progressView.hidden = progress >= 1.0;
388410
} else if ([keyPath isEqualToString:@"URL"]) {
389411
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"]];
391413
self.previousURL = value;
392414
} else {
393415
self.previousURL = oldValue;
@@ -472,15 +494,19 @@ - (void)viewWillLayoutSubviews {
472494

473495
[self.navView setFrame:CGRectMake(0, 0, ZF_SCREEN_WIDTH, navHeight)];
474496
{
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)];
480502
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)];
482508
[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)];
484510
}
485511
y += navHeight;
486512
[self.webView setFrame:CGRectMake(0, y, ZF_SCREEN_WIDTH, ZF_SCREEN_HEIGHT - y)];
@@ -509,7 +535,7 @@ - (void)rightButtonClick:(UIButton *)button {
509535
}
510536

511537
- (void)refresh {
512-
self.titleLabel.text = @"加载中...";
538+
self.titleLabel.text = [ZFWKWebVCConf localizedStringForKey:@"Loading..."];
513539
zf_wkWebViewEventCallBack callback = self.config.callbacks[ZFWKWebViewEventRefreshKey];
514540
if (callback) callback(self, self.config, nil);
515541
[self.webView stopLoading];
@@ -593,8 +619,8 @@ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSStrin
593619
completionHandler();
594620
return;
595621
}
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();
598624
}]];
599625
if (self.config) {
600626
[self presentViewController:alertController animated:YES completion:^{}];
@@ -634,14 +660,15 @@ - (void)showError:(NSError *)error {
634660
zf_wkWebViewEventCallBack callback = self.config.callbacks[ZFWKWebViewEventLoadFailedKey];
635661
if (callback) callback(self, self.config, error);
636662
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];
638664
if (error.localizedFailureReason) {
639-
text = [text stringByAppendingFormat:@"\n原因:%@", error.localizedFailureReason];
665+
text = [text stringByAppendingFormat:@"\n%@:%@", [ZFWKWebVCConf localizedStringForKey:@"Reason"], error.localizedFailureReason];
640666
}
641667
if (error.localizedRecoverySuggestion) {
642-
text = [text stringByAppendingFormat:@"\n解决:%@", error.localizedRecoverySuggestion];
668+
text = [text stringByAppendingFormat:@"\n%@:%@",[ZFWKWebVCConf localizedStringForKey:@"Solution"], error.localizedRecoverySuggestion];
643669
}
644-
self.titleLabel.text = @"无法打开此网页";
670+
671+
self.titleLabel.text = [ZFWKWebVCConf localizedStringForKey:@"Can't open this page"];
645672
self.loadFailedView.textLabel.text = text;
646673
}
647674

@@ -674,4 +701,6 @@ - (void)userContentController:(WKUserContentController *)userContentController d
674701

675702
}
676703

704+
705+
677706
@end

0 commit comments

Comments
 (0)