@@ -76,32 +76,32 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
76
76
{
77
77
bundleResourceName = resourceName;
78
78
bundleResourceExtension = resourceExtension;
79
-
79
+
80
80
[self ensureBinaryBundleExists ];
81
-
81
+
82
82
NSString *logMessageFormat = @" Loading JS bundle from %@ " ;
83
-
83
+
84
84
NSError *error;
85
85
NSString *packageFile = [CodePushPackage getCurrentPackageBundlePath: &error];
86
86
NSURL *binaryBundleURL = [self binaryBundleURL ];
87
-
87
+
88
88
if (error || !packageFile) {
89
89
NSLog (logMessageFormat, binaryBundleURL);
90
90
isRunningBinaryVersion = YES ;
91
91
return binaryBundleURL;
92
92
}
93
-
93
+
94
94
NSString *binaryAppVersion = [[CodePushConfig current ] appVersion ];
95
95
NSDictionary *currentPackageMetadata = [CodePushPackage getCurrentPackage: &error];
96
96
if (error || !currentPackageMetadata) {
97
97
NSLog (logMessageFormat, binaryBundleURL);
98
98
isRunningBinaryVersion = YES ;
99
99
return binaryBundleURL;
100
100
}
101
-
101
+
102
102
NSString *packageDate = [currentPackageMetadata objectForKey: BinaryBundleDateKey];
103
103
NSString *packageAppVersion = [currentPackageMetadata objectForKey: AppVersionKey];
104
-
104
+
105
105
if ([[CodePushUpdateUtils modifiedDateStringOfFileAtURL: binaryBundleURL] isEqualToString: packageDate] && ([CodePush isUsingTestConfiguration ] ||[binaryAppVersion isEqualToString: packageAppVersion])) {
106
106
// Return package file because it is newer than the app store binary's JS bundle
107
107
NSURL *packageUrl = [[NSURL alloc ] initFileURLWithPath: packageFile];
@@ -113,11 +113,11 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
113
113
#ifndef DEBUG
114
114
isRelease = YES ;
115
115
#endif
116
-
116
+
117
117
if (isRelease || ![binaryAppVersion isEqualToString: packageAppVersion]) {
118
118
[CodePush clearUpdates ];
119
119
}
120
-
120
+
121
121
NSLog (logMessageFormat, binaryBundleURL);
122
122
isRunningBinaryVersion = YES ;
123
123
return binaryBundleURL;
@@ -186,7 +186,7 @@ - (NSDictionary *)constantsToExport
186
186
@" codePushInstallModeOnNextRestart" :@(CodePushInstallModeOnNextRestart),
187
187
@" codePushInstallModeImmediate" : @(CodePushInstallModeImmediate),
188
188
@" codePushInstallModeOnNextResume" : @(CodePushInstallModeOnNextResume),
189
-
189
+
190
190
@" codePushUpdateStateRunning" : @(CodePushUpdateStateRunning),
191
191
@" codePushUpdateStatePending" : @(CodePushUpdateStatePending),
192
192
@" codePushUpdateStateLatest" : @(CodePushUpdateStateLatest)
@@ -208,44 +208,39 @@ + (void)ensureBinaryBundleExists
208
208
{
209
209
if (![self binaryBundleURL ]) {
210
210
NSString *errorMessage;
211
-
211
+
212
212
#if TARGET_IPHONE_SIMULATOR
213
213
errorMessage = @" React Native doesn't generate your app's JS bundle by default when deploying to the simulator. "
214
214
" If you'd like to test CodePush using the simulator, you can do one of three things depending on your React "
215
215
" Native version and/or preferred workflow:\n\n "
216
-
216
+
217
217
" 1. Update your AppDelegate.m file to load the JS bundle from the packager instead of from CodePush. "
218
218
" You can still test your CodePush update experience using this workflow (debug builds only).\n\n "
219
-
219
+
220
220
" 2. Force the JS bundle to be generated in simulator builds by removing the if block that echoes "
221
221
" \" Skipping bundling for Simulator platform\" in the \" node_modules/react-native/packager/react-native-xcode.sh\" file.\n\n "
222
-
222
+
223
223
" 3. Deploy a release build to the simulator, which unlike debug builds, will generate the JS bundle (React Native >=0.22.0 only)." ;
224
224
#else
225
225
errorMessage = [NSString stringWithFormat: @" The specified JS bundle file wasn't found within the app's binary. Is \" %@ \" the correct file name?" , [bundleResourceName stringByAppendingPathExtension: bundleResourceExtension]];
226
226
#endif
227
-
227
+
228
228
RCTFatal ([CodePushErrorUtils errorWithMessage: errorMessage]);
229
229
}
230
230
}
231
231
232
232
- (instancetype )init
233
233
{
234
234
self = [super init ];
235
-
235
+
236
236
if (self) {
237
237
[self initializeUpdateAfterRestart ];
238
238
}
239
-
239
+
240
240
return self;
241
241
}
242
242
243
- /*
244
- * This method is used when the app is started to either
245
- * initialize a pending update or rollback a faulty update
246
- * to the previous version.
247
- */
248
- - (void )initializeUpdateAfterRestart
243
+ - (void )clearDebugUpdates
249
244
{
250
245
dispatch_async (dispatch_get_main_queue (), ^{
251
246
if ([_bridge.bundleURL.scheme hasPrefix: @" http" ]) {
@@ -260,6 +255,16 @@ - (void)initializeUpdateAfterRestart
260
255
}
261
256
}
262
257
});
258
+ }
259
+
260
+ /*
261
+ * This method is used when the app is started to either
262
+ * initialize a pending update or rollback a faulty update
263
+ * to the previous version.
264
+ */
265
+ - (void )initializeUpdateAfterRestart
266
+ {
267
+ [self clearDebugUpdates ];
263
268
264
269
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
265
270
NSDictionary *pendingUpdate = [preferences objectForKey: PendingUpdateKey];
@@ -305,7 +310,7 @@ - (BOOL)isFailedHash:(NSString*)packageHash
305
310
}
306
311
}
307
312
}
308
-
313
+
309
314
return NO ;
310
315
}
311
316
}
@@ -319,13 +324,13 @@ - (BOOL)isPendingUpdate:(NSString*)packageHash
319
324
{
320
325
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
321
326
NSDictionary *pendingUpdate = [preferences objectForKey: PendingUpdateKey];
322
-
327
+
323
328
// If there is a pending update whose "state" isn't loading, then we consider it "pending".
324
329
// Additionally, if a specific hash was provided, we ensure it matches that of the pending update.
325
330
BOOL updateIsPending = pendingUpdate &&
326
331
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue ] == NO &&
327
332
(!packageHash || [pendingUpdate[PendingUpdateHashKey] isEqualToString: packageHash]);
328
-
333
+
329
334
return updateIsPending;
330
335
}
331
336
@@ -346,7 +351,7 @@ - (void)loadBundle
346
351
if ([CodePush isUsingTestConfiguration ] || ![_bridge.bundleURL.scheme hasPrefix: @" http" ]) {
347
352
[_bridge setValue: [CodePush bundleURL ] forKey: @" bundleURL" ];
348
353
}
349
-
354
+
350
355
[_bridge reload ];
351
356
});
352
357
}
@@ -362,10 +367,10 @@ - (void)rollbackPackage
362
367
{
363
368
NSError *error;
364
369
NSDictionary *failedPackage = [CodePushPackage getCurrentPackage: &error];
365
-
370
+
366
371
// Write the current package's metadata to the "failed list"
367
372
[self saveFailedUpdate: failedPackage];
368
-
373
+
369
374
// Rollback to the previous version and de-register the new update
370
375
[CodePushPackage rollbackPackage ];
371
376
[CodePush removePendingUpdate ];
@@ -388,7 +393,7 @@ - (void)saveFailedUpdate:(NSDictionary *)failedPackage
388
393
// objects, regardless if you stored something mutable.
389
394
failedUpdates = [failedUpdates mutableCopy ];
390
395
}
391
-
396
+
392
397
[failedUpdates addObject: failedPackage];
393
398
[preferences setObject: failedUpdates forKey: FailedUpdatesKey];
394
399
[preferences synchronize ];
@@ -430,7 +435,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
430
435
NSDictionary *pendingUpdate = [[NSDictionary alloc ] initWithObjectsAndKeys:
431
436
packageHash,PendingUpdateHashKey,
432
437
[NSNumber numberWithBool: isLoading],PendingUpdateIsLoadingKey, nil ];
433
-
438
+
434
439
[preferences setObject: pendingUpdate forKey: PendingUpdateKey];
435
440
[preferences synchronize ];
436
441
}
@@ -471,7 +476,7 @@ - (void)applicationWillResignActive
471
476
[mutableUpdatePackage setValue: [CodePushUpdateUtils modifiedDateStringOfFileAtURL: binaryBundleURL]
472
477
forKey: BinaryBundleDateKey];
473
478
}
474
-
479
+
475
480
[CodePushPackage
476
481
downloadPackage: mutableUpdatePackage
477
482
expectedBundleFileName: [bundleResourceName stringByAppendingPathExtension: bundleResourceExtension]
@@ -492,11 +497,11 @@ - (void)applicationWillResignActive
492
497
dispatch_async (_methodQueue, ^{
493
498
NSError *err;
494
499
NSDictionary *newPackage = [CodePushPackage getPackage: mutableUpdatePackage[PackageHashKey] error: &err];
495
-
500
+
496
501
if (err) {
497
502
return reject ([NSString stringWithFormat: @" %lu " , (long )err.code], err.localizedDescription , err);
498
503
}
499
-
504
+
500
505
resolve (newPackage);
501
506
});
502
507
}
@@ -506,7 +511,7 @@ - (void)applicationWillResignActive
506
511
if ([CodePushErrorUtils isCodePushError: err]) {
507
512
[self saveFailedUpdate: mutableUpdatePackage];
508
513
}
509
-
514
+
510
515
reject ([NSString stringWithFormat: @" %lu " , (long )err.code], err.localizedDescription , err);
511
516
});
512
517
}];
@@ -531,21 +536,21 @@ - (void)applicationWillResignActive
531
536
resolve (configuration);
532
537
return ;
533
538
}
534
-
539
+
535
540
if (binaryHash == nil ) {
536
541
// The hash was not generated either due to a previous unknown error or the fact that
537
542
// the React Native assets were not bundled in the binary (e.g. during dev/simulator)
538
543
// builds.
539
544
resolve (configuration);
540
545
return ;
541
546
}
542
-
547
+
543
548
NSMutableDictionary *mutableConfiguration = [configuration mutableCopy ];
544
549
[mutableConfiguration setObject: binaryHash forKey: PackageHashKey];
545
550
resolve (mutableConfiguration);
546
551
return ;
547
552
}
548
-
553
+
549
554
resolve (configuration);
550
555
}
551
556
@@ -567,10 +572,10 @@ - (void)applicationWillResignActive
567
572
// wanted to retrieve the pending or running update.
568
573
return resolve (nil );
569
574
}
570
-
575
+
571
576
// We have a CodePush update, so let's see if it's currently in a pending state.
572
577
BOOL currentUpdateIsPending = [self isPendingUpdate: [package objectForKey: PackageHashKey]];
573
-
578
+
574
579
if (updateState == CodePushUpdateStatePending && !currentUpdateIsPending) {
575
580
// The caller wanted a pending update
576
581
// but there isn't currently one.
@@ -590,7 +595,7 @@ - (void)applicationWillResignActive
590
595
// disk that is not actually running.
591
596
[package setObject: @(YES ) forKey: @" _isDebugOnly" ];
592
597
}
593
-
598
+
594
599
// Enable differentiating pending vs. non-pending updates
595
600
[package setObject: @(currentUpdateIsPending) forKey: PackageIsPendingKey];
596
601
resolve (package);
@@ -610,16 +615,16 @@ - (void)applicationWillResignActive
610
615
[CodePushPackage installPackage: updatePackage
611
616
removePendingUpdate: [self isPendingUpdate: nil ]
612
617
error: &error];
613
-
618
+
614
619
if (error) {
615
620
reject ([NSString stringWithFormat: @" %lu " , (long )error.code], error.localizedDescription , error);
616
621
} else {
617
622
[self savePendingUpdate: updatePackage[PackageHashKey]
618
623
isLoading: NO ];
619
-
624
+
620
625
if (installMode == CodePushInstallModeOnNextResume) {
621
626
_minimumBackgroundDuration = minimumBackgroundDuration;
622
-
627
+
623
628
if (!_hasResumeListener) {
624
629
// Ensure we do not add the listener twice.
625
630
// Register for app resume notifications so that we
@@ -628,16 +633,16 @@ - (void)applicationWillResignActive
628
633
selector: @selector (applicationWillEnterForeground )
629
634
name: UIApplicationWillEnterForegroundNotification
630
635
object: [UIApplication sharedApplication ]];
631
-
636
+
632
637
[[NSNotificationCenter defaultCenter ] addObserver: self
633
638
selector: @selector (applicationWillResignActive )
634
639
name: UIApplicationWillResignActiveNotification
635
640
object: [UIApplication sharedApplication ]];
636
-
641
+
637
642
_hasResumeListener = YES ;
638
643
}
639
644
}
640
-
645
+
641
646
// Signal to JS that the update has been applied.
642
647
resolve (nil );
643
648
}
@@ -668,7 +673,7 @@ - (void)applicationWillResignActive
668
673
&& nil != packageHash
669
674
&& [packageHash length ] > 0
670
675
&& [packageHash isEqualToString: [CodePushPackage getCurrentPackageHash: &error]];
671
-
676
+
672
677
resolve (@(isFirstRun));
673
678
}
674
679
@@ -715,7 +720,7 @@ - (void)applicationWillResignActive
715
720
*/
716
721
RCT_EXPORT_METHOD (getNewStatusReport:(RCTPromiseResolveBlock)resolve
717
722
rejecter:(RCTPromiseRejectBlock)reject)
718
- {
723
+ {
719
724
if (needToReportRollback) {
720
725
needToReportRollback = NO ;
721
726
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
@@ -739,7 +744,7 @@ - (void)applicationWillResignActive
739
744
resolve ([CodePushTelemetryManager getBinaryUpdateReport: appVersion]);
740
745
return ;
741
746
}
742
-
747
+
743
748
resolve (nil );
744
749
}
745
750
0 commit comments