Skip to content

Commit

Permalink
breaking (UserAgent): Drop CDVUserAgentUtil and Implement for WKWebVi…
Browse files Browse the repository at this point in the history
…ew (#801)

* breaking (CDVUserAgentUtil): delete
* feature (User-Agent): Added Support for WKWebView
  • Loading branch information
erisu authored Mar 9, 2020
1 parent 2922437 commit 20248c1
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti
}
configuration.allowsAirPlayForMediaPlayback = allowsAirPlayForMediaPlayback;

/*
* Sets Custom User Agents
* - (Default) "userAgent" is set the the clean user agent.
* E.g.
* UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
*
* - If "OverrideUserAgent" is set, it will overwrite the entire "userAgent" value. The "AppendUserAgent" will be iggnored if set.
* Notice: The override logic is handled in the "pluginInitialize" method.
* E.g.
* OverrideUserAgent = "foobar"
* UserAgent = "foobar"
*
* - If "AppendUserAgent" is set and "OverrideUserAgent" is not set, the user defined "AppendUserAgent" will be appended to the "userAgent"
* E.g.
* AppendUserAgent = "foobar"
* UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 foobar"
*/
NSString *userAgent = configuration.applicationNameForUserAgent;
if (
[settings cordovaSettingForKey:@"OverrideUserAgent"] == nil &&
[settings cordovaSettingForKey:@"AppendUserAgent"] != nil
) {
userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [settings cordovaSettingForKey:@"AppendUserAgent"]];
}
configuration.applicationNameForUserAgent = userAgent;

return configuration;
}

Expand Down Expand Up @@ -149,9 +175,16 @@ - (void)pluginInitialize
// re-create WKWebView, since we need to update configuration
WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.engineWebView.frame configuration:configuration];
wkWebView.UIDelegate = self.uiDelegate;
self.engineWebView = wkWebView;

wkWebView.customUserAgent = vc.userAgent;
/*
* This is where the "OverrideUserAgent" is handled. This will replace the entire UserAgent
* with the user defined custom UserAgent.
*/
if ([settings cordovaSettingForKey:@"OverrideUserAgent"] != nil) {
wkWebView.customUserAgent = [settings cordovaSettingForKey:@"OverrideUserAgent"];
}

self.engineWebView = wkWebView;

if ([self.viewController conformsToProtocol:@protocol(WKUIDelegate)]) {
wkWebView.UIDelegate = (id <WKUIDelegate>)self.viewController;
Expand Down Expand Up @@ -425,9 +458,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 @@ -439,7 +469,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
1 change: 0 additions & 1 deletion CordovaLib/Classes/Public/CDV.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@
#import "CDVWhitelist.h"
#import "CDVScreenOrientationDelegate.h"
#import "CDVTimer.h"
#import "CDVUserAgentUtil.h"
2 changes: 0 additions & 2 deletions CordovaLib/Classes/Public/CDVCommandDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,5 @@ typedef NSURL* (^ UrlTransformerBlock)(NSURL*);
- (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop;
// Runs the given block on a background thread using a shared thread-pool.
- (void)runInBackground:(void (^)(void))block;
// Returns the User-Agent of the associated WKWebView.
- (NSString*)userAgent;

@end
5 changes: 0 additions & 5 deletions CordovaLib/Classes/Public/CDVCommandDelegateImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ - (void)runInBackground:(void (^)(void))block
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
}

- (NSString*)userAgent
{
return [_viewController userAgent];
}

- (NSDictionary*)settings
{
return _viewController.settings;
Expand Down
27 changes: 0 additions & 27 deletions CordovaLib/Classes/Public/CDVUserAgentUtil.h

This file was deleted.

156 changes: 0 additions & 156 deletions CordovaLib/Classes/Public/CDVUserAgentUtil.m

This file was deleted.

18 changes: 0 additions & 18 deletions CordovaLib/Classes/Public/CDVViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
id <CDVCommandDelegate> _commandDelegate;
@protected
CDVCommandQueue* _commandQueue;
NSString* _userAgent;
}

@property (nonatomic, readonly, weak) IBOutlet UIView* webView;
Expand All @@ -52,29 +51,12 @@
@property (nonatomic, readonly, strong) id <CDVWebViewEngineProtocol> webViewEngine;
@property (nonatomic, readonly, strong) id <CDVCommandDelegate> commandDelegate;

/**
The complete user agent that Cordova will use when sending web requests.
*/
@property (nonatomic, readonly) NSString* userAgent;

/**
The base user agent data that Cordova will use to build its user agent. If this
property isn't set, Cordova will use the standard web view user agent as its
base.
*/
@property (nonatomic, readwrite, copy) NSString* baseUserAgent;

/**
Takes/Gives an array of UIInterfaceOrientation (int) objects
ex. UIInterfaceOrientationPortrait
*/
@property (nonatomic, readwrite, strong) NSArray* supportedOrientations;

/**
The address of the lock token used for controlling access to setting the user-agent
*/
@property (nonatomic, readonly) NSInteger* userAgentLockToken;

- (UIView*)newCordovaViewWithFrame:(CGRect)bounds;

- (NSString*)appURLScheme;
Expand Down
Loading

0 comments on commit 20248c1

Please sign in to comment.