Skip to content

Commit

Permalink
feat(ios): implement custom userAgent handling (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Mar 27, 2020
1 parent 0bf16f1 commit 8587114
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/ios/CDVWKWebViewEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti
[configuration setValue:[NSNumber numberWithBool:YES]
forKey:_BGStatus];
}

NSString *userAgent = configuration.applicationNameForUserAgent;
if (
[settings cordovaSettingForKey:@"OverrideUserAgent"] == nil &&
[settings cordovaSettingForKey:@"AppendUserAgent"] != nil
) {
userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [settings cordovaSettingForKey:@"AppendUserAgent"]];
}
configuration.applicationNameForUserAgent = userAgent;
configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:YES];
configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO];
configuration.allowsAirPlayForMediaPlayback = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES];
Expand Down Expand Up @@ -270,8 +277,9 @@ - (void)pluginInitialize
// add to keyWindow to ensure it is 'active'
[UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView];

if ([self.viewController isKindOfClass:[CDVViewController class]]) {
wkWebView.customUserAgent = ((CDVViewController*) self.viewController).userAgent;
NSString * overrideUserAgent = [settings cordovaSettingForKey:@"OverrideUserAgent"];
if (overrideUserAgent != nil) {
wkWebView.customUserAgent = overrideUserAgent;
}

if ([self.viewController conformsToProtocol:@protocol(WKUIDelegate)]) {
Expand Down Expand Up @@ -670,9 +678,6 @@ - (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*

- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation
{
CDVViewController* vc = (CDVViewController*)self.viewController;
[CDVUserAgentUtil releaseLock:vc.userAgentLockToken];

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPageDidLoadNotification object:webView]];
}

Expand All @@ -684,7 +689,6 @@ - (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(WKNavigatio
- (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigation withError:(NSError*)error
{
CDVViewController* vc = (CDVViewController*)self.viewController;
[CDVUserAgentUtil releaseLock:vc.userAgentLockToken];

NSString* message = [NSString stringWithFormat:@"Failed to load webpage with error: %@", [error localizedDescription]];
NSLog(@"%@", message);
Expand Down

0 comments on commit 8587114

Please sign in to comment.