Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Allow headers for server communication #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ When you publish your application on the store - you pack in it all your web con
1. Publish new version of the app on the store. But it takes time, especially with the App Store.
2. Sacrifice the offline feature and load all the pages online. But as soon as Internet connection goes down - application won't work.

This plugin is intended to fix all that. When user starts the app for the first time - it copies all the web files onto the external storage. From this moment all pages are loaded from the external folder and not from the packed bundle. On every launch plugin connects to your server and checks if the new version of web project is available for download. If so - it loads it on the device and installs on the next launch.
This plugin is intended to fix all that. When user starts the app for the first time - it copies all the web files onto the external storage. From this moment all pages are loaded from the external folder and not from the packed bundle. On every launch plugin connects to your server (with optional authentication, see fetchUpdate() below) and checks if the new version of web project is available for download. If so - it loads it on the device and installs on the next launch.

As a result, your application receives updates of the web content as soon as possible, and still can work in offline mode. Also, plugin allows you to specify dependency between the web release and the native version to make sure, that new release will work on the older versions of the application.

Expand Down
3 changes: 3 additions & 0 deletions src/ios/HCPPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@
*/
- (void)jsIsUpdateAvailableForInstallation:(CDVInvokedUrlCommand *)command;


@property (nonatomic, retain) NSDictionary* headers;

@end
5 changes: 5 additions & 0 deletions src/ios/HCPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,11 @@ - (void)jsFetchUpdate:(CDVInvokedUrlCommand *)command {
if (!_isPluginReadyForWork) {
[self sendPluginNotReadyToWorkMessageForEvent:kHCPUpdateDownloadErrorEvent callbackID:command.callbackId];
}

// headers may be passed as first argument, as a dict
if (command.arguments.count == 1) {
self.headers = command.arguments[0];
}

[self _fetchUpdate:command.callbackId];
}
Expand Down
3 changes: 3 additions & 0 deletions src/ios/Network/HCPFileDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ typedef void (^HCPDataDownloadCompletionBlock)(NSData *data, NSError *error);
*/
- (void) downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL completionBlock:(HCPFileDownloadCompletionBlock)block;

// headers to add to the session
@property (nonatomic, retain) NSDictionary* headers;

@end
6 changes: 6 additions & 0 deletions src/ios/Network/HCPFileDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ @implementation HCPFileDownloader
- (void) downloadDataFromUrl:(NSURL*) url completionBlock:(HCPDataDownloadCompletionBlock) block {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
if (self.headers) {
[configuration setHTTPAdditionalHeaders:self.headers];
}
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSURLSessionDataTask* dowloadTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
Expand All @@ -28,6 +31,9 @@ - (void) downloadDataFromUrl:(NSURL*) url completionBlock:(HCPDataDownloadComple
- (void) downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder:(NSURL *)folderURL completionBlock:(HCPFileDownloadCompletionBlock)block {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
if (self.headers) {
[configuration setHTTPAdditionalHeaders:self.headers];
}
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

__block NSMutableSet* startedTasks = [NSMutableSet set];
Expand Down
3 changes: 2 additions & 1 deletion src/ios/Updater/HCPUpdateLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
* @param currentWebVersion current working version of the web content
* @param currentNativeVersion current native version of the app
* @param error error object reference; filled with data when we failed to launch the update task
* @param headers optional headers
*
* @return YES if download task is launched; NO - otherwise
*/
- (BOOL)downloadUpdateWithConfigUrl:(NSURL *)configUrl currentWebVersion:(NSString *)currentWebVersion currentNativeVersion:(NSUInteger)currentNativeVersion error:(NSError **)error;
- (BOOL)downloadUpdateWithConfigUrl:(NSURL *)configUrl currentWebVersion:(NSString *)currentWebVersion currentNativeVersion:(NSUInteger)currentNativeVersion error:(NSError **)error headers: (NSDictionary*) headers;

/**
* Flag to check if we are doing any downloads at the moment.
Expand Down
5 changes: 3 additions & 2 deletions src/ios/Updater/HCPUpdateLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ - (BOOL)isDownloadInProgress {
return _isExecuting;
}

- (BOOL)downloadUpdateWithConfigUrl:(NSURL *)configUrl currentWebVersion:(NSString *)currentWebVersion currentNativeVersion:(NSUInteger)currentNativeVersion error:(NSError **)error {
- (BOOL)downloadUpdateWithConfigUrl:(NSURL *)configUrl currentWebVersion:(NSString *)currentWebVersion currentNativeVersion:(NSUInteger)currentNativeVersion error:(NSError **)error headers:(NSDictionary*) headers {
if (_isExecuting) {
*error = [NSError errorWithCode:kHCPDownloadAlreadyInProgressErrorCode description:@"Download already in progress. Please, wait for it to finish."];
return NO;
Expand All @@ -48,7 +48,8 @@ - (BOOL)downloadUpdateWithConfigUrl:(NSURL *)configUrl currentWebVersion:(NSStri
*error = nil;
id<HCPWorker> task = [[HCPUpdateLoaderWorker alloc] initWithConfigUrl:configUrl
currentWebVersion:currentWebVersion
nativeInterfaceVersion:currentNativeVersion];
nativeInterfaceVersion:currentNativeVersion];
task.headers = headers;
[self executeTask:task];

return YES;
Expand Down
1 change: 1 addition & 0 deletions src/ios/Updater/HCPUpdateLoaderWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
*/
- (instancetype)initWithConfigUrl:(NSURL *)configURL currentWebVersion:(NSString *)currentWebVersion nativeInterfaceVersion:(NSUInteger)currentNativeVersion;

@property (nonatomic, retain) NSDictionary* headers;
@end
5 changes: 4 additions & 1 deletion src/ios/Updater/HCPUpdateLoaderWorker.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ - (void)runWithComplitionBlock:(void (^)(void))updateLoaderComplitionBlock {
}

HCPFileDownloader *configDownloader = [[HCPFileDownloader alloc] init];
configDownloader.headers = self.headers;

// download new application config
[configDownloader downloadDataFromUrl:_configURL completionBlock:^(NSData *data, NSError *error) {
Expand Down Expand Up @@ -144,7 +145,9 @@ - (void)downloadUpdatedFiles:(NSArray *)updatedFiles

// download files
HCPFileDownloader *downloader = [[HCPFileDownloader alloc] init];
// TODO: set credentials on downloader

// pass headers (auth or other)
downloader.headers = self.headers;

[downloader downloadFiles:updatedFiles
fromURL:newAppConfig.contentConfig.contentURL
Expand Down
5 changes: 3 additions & 2 deletions www/chcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ var chcp = {
* Usually this is done automatically by the plugin, but can be triggered at any time from the web page.
*
* @param {Callback(error, data)} callback - called when native side finished update process
* @param headers - provide optional headers object. This will be used to configure server request
*/
fetchUpdate: function(callback) {
callNativeMethod(pluginNativeMethod.FETCH_UPDATE, null, callback);
fetchUpdate: function(callback, headers) {
callNativeMethod(pluginNativeMethod.FETCH_UPDATE, headers, callback);
},

/**
Expand Down