Skip to content

Commit

Permalink
fix: web-view组件UserAgent为空的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed May 18, 2022
1 parent 0381782 commit ba7ee45
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions plugins/eeui/framework/ios/eeui/Component/eeuiWKWebViewComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,20 @@ - (WKWebView*)loadView
eeuiStorageManager *storage = [eeuiStorageManager sharedIntstance];
originalUserAgent = [storage getCachesString:@"__system:originalUserAgent" defaultVal:@""];
if (![originalUserAgent containsString:@";ios_kuaifan_eeui/"]) {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"] ?:@"";
NSString *versionName = (NSString*)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
originalUserAgent = [NSString stringWithFormat:@"%@;ios_kuaifan_eeui/%@", oldAgent, versionName];
[storage setCachesString:@"__system:originalUserAgent" value:originalUserAgent expired:0];
__block WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero];
[wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
wkWebView = nil;
if (!error) {
NSString *versionName = (NSString*)[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
originalUserAgent = [NSString stringWithFormat:@"%@;ios_kuaifan_eeui/%@", result, versionName];
if (self->_userAgent.length > 0) {
originalUserAgent = [NSString stringWithFormat:@"%@/%@", originalUserAgent, self->_userAgent];
}
[storage setCachesString:@"__system:originalUserAgent" value:originalUserAgent expired:0];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:originalUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
}];
}
if (_userAgent.length > 0) {
originalUserAgent = [NSString stringWithFormat:@"%@/%@", originalUserAgent, _userAgent];
Expand All @@ -116,7 +125,7 @@ - (WKWebView*)loadView
// 去掉 WkWebviewe Done 工具栏
- (void) hideWKWebviewKeyboardShortcutBar:(WKWebView *)webView {
UIView *targetView;

for (UIView *view in webView.scrollView.subviews) {
if([[view.class description] hasPrefix:@"WKContent"]) {
targetView = view;
Expand All @@ -127,7 +136,7 @@ - (void) hideWKWebviewKeyboardShortcutBar:(WKWebView *)webView {
}
NSString *noInputAccessoryViewClassName = [NSString stringWithFormat:@"%@_NoInputAccessoryView", targetView.class.superclass];
Class newClass = NSClassFromString(noInputAccessoryViewClassName);

if(newClass == nil) {
newClass = objc_allocateClassPair(targetView.class, [noInputAccessoryViewClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
if(!newClass) {
Expand Down Expand Up @@ -156,7 +165,7 @@ - (void)viewDidLoad
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
}

if (_isHiddenDone) {
[self hideWKWebviewKeyboardShortcutBar: webView];
}
Expand Down

0 comments on commit ba7ee45

Please sign in to comment.