Skip to content

Commit

Permalink
Merge pull request #2 from Telerik-Verified-Plugins/bektchiev/merge-1…
Browse files Browse the repository at this point in the history
….1.1

Merge tag 1.1.1 from apache
  • Loading branch information
Martin Bektchiev authored Jan 31, 2017
2 parents aa5a9d5 + dc1031d commit 04a5524
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 83 deletions.
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Thanks!


### Checklist
- [ ] [ICLA](http://www.apache.org/licenses/icla.txt) has been signed and submitted to secretary@apache.org.
- [ ] [Reported an issue](http://cordova.apache.org/contribute/issues.html) in the JIRA database
- [ ] Commit message follows the format: "CB-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected.
- [ ] Added automated test coverage as appropriate for this change.
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
language: node_js
osx_image: xcode7.3
language: objective-c
sudo: false
node_js:
- "4.2"
before_install:
- npm cache clean -f
- npm install -g n
- n stable
- node --version
install:
- npm install
script:
- npm test
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ To test the development version:
cordova platform add https://github.com/apache/cordova-ios.git#master
cordova plugin add https://github.com/apache/cordova-plugin-wkwebview-engine.git#master

You also must have Xcode 7 (iOS 9 SDK) installed. Check your Xcode version by running:
You also must have at least Xcode 7 (iOS 9 SDK) installed. Check your Xcode version by running:

xcode-select --print-path

Required Permissions
-----------
WKWebView may not fully launch (the deviceready event may not fire) unless if the following is included in config.xml:
WKWebView may not fully launch (the deviceready event may not fire) unless if the following is included in config.xml. This should already be installed by Cordova in your platform config.xml when the plugin is installed.

#### config.xml

<feature name="CDVWKWebViewEngine">
Expand All @@ -71,7 +72,9 @@ We have an [experimental plugin](https://github.com/apache/cordova-plugins/tree/
Application Transport Security (ATS) in iOS 9
-----------

The next released version of the [cordova-cli 5.4.0](https://www.npmjs.com/package/cordova) will support automatic conversion of the [&lt;access&gt;](http://cordova.apache.org/docs/en/edge/guide/appdev/whitelist/index.html) tags in config.xml to Application Transport Security [ATS](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33) directives. Upgrade to the version 5.4.0 to use this new functionality.
Starting with [cordova-cli 5.4.0](https://www.npmjs.com/package/cordova), it will support automatic conversion of the [&lt;access&gt;](http://cordova.apache.org/docs/en/edge/guide/appdev/whitelist/index.html) tags in config.xml to Application Transport Security [ATS](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33) directives.

Upgrade to at least version 5.4.0 of the cordova-cli to use this new functionality.

Limitations
--------
Expand Down
56 changes: 41 additions & 15 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,34 @@

# Release Notes

### 1.1.1 (Dec 07, 2016)
* Updated README.md
* added check for at least iOS 9.0
* CB-10228:(iOS) AppendUserAgent not working with WKWebView
* [CB-11997](https://issues.apache.org/jira/browse/CB-11997) Code review comments
* [CB-11997](https://issues.apache.org/jira/browse/CB-11997) Add crash recovery for iOS 8
* [CB-11917](https://issues.apache.org/jira/browse/CB-11917) - Remove pull request template checklist item: "iCLA has been submitted…"
* [CB-11818](https://issues.apache.org/jira/browse/CB-11818) - Avoid retain cycle: WKUserContentController retains its message handler, to break it we cannot pass directly CDVWKWebViewEngine's instance
* [CB-11832](https://issues.apache.org/jira/browse/CB-11832) Incremented plugin version.

### 1.1.0 (Sep 08, 2016)
* [CB-11824](https://issues.apache.org/jira/browse/CB-11824) - Update tests to include objective-c tests
* [CB-11554](https://issues.apache.org/jira/browse/CB-11554) - fixed unit tests
* [CB-11815](https://issues.apache.org/jira/browse/CB-11815) (**iOS**) Fix hard-coded bridge name "cordova"
* [CB-11554](https://issues.apache.org/jira/browse/CB-11554) - too 'brutal' app reload when title is empty
* [CB-11074](https://issues.apache.org/jira/browse/CB-11074) - Ensure settings from `config.xml` are taken into consideration
* Add ability to set the deceleration rate for the scrollview to 'fast'
* [CB-11496](https://issues.apache.org/jira/browse/CB-11496) - Add obj-c unit tests for `WKWebViewConfiguration`, `WKPreference`
* [CB-11496](https://issues.apache.org/jira/browse/CB-11496) - Create Obj-C unit-tests for `wkwebview-engine` (fix linker error)
* [CB-11452](https://issues.apache.org/jira/browse/CB-11452) - Update README.md with latest news about `AllowInlineMediaPlayback` fix
* [CB-9888](https://issues.apache.org/jira/browse/CB-9888) (**iOS**) check & reload `WKWebView`
* [CB-11375](https://issues.apache.org/jira/browse/CB-11375) - `onReset` method of `CDVPlugin` is never called
* Add pull request template.
* [CB-10818](https://issues.apache.org/jira/browse/CB-10818) - Support the scroll deceleration speed preference.
* [CB-10817](https://issues.apache.org/jira/browse/CB-10817) - Will now reload the `webView` if a crash occurs

### 1.0.3 (Apr 15, 2016)
* CB-10636 Add `JSHint` for plugins
* [CB-10636](https://issues.apache.org/jira/browse/CB-10636) Add `JSHint` for plugins

### 1.0.2 (Feb 09, 2016)
* [CB-10269](https://issues.apache.org/jira/browse/CB-10269) - Replace cordova exec only when present in wkwebview
Expand All @@ -31,22 +57,22 @@

### 1.0.1 (Dec 11, 2015)

* CB-10190 - WKWebView engine is not releasing the user-agent lock
* [CB-10190](https://issues.apache.org/jira/browse/CB-10190) - WKWebView engine is not releasing the user-agent lock

### 1.0.0 (Dec 04, 2015)

* CB-10146 - Add to README WKWebViewEngine quirks that will affect migration from UIWebView
* CB-10133 - DataClone DOM Exception 25 thrown for postMessage
* CB-10106 - added bridge proxy
* CB-10107 - nativeEvalAndFetch called for all bridges
* CB-10106 - iOS bridges need to take into account bridge changes
* CB-10073 - WKWebViewEngine should post CDVPluginResetNotification
* CB-10035 Updated RELEASENOTES to be newest to oldest
* CB-10002 - WKWebView should propagate shouldOverrideLoadWithRequest to plugins
* CB-9979 CB-9972 Change ATS link to new link
* CB-9636 - Plugin should detect at runtime iOS 8 and use of file:// url and present an error
* CB-8839 - WKWebView ignores DisallowOverscroll preference
* CB-8556 - fix handleOpenURL for WKWebViewEngine plugin
* CB-8666 - Update CDVWKWebViewEngine plugin to use 4.0.x branch code
* [CB-10146](https://issues.apache.org/jira/browse/CB-10146) - Add to README WKWebViewEngine quirks that will affect migration from UIWebView
* [CB-10133](https://issues.apache.org/jira/browse/CB-10133) - DataClone DOM Exception 25 thrown for postMessage
* [CB-10106](https://issues.apache.org/jira/browse/CB-10106) - added bridge proxy
* [CB-10107](https://issues.apache.org/jira/browse/CB-10107) - nativeEvalAndFetch called for all bridges
* [CB-10106](https://issues.apache.org/jira/browse/CB-10106) - iOS bridges need to take into account bridge changes
* [CB-10073](https://issues.apache.org/jira/browse/CB-10073) - WKWebViewEngine should post CDVPluginResetNotification
* [CB-10035](https://issues.apache.org/jira/browse/CB-10035) Updated RELEASENOTES to be newest to oldest
* [CB-10002](https://issues.apache.org/jira/browse/CB-10002) - WKWebView should propagate shouldOverrideLoadWithRequest to plugins
* [CB-9979](https://issues.apache.org/jira/browse/CB-9979) [CB-9972](https://issues.apache.org/jira/browse/CB-9972) Change ATS link to new link
* [CB-9636](https://issues.apache.org/jira/browse/CB-9636) - Plugin should detect at runtime iOS 8 and use of file:// url and present an error
* [CB-8839](https://issues.apache.org/jira/browse/CB-8839) - WKWebView ignores DisallowOverscroll preference
* [CB-8556](https://issues.apache.org/jira/browse/CB-8556) - fix handleOpenURL for WKWebViewEngine plugin
* [CB-8666](https://issues.apache.org/jira/browse/CB-8666) - Update CDVWKWebViewEngine plugin to use 4.0.x branch code


11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "cordova-plugin-wkwebview-engine",
"version": "1.0.4-dev",
"version": "1.1.1",
"description": "The official Apache Cordova WKWebView Engine Plugin",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine.git"
Expand All @@ -15,8 +12,10 @@
"wkwebview"
],
"scripts": {
"test": "npm run jshint",
"jshint": "node node_modules/jshint/bin/jshint src"
"test": "npm run jshint && npm run objc-tests",
"objc-tests": "cd tests/ios && npm test",
"preobjc-tests": "cd tests/ios && npm install",
"jshint": "node_modules/.bin/jshint src"
},
"author": "Apache Cordova",
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-wkwebview-engine"
version="1.0.4-dev">
version="1.1.1">
<name>Cordova WKWebView Engine</name>
<description>Cordova WKWebView Engine Plugin</description>
<license>Apache 2.0</license>
Expand Down
149 changes: 117 additions & 32 deletions src/ios/CDVWKWebViewEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ Licensed to the Apache Software Foundation (ASF) under one
#define CDV_BRIDGE_NAME @"cordova"
#define CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR @"loadFileURL:allowingReadAccessToURL:"

@interface CDVWKWeakScriptMessageHandler : NSObject <WKScriptMessageHandler>

@property (nonatomic, weak, readonly) id<WKScriptMessageHandler>scriptMessageHandler;

- (instancetype)initWithScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler;

@end


@interface CDVWKWebViewEngine ()

@property (nonatomic, strong, readwrite) UIView* engineWebView;
@property (nonatomic, strong, readwrite) id <WKUIDelegate> uiDelegate;
@property (nonatomic, weak) id <WKScriptMessageHandler> weakScriptMessageHandler;

@end

Expand All @@ -47,31 +57,51 @@ - (instancetype)initWithFrame:(CGRect)frame
if (NSClassFromString(@"WKWebView") == nil) {
return nil;
}
self.uiDelegate = [[CDVWKWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];

WKUserContentController* userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:self name:CDV_BRIDGE_NAME];

WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;

WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];

wkWebView.UIDelegate = self.uiDelegate;
self.engineWebView = [[WKWebView alloc] initWithFrame:frame];
}

self.engineWebView = wkWebView;
return self;
}

NSLog(@"Using WKWebView");
- (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)settings
{
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
if (settings == nil) {
return configuration;
}

return self;
configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:NO];
configuration.mediaPlaybackRequiresUserAction = [settings cordovaBoolSettingForKey:@"MediaPlaybackRequiresUserAction" defaultValue:YES];
configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO];
configuration.mediaPlaybackAllowsAirPlay = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES];

return configuration;
}

- (void)pluginInitialize
{
// viewController would be available now. we attempt to set all possible delegates to it, by default
NSDictionary* settings = self.commandDelegate.settings;

WKWebView* wkWebView = (WKWebView*)_engineWebView;
self.uiDelegate = [[CDVWKWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];

CDVWKWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWKWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self];

WKUserContentController* userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];

WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];
configuration.userContentController = userContentController;

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

if (IsAtLeastiOSVersion(@"9.0") && [self.viewController isKindOfClass:[CDVViewController class]]) {
wkWebView.customUserAgent = ((CDVViewController*) self.viewController).userAgent;
}

if ([self.viewController conformsToProtocol:@protocol(WKUIDelegate)]) {
wkWebView.UIDelegate = (id <WKUIDelegate>)self.viewController;
Expand All @@ -84,42 +114,78 @@ - (void)pluginInitialize
}

if ([self.viewController conformsToProtocol:@protocol(WKScriptMessageHandler)]) {
[wkWebView.configuration.userContentController addScriptMessageHandler:(id < WKScriptMessageHandler >)self.viewController name:@"cordova"];
[wkWebView.configuration.userContentController addScriptMessageHandler:(id < WKScriptMessageHandler >)self.viewController name:CDV_BRIDGE_NAME];
}

[self updateSettings:self.commandDelegate.settings];
[self updateSettings:settings];

// check if content thread has died on resume
NSLog(@"%@", @"CDVWKWebViewEngine will reload WKWebView if required on resume");
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onAppWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification object:nil];

NSLog(@"Using WKWebView");

[self addURLObserver];
}

- (void)onReset {
[self addURLObserver];
}

static void * KVOContext = &KVOContext;

- (void)addURLObserver {
if(!IsAtLeastiOSVersion(@"9.0")){
[self.webView addObserver:self forKeyPath:@"URL" options:0 context:KVOContext];
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (context == KVOContext) {
if (object == [self webView] && [keyPath isEqualToString: @"URL"] && [object valueForKeyPath:keyPath] == nil){
NSLog(@"URL is nil. Reloading WKWebView");
[(WKWebView*)_engineWebView reload];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void) onAppWillEnterForeground:(NSNotification*)notification {
[self reloadIfRequired];
if ([self shouldReloadWebView]) {
NSLog(@"%@", @"CDVWKWebViewEngine reloading!");
[(WKWebView*)_engineWebView reload];
}
}

- (BOOL)reloadIfRequired
- (BOOL)shouldReloadWebView
{
WKWebView* wkWebView = (WKWebView*)_engineWebView;
NSString* title = wkWebView.title;
BOOL reload = ((title == nil) || [title isEqualToString:@""]);
return [self shouldReloadWebView:wkWebView.URL title:wkWebView.title];
}

- (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title
{
BOOL title_is_nil = (title == nil);
BOOL location_is_blank = [[location absoluteString] isEqualToString:@"about:blank"];

BOOL reload = (title_is_nil || location_is_blank);

#ifdef DEBUG
NSLog(@"%@", @"CDVWKWebViewEngine reloadIfRequired");
NSLog(@"CDVWKWebViewEngine reloadIfRequired WKWebView.title: %@", title);
NSLog(@"CDVWKWebViewEngine reloadIfRequired reload: %u", reload);
NSLog(@"%@", @"CDVWKWebViewEngine shouldReloadWebView::");
NSLog(@"CDVWKWebViewEngine shouldReloadWebView title: %@", title);
NSLog(@"CDVWKWebViewEngine shouldReloadWebView location: %@", [location absoluteString]);
NSLog(@"CDVWKWebViewEngine shouldReloadWebView reload: %u", reload);
#endif

if (reload) {
NSLog(@"%@", @"CDVWKWebViewEngine reloading!");
[wkWebView reload];
}

return reload;
}


- (id)loadRequest:(NSURLRequest*)request
{
if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes
Expand Down Expand Up @@ -174,10 +240,6 @@ - (void)updateSettings:(NSDictionary*)settings
WKWebView* wkWebView = (WKWebView*)_engineWebView;

wkWebView.configuration.preferences.minimumFontSize = [settings cordovaFloatSettingForKey:@"MinimumFontSize" defaultValue:0.0];
wkWebView.configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:NO];
wkWebView.configuration.mediaPlaybackRequiresUserAction = [settings cordovaBoolSettingForKey:@"MediaPlaybackRequiresUserAction" defaultValue:YES];
wkWebView.configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO];
wkWebView.configuration.mediaPlaybackAllowsAirPlay = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES];

/*
wkWebView.configuration.preferences.javaScriptEnabled = [settings cordovaBoolSettingForKey:@"JavaScriptEnabled" default:YES];
Expand Down Expand Up @@ -208,6 +270,8 @@ - (void)updateSettings:(NSDictionary*)settings

if (![@"fast" isEqualToString:decelerationSetting]) {
[wkWebView.scrollView setDecelerationRate:UIScrollViewDecelerationRateNormal];
} else {
[wkWebView.scrollView setDecelerationRate:UIScrollViewDecelerationRateFast];
}
}

Expand Down Expand Up @@ -379,4 +443,25 @@ - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavi

return decisionHandler(NO);
}

@end

#pragma mark - CDVWKWeakScriptMessageHandler

@implementation CDVWKWeakScriptMessageHandler

- (instancetype)initWithScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler
{
self = [super init];
if (self) {
_scriptMessageHandler = scriptMessageHandler;
}
return self;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptMessageHandler userContentController:userContentController didReceiveScriptMessage:message];
}

@end
Loading

0 comments on commit 04a5524

Please sign in to comment.