diff --git a/README.md b/README.md index f9ab666..92964a9 100644 --- a/README.md +++ b/README.md @@ -1105,6 +1105,8 @@ Because of this behavior, you can safely deploy updates to both the app store(s) - __(NSURL \*)bundleURLForResource:(NSString \*)resourceName withExtension:(NSString \*)resourceExtension__: Equivalent to the `bundleURLForResource:` method, but also allows customizing the extension used by the JS bundle that is looked for within the app binary. This is useful if you aren't naming this file `*.jsbundle` (which is the default convention). +- __(void)overrideAppVersion:(NSString \*)appVersionOverride__ - Sets the version of the application's binary interface, which would otherwise default to the App Store version specified as the `CFBundleShortVersionString` in the `Info.plist`. This should be called a single time, before the bundle URL is loaded. + - __(void)setDeploymentKey:(NSString \*)deploymentKey__ - Sets the deployment key that the app should use when querying for updates. This is a dynamic alternative to setting the deployment key in your `Info.plist` and/or specifying a deployment key in JS when calling `checkForUpdate` or `sync`. ### Java API Reference (Android) @@ -1131,6 +1133,8 @@ Constructs the CodePush client runtime and represents the `ReactPackage` instanc - __getBundleUrl(String bundleName)__ - Returns the path to the most recent version of your app's JS bundle file, using the specified resource name (e.g. `index.android.bundle`). This method has the same resolution behavior as the Objective-C equivalent described above. +- __overrideAppVersion(String appVersionOverride)__ - Sets the version of the application's binary interface, which would otherwise default to the Play Store version specified as the `versionName` in the `build.gradle`. This should be called a single time, before the CodePush instance is constructed. + ## Example Apps / Starters The React Native community has graciously created some awesome open source apps that can serve as examples for developers that are getting started. The following is a list of OSS React Native apps that are also using CodePush, and can therefore be used to see how others are using the service: diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 2c18009..774a117 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -29,6 +29,7 @@ public class CodePush implements ReactPackage { private static boolean sIsRunningBinaryVersion = false; private static boolean sNeedToReportRollback = false; private static boolean sTestConfigurationFlag = false; + private static String sAppVersion = null; private boolean mDidUpdate = false; @@ -40,7 +41,6 @@ public class CodePush implements ReactPackage { private SettingsManager mSettingsManager; // Config properties. - private String mAppVersion; private String mDeploymentKey; private String mServerUrl = "https://codepush.azurewebsites.net/"; @@ -63,11 +63,13 @@ public class CodePush implements ReactPackage { mIsDebugMode = isDebugMode; mSettingsManager = new SettingsManager(mContext); - try { - PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0); - mAppVersion = pInfo.versionName; - } catch (PackageManager.NameNotFoundException e) { - throw new CodePushUnknownException("Unable to get package info for " + mContext.getPackageName(), e); + if (sAppVersion == null) { + try { + PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0); + sAppVersion = pInfo.versionName; + } catch (PackageManager.NameNotFoundException e) { + throw new CodePushUnknownException("Unable to get package info for " + mContext.getPackageName(), e); + } } mCurrentInstance = this; @@ -96,7 +98,7 @@ public class CodePush implements ReactPackage { } public String getAppVersion() { - return mAppVersion; + return sAppVersion; } public String getAssetsBundleFileName() { @@ -177,14 +179,14 @@ public class CodePush implements ReactPackage { String packageAppVersion = CodePushUtils.tryGetString(packageMetadata, "appVersion"); if (binaryModifiedDateDuringPackageInstall != null && binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime && - (isUsingTestConfiguration() || this.mAppVersion.equals(packageAppVersion))) { + (isUsingTestConfiguration() || sAppVersion.equals(packageAppVersion))) { CodePushUtils.logBundleUrl(packageFilePath); sIsRunningBinaryVersion = false; return packageFilePath; } else { // The binary version is newer. this.mDidUpdate = false; - if (!this.mIsDebugMode || !this.mAppVersion.equals(packageAppVersion)) { + if (!this.mIsDebugMode || !sAppVersion.equals(packageAppVersion)) { this.clearUpdates(); } @@ -249,6 +251,10 @@ public class CodePush implements ReactPackage { return sNeedToReportRollback; } + public static void overrideAppVersion(String appVersionOverride) { + sAppVersion = appVersionOverride; + } + private void rollbackPackage() { WritableMap failedPackage = mUpdateManager.getCurrentPackage(); mSettingsManager.saveFailedUpdate(failedPackage); @@ -280,7 +286,7 @@ public class CodePush implements ReactPackage { CodePushNativeModule codePushModule = new CodePushNativeModule(reactApplicationContext, this, mUpdateManager, mTelemetryManager, mSettingsManager); CodePushDialog dialogModule = new CodePushDialog(reactApplicationContext); - List nativeModules = new ArrayList<>(); + List nativeModules = new ArrayList<>(); nativeModules.add(codePushModule); nativeModules.add(dialogModule); return nativeModules; diff --git a/ios/CodePush/CodePush.h b/ios/CodePush/CodePush.h index 04a617c..3e42060 100644 --- a/ios/CodePush/CodePush.h +++ b/ios/CodePush/CodePush.h @@ -30,7 +30,14 @@ + (NSString *)bundleAssetsPath; /* - * This methods allows dynamically setting the app's + * This method allows the version of the app's binary interface + * to be specified, which would otherwise default to the + * App Store version of the app. + */ ++ (void)overrideAppVersion:(NSString *)deploymentKey; + +/* + * This method allows dynamically setting the app's * deployment key, in addition to setting it via * the Info.plist file's CodePushDeploymentKey setting. */ @@ -45,7 +52,7 @@ @interface CodePushConfig : NSObject -@property (readonly) NSString *appVersion; +@property (copy) NSString *appVersion; @property (readonly) NSString *buildVersion; @property (readonly) NSDictionary *configuration; @property (copy) NSString *deploymentKey; diff --git a/ios/CodePush/CodePush.m b/ios/CodePush/CodePush.m index c8be248..e6616ab 100644 --- a/ios/CodePush/CodePush.m +++ b/ios/CodePush/CodePush.m @@ -73,7 +73,7 @@ static NSString *bundleResourceSubdirectory = nil; if (bundleResourceSubdirectory) { resourcePath = [resourcePath stringByAppendingPathComponent:bundleResourceSubdirectory]; } - + return [resourcePath stringByAppendingPathComponent:[CodePushUpdateUtils assetsFolderName]]; } @@ -159,6 +159,11 @@ static NSString *bundleResourceSubdirectory = nil; return applicationSupportDirectory; } ++ (void)overrideAppVersion:(NSString *)appVersion +{ + [CodePushConfig current].appVersion = appVersion; +} + + (void)setDeploymentKey:(NSString *)deploymentKey { [CodePushConfig current].deploymentKey = deploymentKey; diff --git a/ios/CodePush/CodePushConfig.m b/ios/CodePush/CodePushConfig.m index b84b757..7d12964 100644 --- a/ios/CodePush/CodePushConfig.m +++ b/ios/CodePush/CodePushConfig.m @@ -86,6 +86,11 @@ static NSString * const ServerURLConfigKey = @"serverUrl"; return [_configDictionary objectForKey:ClientUniqueIDConfigKey]; } +- (void)setAppVersion:(NSString *)appVersion +{ + [_configDictionary setValue:appVersion forKey:AppVersionConfigKey]; +} + - (void)setDeploymentKey:(NSString *)deploymentKey { [_configDictionary setValue:deploymentKey forKey:DeploymentKeyConfigKey];