'Opening market from ionic fails
I have this ionic app where I put code to open the browser and go to app store or google play, depending on OS. The call to App Store works. The string for app store is:
market = 'https://itunes.apple.com/us/app/my_app_name/id12345';
while for android is:
market = 'market://details?id=<package_name>';
The code to open the browser is:
cordova.InAppBrowser.open(market, '_blank', 'location=yes');
When I try to open in android, some kind of browser opens and displays the message:
"Web page not available. The webpage at market://details?id=my_app_id might be temporarly down ..."
Before this, the string for google play was the one you normally use in a browser, which is:
http://play.google.com/store/apps/details?id=<package_name>
In that case the message was about the broser not supporting the call to google play. It asked if I wanted to download google play app.
I guess the right way is to use the "market"-prefix? But still dont understand why its not showing the app.
Solution 1:[1]
Ok, so I looked into 'cordova-plugin-market' source code for ios. This is how the url to appstore is built:
NSString *url = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/%@", appId];
And this is how url looks if you want to navigate from PC browser:
https://itunes.apple.com/app/id333903271 (twitter for example)
So if you use this plugin for Ios, consider adding 'id' prefix, and instead of using bundle Id (package name) use Apple ID of your app. For android of course com.example.package will be enough
let appId;
if (this.platform.is("android")) {
appId = "com.example.package"
} else {
appId = "id1234567"
}
this.market.open(appId).then(response => {
console.debug(response);
}).catch(error => {
console.warn(error);
});
}
Solution 2:[2]
I added cordova plugin add https://github.com/xmartlabs/cordova-plugin-market And then it worked.
Solution 3:[3]
import { InAppBrowserOptions, InAppBrowser } from '@ionic-native/in-app-browser';
import { Market } from "@ionic-native/market";
import { Platform } from "ionic-angular";
constructor(private market:Market, private inappBrowswer:InAppBrowser,
private platform:Platform)
{
this.onUpdateNow();//Call the update function
}
onUpdateNow() {
this.platform.ready().then(() => {
if (this.platform.is("ios")) {
//replace '310633997' with your iOS App ID
this.openInAppStore('itms-apps://itunes.apple.com/app/310633997'); //call the openInAppStore
} else if (this.platform.is("android")) {
//replace 'com.whatsapp.saint' with your Android App ID
this.market.open("com.whatsapp.saint").then(response => {
console.log(response);
}).catch(error => {
console.log(error);
});
}
});
}
openInAppStore(link) {
let options: InAppBrowserOptions = {
location: 'yes',//Or 'no'
};
let target = "_blank";
this.inappBrowswer.create(link, target, options);
}
Solution 4:[4]
I tried the plugin with capacitor and its working like a charm using the package name and the app id (for android and ios accordingly):
public goToStore() {
const appId = 1234512345;
const packageName = 'com.test.app';
Capacitor.getPlatform() === 'android' ? this.market.open(packageName) : this.market.open(appId);
}
The plugins:
"@ionic-native/market": "5.36.0",
"cordova-plugin-market": "1.2.0",
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | oderfla |
| Solution 3 | Saint Deemene |
| Solution 4 | Alejandro Barone |
