Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 5b9f027

Browse files
Fix cordova-ios v6 issues and remove UIWebView support (#658)
* Fix issues with cordova ios 6 Fix blank screen appearing after CodePush update Remove support for UIWebview
1 parent 95cc846 commit 5b9f027

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

README.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ In order to ensure that your end users always have a functioning version of your
3535
Cordova 5.0.0+ is fully supported, along with the following associated platforms:
3636

3737
* Android ([cordova-android](https://github.com/apache/cordova-android) 4.0.0+) - *Including CrossWalk!* *Note: Only on TLS 1.2 compatible devices*
38-
* iOS ([cordova-ios](https://github.com/apache/cordova-ios) 3.9.0+) - *Note: In order to use CodePush along with the [`cordova-plugin-wkwebview-engine`](https://github.com/apache/cordova-plugin-wkwebview-engine) plugin, you need to install `v1.5.1-beta+` version of `cordova-plugin-code-push`, which includes full support for apps using either WebView.*
38+
* iOS ([cordova-ios](https://github.com/apache/cordova-ios) 3.9.0+) - please see notes below.
39+
40+
> Note: Starting with v2.0.0 `cordova-plugin-code-push` doesn't support apps using UIWebView due to [Apple officially deprecated it and discourage developers from using it](https://developer.apple.com/news/?id=12232019b). Prior versions of the plugin still support UIWebView but be aware that the App Store will no longer accept new apps using UIWebView as of April 2020 and app updates using UIWebView as of December 2020.
41+
42+
> Note: In order to use CodePush along with the [`cordova-plugin-wkwebview-engine`](https://github.com/apache/cordova-plugin-wkwebview-engine) plugin, you need to install `v1.5.1-beta+` version of `cordova-plugin-code-push`, which includes full support for apps using either WebView. Please see [Using WKWebView](#using-wkwebview) section for more information of how to confiure your app to use `cordova-plugin-wkwebview-engine`.
3943
4044
To check which versions of each Cordova platform you are currently using, you can run the following command and inspect the `Installed platforms` list:
4145

@@ -123,10 +127,15 @@ With the CodePush plugin installed, configure your app to use it via the followi
123127

124128
You are now ready to use the plugin in the application code. See the [sample applications](/samples) for examples and the API documentation for more details.
125129

126-
*NOTE: There is a possibility to specify WebView engine on the plugin build phase. By default UIWebView engine is used. You can force plugin to use WKWebView by adding this iOS specific preference:*
127-
```xml
128-
<preference name="WKWebViewOnly" value="true" />
129-
```
130+
### Using WKWebView
131+
132+
For cordova-ios v4-v5 there is a possibility to specify WebView engine on the plugin build phase. By default UIWebView engine is used. To use WKWebView engine please do the following:
133+
134+
* Install [cordova-plugin-wkwebview-engine](https://github.com/apache/cordova-plugin-wkwebview-engine#installation)
135+
* [Configure your app](https://github.com/apache/cordova-plugin-wkwebview-engine#required-permissions) to use WKWebView
136+
137+
> Note: `cordova-plugin-wkwebview-engine` is just a workaround for cordova-ios v4-v5 users to be able to use WKWebView in their apps to avoid stop accepting updates via AppStore as of December 2020.
138+
Cordova-ios v6+ has full support for native WKWebView and doesn't require `cordova-plugin-wkwebview-engine`.
130139

131140
## Plugin Usage
132141

src/ios/CodePush.m

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <Cordova/CDV.h>
22
#import <Cordova/CDVConfigParser.h>
33
#import <Cordova/CDVWebViewEngineProtocol.h>
4+
#import <Cordova/NSDictionary+CordovaPreferences.h>
45
#import "CodePush.h"
56
#import "CodePushPackageMetadata.h"
67
#import "CodePushPackageManager.h"
@@ -402,15 +403,17 @@ - (BOOL)loadPackage:(NSString*)packageLocation {
402403
}
403404

404405
- (void)loadURL:(NSURL*)url {
405-
// In order to make use of the "modern" Cordova platform, while still
406-
// maintaining back-compat with Cordova iOS 3.9.0, we need to conditionally
407-
// use the WebViewEngine for performing navigations only if the host app
408-
// is running 4.0.0+, and fallback to directly using the WebView otherwise.
409-
#if (WK_WEB_VIEW_ONLY && defined(__CORDOVA_4_0_0)) || defined(__CORDOVA_4_0_0)
410-
[self.webViewEngine loadRequest:[NSURLRequest requestWithURL:url]];
406+
#if WK_WEB_VIEW_ONLY && defined(__CORDOVA_4_0_0)
407+
BOOL useUiWebView = NO;
411408
#else
412-
[(UIWebView*)self.webView loadRequest:[NSURLRequest requestWithURL:url]];
409+
BOOL useUiWebView = YES;
413410
#endif
411+
if([Utilities CDVWebViewEngineAvailable] || !useUiWebView)
412+
{
413+
[self.webViewEngine loadRequest:[NSURLRequest requestWithURL:url]];
414+
} else {
415+
CPLog(@"Current version of CodePush plugin doesn't support UIWebView anymore. Please consider using version of the plugin below v2.0.0 or migrating to WkWebView. For more info please see https://developer.apple.com/news/?id=12232019b.");
416+
}
414417
}
415418

416419
+ (Boolean) hasIonicWebViewEngine:(id<CDVWebViewEngineProtocol>) webViewEngine {
@@ -475,6 +478,11 @@ - (NSURL *)getStartPageURLForLocalPackage:(NSString*)packageLocation {
475478
NSArray* realLocationArray = @[libraryLocation, @"NoCloud", packageLocation, @"www", startPage];
476479
NSString* realStartPageLocation = [NSString pathWithComponents:realLocationArray];
477480
if ([[NSFileManager defaultManager] fileExistsAtPath:realStartPageLocation]) {
481+
// Fixes WKWebView unable to load start page from CodePush update directory
482+
NSString* scheme = [self getAppScheme];
483+
if ([Utilities CDVWebViewEngineAvailable] && ([realStartPageLocation hasPrefix:@"/_app_file_"] == NO) && !([scheme isEqualToString: @"file"] || scheme == nil)) {
484+
realStartPageLocation = [@"/_app_file_" stringByAppendingString:realStartPageLocation];
485+
}
478486
return [NSURL fileURLWithPath:realStartPageLocation];
479487
}
480488
}
@@ -546,5 +554,17 @@ - (void)getAppVersion:(CDVInvokedUrlCommand *)command {
546554
}];
547555
}
548556

557+
- (NSString*)getAppScheme {
558+
NSDictionary* settings = self.commandDelegate.settings;
559+
// Cordova
560+
NSString *scheme = [settings cordovaSettingForKey:@"scheme"];
561+
if (scheme != nil) {
562+
return scheme;
563+
}
564+
// Ionic
565+
scheme = [settings cordovaSettingForKey:@"iosScheme"];
566+
return scheme;
567+
}
568+
549569
@end
550570

src/ios/CodePushReportingManager.m

-16
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,13 @@ + (void)reportStatus:(StatusReport*)statusReport withWebView:(UIView*)webView {
3434

3535
/* JS function to call: window.codePush.reportStatus(status: number, label: String, appVersion: String, deploymentKey: String) */
3636
NSString* script = [NSString stringWithFormat:@"document.addEventListener(\"deviceready\", function () { window.codePush.reportStatus(%i, %@, %@, %@, %@, %@); });", (int)statusReport.status, labelParameter, appVersionParameter, deploymentKeyParameter, lastVersionLabelOrAppVersionParameter, lastVersionDeploymentKeyParameter];
37-
#if WK_WEB_VIEW_ONLY
3837
if ([webView respondsToSelector:@selector(evaluateJavaScript:completionHandler:)]) {
3938
// The WKWebView requires JS evaluation to occur on the main
4039
// thread starting with iOS11, so ensure that we dispatch to it before executing.
4140
dispatch_async(dispatch_get_main_queue(), ^{
4241
[webView performSelector:@selector(evaluateJavaScript:completionHandler:) withObject:script withObject: NULL];
4342
});
4443
}
45-
#else
46-
if ([webView respondsToSelector:@selector(evaluateJavaScript:completionHandler:)]) {
47-
// The WKWebView requires JS evaluation to occur on the main
48-
// thread starting with iOS11, so ensure that we dispatch to it before executing.
49-
dispatch_async(dispatch_get_main_queue(), ^{
50-
[webView performSelector:@selector(evaluateJavaScript:completionHandler:) withObject:script withObject: NULL];
51-
});
52-
} else if ([webView isKindOfClass:[UIWebView class]]) {
53-
// The UIWebView requires JS evaluation to occur on the main
54-
// thread, so ensure that we dispatch to it before executing.
55-
dispatch_async(dispatch_get_main_queue(), ^{
56-
[(UIWebView*)webView stringByEvaluatingJavaScriptFromString:script];
57-
});
58-
}
59-
#endif
6044
}
6145
}
6246

src/ios/Utilities.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
+ (NSString*)getApplicationVersion;
44
+ (NSString*)getApplicationTimestamp;
55
+ (NSDate*)getApplicationBuildTime;
6+
+ (BOOL)CDVWebViewEngineAvailable;
67

78
@end

src/ios/Utilities.m

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
@implementation Utilities
44

5+
static NSNumber* CDVWebViewEngineExists = nil;
6+
57
+ (NSString*)getApplicationVersion{
68
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
79
}
@@ -24,6 +26,14 @@ + (NSDate*)getApplicationBuildTime{
2426
return fileDate;
2527
}
2628

29+
+ (BOOL)CDVWebViewEngineAvailable{
30+
if(CDVWebViewEngineExists == nil) {
31+
BOOL value = NSClassFromString(@"CDVWebViewEngine") != nil;
32+
CDVWebViewEngineExists = [NSNumber numberWithBool:value];
33+
}
34+
return [CDVWebViewEngineExists boolValue];
35+
}
36+
2737
void CPLog(NSString *formatString, ...) {
2838
va_list args;
2939
va_start(args, formatString);

0 commit comments

Comments
 (0)