SFDX-Data-Move-Utility icon indicating copy to clipboard operation
SFDX-Data-Move-Utility copied to clipboard

In IOS getLatestVersion is not working

Open ChandreshRana opened this issue 5 years ago • 10 comments

I tried following for IOS:

===================== First Method =============================

VersionCheck.getLatestVersion({ provider: 'appStore' // for Ios }) .then(latestVersion => { console.log(latestVersion); });

===================== Second Method ===========================

VersionCheck.getLatestVersion().then(latestVersion => { console.log(latestVersion);
});

===================== Third Method ===========================

VersionCheck.getLatestVersion({ forceUpdate: true, provider: () => fetch(https://itunes.apple.com/${countryCode}lookup?bundleId=${packageName}) .then(r => r.json()) .then(({version}) => version) }).then(latestVersion =>{ console.log(latestVersion); });

  • Nothing get Response as Latest Verison of IOS App from appstore.

  • @kimxogus suggest any additional parameters or api changes for get latest version info

ChandreshRana avatar Dec 26 '19 13:12 ChandreshRana

Hi @ChandreshRana ,

I fixed this issue yesterday, but it looks like not this component issue, it's iTunes API's issue.

If you check the provider, you will find that the countryCode is all uppercase like "AU", the funny thing is if you put AU into query url like https://itunes.apple.com/AU/ookup?bundleId=${packageName}, it returns the previous version, for example, 1.2.33. But as I release the new version 1.2.34 in the morning that query still returns me 1.2.33 If the query is https://itunes.apple.com/au/ookup?bundleId=${packageName}, lowercase au. That can return 1.2.34

So the solution is we provide country as the parameter. like VersionCheck.getLatestVersion({country:'au'})

jessejiang0214 avatar Feb 14 '20 01:02 jessejiang0214

Thanks @jessejiang0214! But that should be added to the documentation! :D

thanhluantl2304 avatar Mar 14 '20 02:03 thanhluantl2304

yes same here, it worked defining the country manually at least for ios i need to check if for andorid

antonioreyna avatar Jun 03 '20 14:06 antonioreyna

@antonioreyna I just had a test on android for this function, it worked quite well (really late response, I know, I only came across this repo yesterday) 😄

gabcvit avatar Feb 18 '21 09:02 gabcvit

Hi, I have tried to do the three methods that you comment before and I had the same result: undefined.

Searching from google i found this example https://itunes.apple.com/lookup?bundleId=${bundleId}&country=${countryCode} which give me the version and others values like appName, appDescription, minimumOsVersion, currency and +20 more values... I think the way to take the version from the App Store (provider: 'appStore') has changed and this library doesn't update this.

My code: iosUpdateUrl = https://itunes.apple.com/lookup?bundleId=${bundleId}&country=${countryCode}

  getIosProvider(iosUpdateUrl) {
    return () =>
      fetch(iosUpdateUrl).then((response) =>
        response?.json()?.then((info) => (info?.resultCount > 0 ? info.results[0].version : null)),
      );
  },

  updateAppMessage(iosUpdateUrl) {
    return VersionCheck.getLatestVersion({
      provider: Platform.OS === 'ios' ? this.getIosProvider(iosUpdateUrl) : 'playStore',
    })
      .then((latestVersion) =>
        !!latestVersion ? VersionCheck.getCurrentVersion() !== latestVersion : false,
      )
      .catch((error) => {
        console.log('error taking app version', error);
        return false;
      });
  }

With this 2 methods i know if i have to display the update app flow or not

Used libraries versions

"react-native-version-check": "^3.4.2" (latest actually) "react-native": "0.63.3"

Source: https://medium.com/usemobile/get-app-version-from-app-store-and-more-e43c5055156a

aperomingo avatar Apr 15 '21 09:04 aperomingo

Update: previous method doesn't work on iOS Release (App Store) but yes on Debug and Release using simulator/physical iPhone... Android is OK

On testing now:

  getIosProvider(iosUpdateUrl) {
    return () =>
      fetch(iosUpdateUrl).then((response) =>
        response?.json()?.then((info) => (info?.resultCount > 0 ? info.results[0].version : null)),
      );
  }

  updateAppMessage(iosUpdateUrl) {
    return VersionCheck.needUpdate({
      provider: Platform.OS === 'ios' ? this.getIosProvider(iosUpdateUrl) : 'playStore',
    })
      .then(async (res) => {
        return typeof res?.isNeeded === 'boolean' ? res?.isNeeded : false;
      })
      .catch((error) => {
        console.log('error taking app version', error);
        return false;
      });
  }

aperomingo avatar Apr 19 '21 09:04 aperomingo

Is the issue not with this line?

return fetch(
  `https://itunes.apple.com/${countryCode}lookup?bundleId=${opt.packageName}&date=${dateNow}`, // this line
  opt.fetchOptions
)

Unpacking that:

https://itunes.apple.com/${countryCode}lookup?bundleId=${opt.packageName}&date=${dateNow}

Can become (removing date for simplicity):

https://itunes.apple.com/GBlookup?bundleId=com.XXX.YYY

Look at the countryCode though, there's no / between it and the lookup URL parameter.

I've verified that putting a / after countryCode and before lookup downloads the JSON as expected so the fix is to amend that fetch to be:

return fetch(
  `https://itunes.apple.com/${countryCode}/lookup?bundleId=${opt.packageName}&date=${dateNow}`,
  opt.fetchOptions
)

That is what I'd suspect? Can you confirm @kimxogus

TomasBarry avatar Mar 09 '22 11:03 TomasBarry

I did this and it works

   try {
        let versionConfig = {};
        const currentVersion = VersionCheck.getCurrentVersion();
        if (Platform.OS === "ios") {
          const packageName = await VersionCheck.getPackageName();
          const countryName = await VersionCheck.getCountry();
          versionConfig["provider"] = () =>
            fetch(`https://itunes.apple.com/${countryName.toLowerCase()}/lookup?bundleId=${packageName}`)
              .then(r => r.json())
              .then((res) => res?.results[0]?.version || currentVersion);
        }
        const latestVersion = await VersionCheck.getLatestVersion(versionConfig);
        const updateNeeded = await VersionCheck.needUpdate({
          latestVersion,
          currentVersion,
          provider: Platform.OS === "ios" ? "appStore" : "playStore",
        });
        if (updateNeeded && updateNeeded?.isNeeded) {
          Alert.alert("Version Update", "Update your app for the newest features", [
            { text: "Cancel", style: "cancel" },
            { text: "Update", style: "default", onPress: () => onUpdatePress() },
          ], { cancelable: false });
        }
      } catch (error) {
        console.log("error", error);
      }

harshsanghani avatar Apr 15 '22 07:04 harshsanghani

the example to get latestVersion for iOS ( fetch from API

      let latestVersion;

      if (Platform.OS === 'ios') {
        const packageName = await VersionCheck.getPackageName();
        const countryName = 'jp'; // getCountry() seems not correct..
        console.log(packageName);

        latestVersion = await fetch(
          `https://itunes.apple.com/${countryName.toLowerCase()}/lookup?bundleId=${packageName}`,
        )
          .then(r => r.json())
          .then(res => {
            console.log(res);
            return res?.results[0]?.version;
          });
      } else {
        latestVersion = await VersionCheck.getLatestVersion();
      }

      console.log(latestVersion);

sachiotomita avatar Aug 05 '22 15:08 sachiotomita

This will help https://www.youtube.com/watch?v=q3-Mrwd1Vm0

BraveEvidence avatar Feb 26 '23 13:02 BraveEvidence