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

Commit 95cc846

Browse files
Revert "Fix cordova-ios 6 issue with file:// requests being rejected (#655)" (#657)
This reverts commit 94a18fa.
1 parent 94a18fa commit 95cc846

File tree

8 files changed

+80
-248
lines changed

8 files changed

+80
-248
lines changed

plugin.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@
9898
<header-file src="src/ios/Utilities.h" />
9999
<source-file src="src/ios/Utilities.m" />
100100
<source-file src="src/ios/CDVWKWebViewEngine+CodePush.m" />
101-
<source-file src="src/ios/WebViewShared.m" />
102-
<source-file src="src/ios/WebViewShared.h" />
103-
<source-file src="src/ios/CDVViewController+CodePush.m" />
104-
101+
105102
<!-- JWT -->
106103
<source-file src="src/ios/JWT/Core/Algorithms/Base/CodePushJWTAlgorithmFactory.m" />
107104
<source-file src="src/ios/JWT/Core/Algorithms/Base/CodePushJWTAlgorithmNone.m" />

src/ios/CDVViewController+CodePush.m

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/ios/CDVWKWebViewEngine+CodePush.m

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,94 @@
11
#if defined(__has_include)
22
#if __has_include("CDVWKWebViewEngine.h")
33

4+
#import <Cordova/NSDictionary+CordovaPreferences.h>
45
#import "CDVWKWebViewEngine.h"
5-
#import "WebViewShared.h"
6+
#import "CodePush.h"
67

78
@implementation CDVWKWebViewEngine (CodePush)
89

10+
NSString* const IdentifierCodePushPath = @"codepush/deploy/versions";
911
NSString* lastLoadedURL = @"";
1012

1113
#pragma clang diagnostic push
1214
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
1315

1416
- (id)loadRequest:(NSURLRequest *)request {
1517
lastLoadedURL = request.URL.absoluteString;
16-
WebViewShared* webViewShared = [WebViewShared getInstanceOrCreate:self.webViewEngine
17-
andCommandDelegate:self.commandDelegate
18-
andViewController:self.viewController];
19-
return [webViewShared loadRequest:request];
18+
NSURL *readAccessURL;
19+
20+
NSURL* bundleURL = [[NSBundle mainBundle] bundleURL];
21+
if (![lastLoadedURL containsString:bundleURL.path] && ![lastLoadedURL containsString:IdentifierCodePushPath]) {
22+
return [self loadPluginRequest:request];
23+
}
24+
25+
if (request.URL.isFileURL) {
26+
// All file URL requests should be handled with the setServerBasePath in case if it is Ionic app.
27+
if ([CodePush hasIonicWebViewEngine: self]) {
28+
NSString* specifiedServerPath = [CodePush getCurrentServerBasePath];
29+
if (![specifiedServerPath containsString:IdentifierCodePushPath] || [request.URL.path containsString:IdentifierCodePushPath]) {
30+
[CodePush setServerBasePath:request.URL.path webView: self];
31+
}
32+
33+
return nil;
34+
}
35+
36+
if ([request.URL.absoluteString containsString:IdentifierCodePushPath]) {
37+
// If the app is attempting to load a CodePush update, then we can lock the WebView down to
38+
// just the CodePush "versions" directory. This prevents non-CodePush assets from being accessible,
39+
// while still allowing us to navigate to a future update, as well as to the binary if a rollback is needed.
40+
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
41+
readAccessURL = [NSURL fileURLWithPathComponents:@[libraryPath, @"NoCloud", @"codepush", @"deploy", @"versions"]];
42+
} else {
43+
// In order to allow the WebView to be navigated from the app bundle to another location, we (for some
44+
// entirely unknown reason) need to ensure that the "read access URL" is set to the parent of the bundle
45+
// as opposed to the www folder, which is what the WKWebViewEngine would attempt to set it to by default.
46+
// If we didn't set this, then the attempt to navigate from the bundle to a CodePush update would fail.
47+
readAccessURL = [[[NSBundle mainBundle] bundleURL] URLByDeletingLastPathComponent];
48+
}
49+
50+
return [(WKWebView*)self.engineWebView loadFileURL:request.URL allowingReadAccessToURL:readAccessURL];
51+
} else {
52+
return [(WKWebView*)self.engineWebView loadRequest: request];
53+
}
54+
}
55+
56+
- (id)loadPluginRequest:(NSURLRequest *)request {
57+
if (request.URL.fileURL) {
58+
NSDictionary* settings = self.commandDelegate.settings;
59+
NSString *bind = [settings cordovaSettingForKey:@"Hostname"];
60+
if(bind == nil){
61+
bind = @"localhost";
62+
}
63+
NSString *scheme = [settings cordovaSettingForKey:@"iosScheme"];
64+
if(scheme == nil || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"file"]){
65+
scheme = @"ionic";
66+
}
67+
NSString *CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@", scheme, bind];
68+
69+
NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage];
70+
NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
71+
NSURL *url = [[NSURL URLWithString:CDV_LOCAL_SERVER] URLByAppendingPathComponent:request.URL.path];
72+
if ([request.URL.path isEqualToString:startFilePath]) {
73+
url = [NSURL URLWithString:CDV_LOCAL_SERVER];
74+
}
75+
if(request.URL.query) {
76+
url = [NSURL URLWithString:[@"?" stringByAppendingString:request.URL.query] relativeToURL:url];
77+
}
78+
if(request.URL.fragment) {
79+
url = [NSURL URLWithString:[@"#" stringByAppendingString:request.URL.fragment] relativeToURL:url];
80+
}
81+
request = [NSURLRequest requestWithURL:url];
82+
}
83+
return [(WKWebView*)self.engineWebView loadRequest:request];
2084
}
2185

