From afb6a04ad092b69b20366da8ac4e6d8f1ffdbe32 Mon Sep 17 00:00:00 2001 From: Nikolay Demyankov Date: Sat, 10 Oct 2015 17:24:56 +0200 Subject: [PATCH] Changed refresh process after update installation. Should fix https://github.com/nordnet/cordova-hot-code-push/issues/2 --- src/ios/HCPPlugin.m | 52 ++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/src/ios/HCPPlugin.m b/src/ios/HCPPlugin.m index ab4d6849..c736b9f0 100644 --- a/src/ios/HCPPlugin.m +++ b/src/ios/HCPPlugin.m @@ -36,7 +36,6 @@ @interface HCPPlugin() { HCPApplicationConfig *_appConfig; HCPAppUpdateRequestAlertDialog *_appUpdateRequestDialog; SocketIOClient *_socketIOClient; - BOOL _shouldFlushCache; } @end @@ -225,7 +224,22 @@ - (BOOL)_installUpdate:(NSString *)callbackID { */ - (void)loadURL:(NSURL *)url { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [self.webView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0]]; + NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:YES]; + NSString *path = components.path; + NSURL *loadURL = [NSURL fileURLWithPath:path]; + + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [self.webView loadRequest:[NSURLRequest requestWithURL:loadURL + cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData + timeoutInterval:60.0]]; + + // We need to reload the page because of the webview caching. + // For example, if we loaded new css file - it is not gonna update, bacuse old version is cached and the file path is the same. + // But if we reload page - everything is fine. This is hacky, but it is the only way to reset the cache. + // Delay is set, because if we try to reload immidiatly - nothing good will happen. + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self.webView reload]; + }); }]; } @@ -332,20 +346,9 @@ - (void)invokeDefaultCallbackWithMessage:(CDVPluginResult *)result { */ - (void)subscribeToEvents { [self subscribeToLifecycleEvents]; - [self subscribeToCordovaEvents]; [self subscribeToPluginInternalEvents]; } -/** - * Subscribe to Cordova events. - */ -- (void)subscribeToCordovaEvents { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(webViewDidLoadPage:) - name:CDVPageDidLoadNotification - object:nil]; -} - /** * Subscribe to lifecycle events. */ @@ -409,28 +412,6 @@ - (void)unsubscribeFromEvents { [[NSNotificationCenter defaultCenter] removeObserver:self]; } -#pragma mark Cordova events - -/** - * Method is called when WebView finished loading the page - * - * @param notification captured notification - */ -- (void)webViewDidLoadPage:(NSNotification *)notification { - if (!_shouldFlushCache) { - return; - } - - // In order to force js/css/images update we need to call "reload" method. - // Don't know why, but only after that resource files get updated. - // There is no such problem with html files. - _shouldFlushCache = NO; - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [self.webView reload]; - }]; -} - - #pragma mark Bundle installation events /** @@ -591,7 +572,6 @@ - (void)onUpdateInstalledEvent:(NSNotification *)notification { // reload application to the index page NSURL *startingPageURL = [self appendWwwFolderPathToPath:[self getStartingPagePath]]; - _shouldFlushCache = YES; [self loadURL:startingPageURL]; }