@@ -15,10 +15,6 @@ @implementation CodePush {
15
15
16
16
#pragma mark - Private constants
17
17
18
- static BOOL needToReportRollback = NO ;
19
- static BOOL isRunningBinaryVersion = NO ;
20
- static BOOL testConfigurationFlag = NO ;
21
-
22
18
// These constants represent valid deployment statuses
23
19
static NSString *const DeploymentFailed = @" DeploymentFailed" ;
24
20
static NSString *const DeploymentSucceeded = @" DeploymentSucceeded" ;
@@ -34,52 +30,64 @@ @implementation CodePush {
34
30
35
31
// These keys are used to inspect/augment the metadata
36
32
// that is associated with an update's package.
33
+ static NSString *const BinaryBundleDateKey = @" binaryDate" ;
37
34
static NSString *const PackageHashKey = @" packageHash" ;
38
35
static NSString *const PackageIsPendingKey = @" isPending" ;
39
36
37
+ #pragma mark - Static variables
38
+
39
+ static BOOL isRunningBinaryVersion = NO ;
40
+ static BOOL needToReportRollback = NO ;
41
+ static BOOL testConfigurationFlag = NO ;
42
+
43
+ // These values are used to save the bundleURL and extension for the JS bundle
44
+ // in the binary.
45
+ static NSString *bundleResourceExtension = @" jsbundle" ;
46
+ static NSString *bundleResourceName = @" main" ;
47
+
40
48
#pragma mark - Public Obj-C API
41
49
42
50
+ (NSURL *)bundleURL
43
51
{
44
- return [self bundleURLForResource: @" main " ];
52
+ return [self bundleURLForResource: bundleResourceName ];
45
53
}
46
54
47
55
+ (NSURL *)bundleURLForResource : (NSString *)resourceName
48
56
{
57
+ bundleResourceName = resourceName;
49
58
return [self bundleURLForResource: resourceName
50
- withExtension: @" jsbundle " ];
59
+ withExtension: bundleResourceExtension ];
51
60
}
52
61
53
62
+ (NSURL *)bundleURLForResource : (NSString *)resourceName
54
63
withExtension : (NSString *)resourceExtension
55
64
{
65
+ bundleResourceName = resourceName;
66
+ bundleResourceExtension = resourceExtension;
56
67
NSError *error;
57
68
NSString *packageFile = [CodePushPackage getCurrentPackageBundlePath: &error];
58
- NSURL *binaryJsBundleUrl = [[ NSBundle mainBundle ] URLForResource: resourceName withExtension: resourceExtension ];
69
+ NSURL *binaryBundleURL = [self binaryBundleURL ];
59
70
60
71
NSString *logMessageFormat = @" Loading JS bundle from %@ " ;
61
72
62
73
if (error || !packageFile) {
63
- NSLog (logMessageFormat, binaryJsBundleUrl );
74
+ NSLog (logMessageFormat, binaryBundleURL );
64
75
isRunningBinaryVersion = YES ;
65
- return binaryJsBundleUrl ;
76
+ return binaryBundleURL ;
66
77
}
67
78
68
- NSDictionary *binaryFileAttributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: [binaryJsBundleUrl path ] error: nil ];
69
- NSDictionary *appFileAttribs = [[NSFileManager defaultManager ] attributesOfItemAtPath: packageFile error: nil ];
70
- NSDate *binaryDate = [binaryFileAttributes objectForKey: NSFileModificationDate ];
71
- NSDate *packageDate = [appFileAttribs objectForKey: NSFileModificationDate ];
72
79
NSString *binaryAppVersion = [[CodePushConfig current ] appVersion ];
73
80
NSDictionary *currentPackageMetadata = [CodePushPackage getCurrentPackage: &error];
74
81
if (error || !currentPackageMetadata) {
75
- NSLog (logMessageFormat, binaryJsBundleUrl );
82
+ NSLog (logMessageFormat, binaryBundleURL );
76
83
isRunningBinaryVersion = YES ;
77
- return binaryJsBundleUrl ;
84
+ return binaryBundleURL ;
78
85
}
79
86
87
+ NSString *packageDate = [currentPackageMetadata objectForKey: BinaryBundleDateKey];
80
88
NSString *packageAppVersion = [currentPackageMetadata objectForKey: @" appVersion" ];
81
89
82
- if ([binaryDate compare: packageDate] == NSOrderedAscending && ([CodePush isUsingTestConfiguration ] ||[binaryAppVersion isEqualToString: packageAppVersion])) {
90
+ if ([[ self modifiedDateStringOfFileAtURL: binaryBundleURL] isEqualToString: packageDate] && ([CodePush isUsingTestConfiguration ] ||[binaryAppVersion isEqualToString: packageAppVersion])) {
83
91
// Return package file because it is newer than the app store binary's JS bundle
84
92
NSURL *packageUrl = [[NSURL alloc ] initFileURLWithPath: packageFile];
85
93
NSLog (logMessageFormat, packageUrl);
@@ -90,9 +98,9 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
90
98
[CodePush clearUpdates ];
91
99
#endif
92
100
93
- NSLog (logMessageFormat, binaryJsBundleUrl );
101
+ NSLog (logMessageFormat, binaryBundleURL );
94
102
isRunningBinaryVersion = YES ;
95
- return binaryJsBundleUrl ;
103
+ return binaryBundleURL ;
96
104
}
97
105
}
98
106
@@ -141,6 +149,11 @@ + (void)clearUpdates
141
149
142
150
@synthesize bridge = _bridge;
143
151
152
+ + (NSURL *)binaryBundleURL
153
+ {
154
+ return [[NSBundle mainBundle ] URLForResource: bundleResourceName withExtension: bundleResourceExtension];
155
+ }
156
+
144
157
/*
145
158
* This method is used by the React Native bridge to allow
146
159
* our plugin to expose constants to the JS-side. In our case
@@ -273,6 +286,20 @@ - (void)loadBundle
273
286
});
274
287
}
275
288
289
+ /*
290
+ * This returns the modified date as a string for a given file URL.
291
+ */
292
+ + (NSString *)modifiedDateStringOfFileAtURL : (NSURL *)fileURL
293
+ {
294
+ if (fileURL != nil ) {
295
+ NSDictionary *fileAttributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: [fileURL path ] error: nil ];
296
+ NSDate *modifiedDate = [fileAttributes objectForKey: NSFileModificationDate ];
297
+ return [NSString stringWithFormat: @" %f " , [modifiedDate timeIntervalSince1970 ]];
298
+ } else {
299
+ return nil ;
300
+ }
301
+ }
302
+
276
303
/*
277
304
* This method is used when an update has failed installation
278
305
* and the app needs to be rolled back to the previous bundle.
@@ -367,7 +394,14 @@ - (void)savePendingUpdate:(NSString *)packageHash
367
394
rejecter:(RCTPromiseRejectBlock)reject)
368
395
{
369
396
dispatch_async (dispatch_get_main_queue (), ^{
370
- [CodePushPackage downloadPackage: updatePackage
397
+ NSDictionary *mutableUpdatePackage = [updatePackage mutableCopy ];
398
+ NSURL *binaryBundleURL = [CodePush binaryBundleURL ];
399
+ if (binaryBundleURL != nil ) {
400
+ [mutableUpdatePackage setValue: [CodePush modifiedDateStringOfFileAtURL: binaryBundleURL]
401
+ forKey: BinaryBundleDateKey];
402
+ }
403
+
404
+ [CodePushPackage downloadPackage: mutableUpdatePackage
371
405
// The download is progressing forward
372
406
progressCallback: ^(long long expectedContentLength, long long receivedContentLength) {
373
407
// Notify the script-side about the progress
@@ -381,7 +415,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
381
415
// The download completed
382
416
doneCallback: ^{
383
417
NSError *err;
384
- NSDictionary *newPackage = [CodePushPackage getPackage: updatePackage [PackageHashKey] error: &err];
418
+ NSDictionary *newPackage = [CodePushPackage getPackage: mutableUpdatePackage [PackageHashKey] error: &err];
385
419
386
420
if (err) {
387
421
return reject ([NSString stringWithFormat: @" %lu " , (long )err.code], err.localizedDescription , err);
@@ -392,7 +426,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
392
426
// The download failed
393
427
failCallback: ^(NSError *err) {
394
428
if ([CodePushPackage isCodePushError: err]) {
395
- [self saveFailedUpdate: updatePackage ];
429
+ [self saveFailedUpdate: mutableUpdatePackage ];
396
430
}
397
431
398
432
reject ([NSString stringWithFormat: @" %lu " , (long )err.code], err.localizedDescription , err);
0 commit comments