2286
#pragma clang diagnostic pop
2387

2488
// Fix bug related to unable WKWebView recovery after reinit with loaded codepush update
2589
- (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigation withError:(NSError*)error {
2690
// NSURLErrorFailingURLStringErrorKey is URL which caused a load to fail, if it's null then webView was terminated for some reason
27-
if ([[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey] == nil && [lastLoadedURL containsString:[WebViewShared getIdentifierCodePushPath]]) {
91+
if ([[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey] == nil && [lastLoadedURL containsString:IdentifierCodePushPath]) {
2892
NSLog(@"Failed to load webpage with error: %@", [error localizedDescription]);
2993
NSLog(@"Trying to reload request with url: %@", lastLoadedURL);
3094
// Manually loading codepush start page via loadRequest method of this category

src/ios/CodePush.m

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#import "StatusReport.h"
1212
#import "UpdateHashUtils.h"
1313
#import "CodePushJWT.h"
14-
#import "WebViewShared.h"
1514

1615
@implementation CodePush
1716

@@ -22,7 +21,6 @@ @implementation CodePush
2221
NSString* const DeploymentKeyPreference = @"codepushdeploymentkey";
2322
NSString* const PublicKeyPreference = @"codepushpublickey";
2423
StatusReport* rollbackStatusReport = nil;
25-
WebViewShared* webViewShared = nil;
2624

2725
- (void)getBinaryHash:(CDVInvokedUrlCommand *)command {
2826
[self.commandDelegate runInBackground:^{
@@ -353,9 +351,6 @@ - (void)navigateToLocalDeploymentIfExists {
353351
}
354352

355353
- (void)pluginInitialize {
356-
webViewShared = [WebViewShared getInstanceOrCreate:self.webViewEngine
357-
andCommandDelegate:self.commandDelegate
358-
andViewController:self.viewController];
359354
// register for "on resume", "on pause" notifications
360355
[self clearDeploymentsIfBinaryUpdated];
361356
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
@@ -407,20 +402,15 @@ - (BOOL)loadPackage:(NSString*)packageLocation {
407402
}
408403

409404
- (void)loadURL:(NSURL*)url {
410-
// Fix file:// requests issues for cordova-ios 6+
411-
if([Utilities CDVWebViewEngineAvailable]) {
412-
[webViewShared loadRequest:[NSURLRequest requestWithURL:url]];
413-
} else {
414-
// In order to make use of the "modern" Cordova platform, while still
415-
// maintaining back-compat with Cordova iOS 3.9.0, we need to conditionally
416-
// use the WebViewEngine for performing navigations only if the host app
417-
// is running 4.0.0+, and fallback to directly using the WebView otherwise.
418-
#if (WK_WEB_VIEW_ONLY && defined(__CORDOVA_4_0_0)) || defined(__CORDOVA_4_0_0)
419-
[self.webViewEngine loadRequest:[NSURLRequest requestWithURL:url]];
420-
#else
421-
[(UIWebView*)self.webView loadRequest:[NSURLRequest requestWithURL:url]];
422-
#endif
423-
}
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]];
411+
#else
412+
[(UIWebView*)self.webView loadRequest:[NSURLRequest requestWithURL:url]];
413+
#endif
424414
}
425415

426416
+ (Boolean) hasIonicWebViewEngine:(id<CDVWebViewEngineProtocol>) webViewEngine {

src/ios/Utilities.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
+ (NSString*)getApplicationVersion;
44
+ (NSString*)getApplicationTimestamp;
55
+ (NSDate*)getApplicationBuildTime;
6-
+ (BOOL)CDVWebViewEngineAvailable;
76

87
@end

src/ios/Utilities.m

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
@implementation Utilities
44

5-
static BOOL CDVWebViewEngineChecked = NO;
6-
static BOOL CDVWebViewEngineExists = NO;
7-
85
+ (NSString*)getApplicationVersion{
96
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
107
}
@@ -27,14 +24,6 @@ + (NSDate*)getApplicationBuildTime{
2724
return fileDate;
2825
}
2926

30-
+ (BOOL)CDVWebViewEngineAvailable{
31-
if (!CDVWebViewEngineChecked) {
32-
CDVWebViewEngineChecked = YES;
33-
CDVWebViewEngineExists = NSClassFromString(@"CDVWebViewEngine") != nil;
34-
}
35-
return CDVWebViewEngineExists;
36-
}
37-
3827
void CPLog(NSString *formatString, ...) {
3928
va_list args;
4029
va_start(args, formatString);

src/ios/WebViewShared.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)