Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In IOS getLatestVersion is not working #104

Open
ChandreshRana opened this issue Dec 26, 2019 · 10 comments
Open

In IOS getLatestVersion is not working #104

ChandreshRana opened this issue Dec 26, 2019 · 10 comments

Comments

@ChandreshRana
Copy link

ChandreshRana commented Dec 26, 2019

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

@jessejiang0214
Copy link

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'})

@thanhluantl2304
Copy link

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

@antonioreyna
Copy link

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

@gabcvit
Copy link

gabcvit commented Feb 18, 2021

@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) 😄

@aperomingo
Copy link

aperomingo commented Apr 15, 2021

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
Copy link

aperomingo commented Apr 19, 2021

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;
      });
  }

@kimxogus kimxogus pinned this issue May 22, 2021
@TomasBarry
Copy link

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

@harshsanghani
Copy link

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);
      }

@sachiotomita
Copy link

sachiotomita commented Aug 5, 2022

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);

@BraveEvidence
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants