Skip to content

Commit 742d6d7

Browse files
author
Muhammed Simsek
committed
Added the option to open the app detail page in the AppGallery, if present. If not, it will open the Play Store link.
1 parent ca27e74 commit 742d6d7

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</h3>
88

99
<p align="center">
10-
Easily <a href="https://en.wikipedia.org/wiki/Mobile_deep_linking">deep link</a> to other apps in React Native. If the app isn't installed on the user's phone, open the App Store or Play Store link instead.
10+
Easily <a href="https://en.wikipedia.org/wiki/Mobile_deep_linking">deep link</a> to other apps in React Native. If the app isn't installed on the user's phone, open the App Store, Play Store or AppGallery link instead.
1111
</p>
1212

1313
---
@@ -35,14 +35,14 @@ Easily <a href="https://en.wikipedia.org/wiki/Mobile_deep_linking">deep link</a>
3535
```javascript
3636
import AppLink from 'react-native-app-link';
3737

38-
AppLink.maybeOpenURL(url, { appName, appStoreId, appStoreLocale, playStoreId }).then(() => {
38+
AppLink.maybeOpenURL(url, { appName, appStoreId, appStoreLocale, playStoreId, appGalleryId }).then(() => {
3939
// do stuff
4040
})
4141
.catch((err) => {
4242
// handle error
4343
});
4444

45-
AppLink.openInStore({ appName, appStoreId, appStoreLocale, playStoreId }).then(() => {
45+
AppLink.openInStore({ appName, appStoreId, appStoreLocale, playStoreId, appGalleryId }).then(() => {
4646
// do stuff
4747
})
4848
.catch((err) => {
@@ -52,7 +52,7 @@ AppLink.openInStore({ appName, appStoreId, appStoreLocale, playStoreId }).then((
5252

5353
## API:
5454

55-
### `maybeOpenURL(url, config) -> Promise` Opens link if app is present. If not, it opens an app store to prompt the user to download it.
55+
### `maybeOpenURL(url, config) -> Promise` Opens link if app is present. If not, it opens an app store to prompt the user to download it.
5656

5757
`url`: (String) a url in the specified app's [deep linking](https://en.wikipedia.org/wiki/Mobile_deep_linking) format that points to the content you want to open.
5858

@@ -62,13 +62,15 @@ AppLink.openInStore({ appName, appStoreId, appStoreLocale, playStoreId }).then((
6262

6363
`config.appStoreId`: (String) the app's ID on the App Store (iOS). Example: `{ appStoreId: '529379082' }`
6464

65-
`config.appStoreLocale`: (String) the App Store's locale (iOS). Defaults to the USA App Store. Example: `{ appStoreId: 'us' }`
65+
`config.appStoreLocale`: (String) the App Store's locale (iOS). Defaults to the USA App Store. Example: `{ appStoreLocale: 'us' }`
6666

6767
`config.playStoreId`: (String) the app's package identifier on the Play Store (Android). Example: `{ playStoreId: 'me.lyft.android' }`
6868

69+
`config.appGalleryId`: (String) the app's ID on the AppGallery (Android). Example: `{ appGalleryId: 'C100170981' }`
70+
6971
---
7072

71-
### `openInStore(config) -> Promise` Opens an app store to the listing requested.
73+
### `openInStore(config) -> Promise` Opens an app store to the listing requested.
7274

7375
`config`: (Object) a config for generate store urls.
7476

@@ -80,4 +82,6 @@ AppLink.openInStore({ appName, appStoreId, appStoreLocale, playStoreId }).then((
8082

8183
`config.playStoreId`: (String) the app's package identifier on the Play Store (Android). Example: `{ playStoreId: 'me.lyft.android' }`
8284

85+
`config.appGalleryId`: (String) the app's ID on the AppGallery (Android). Example: `{ appGalleryId: 'C100170981' }`
86+
8387
> If there are any issues file an issue above and don't hesitate to spin up a PR and contribute!

index.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Linking, Platform } from 'react-native';
22

33
export const maybeOpenURL = async (
44
url,
5-
{ appName, appStoreId, appStoreLocale, playStoreId }
5+
{ appName, appStoreId, appStoreLocale, playStoreId, appGalleryId = null }
66
) => {
77
Linking.openURL(url).catch(err => {
88
if (err.code === 'EUNSPECIFIED') {
@@ -14,23 +14,51 @@ export const maybeOpenURL = async (
1414

1515
Linking.openURL(`https://itunes.apple.com/${locale}/app/${appName}/id${appStoreId}`);
1616
} else {
17-
Linking.openURL(
18-
`https://play.google.com/store/apps/details?id=${playStoreId}`
19-
);
17+
if(appGalleryId != null) {
18+
Linking.openURL(
19+
`hiapplink://com.huawei.appmarket?appId=${appGalleryId}`
20+
).catch(err => {
21+
console.log(err)
22+
if(err.code == 'EUNSPECIFIED') {
23+
Linking.openURL(
24+
`https://play.google.com/store/apps/details?id=${playStoreId}`
25+
);
26+
}
27+
})
28+
}
29+
else {
30+
Linking.openURL(
31+
`https://play.google.com/store/apps/details?id=${playStoreId}`
32+
);
33+
}
2034
}
2135
} else {
2236
throw new Error(`Could not open ${appName}. ${err.toString()}`);
2337
}
2438
});
2539
};
2640

27-
export const openInStore = async ({ appName, appStoreId, appStoreLocale = 'us', playStoreId }) => {
41+
export const openInStore = async ({ appName, appStoreId, appStoreLocale = 'us', playStoreId, appGalleryId = null }) => {
2842
if (Platform.OS === 'ios') {
2943
Linking.openURL(`https://itunes.apple.com/${appStoreLocale}/app/${appName}/id${appStoreId}`);
3044
} else {
31-
Linking.openURL(
32-
`https://play.google.com/store/apps/details?id=${playStoreId}`
33-
);
45+
if(appGalleryId != null) {
46+
Linking.openURL(
47+
`hiapplink://com.huawei.appmarket?appId=${appGalleryId}`
48+
).catch(err => {
49+
console.log(err)
50+
if(err.code == 'EUNSPECIFIED') {
51+
Linking.openURL(
52+
`https://play.google.com/store/apps/details?id=${playStoreId}`
53+
);
54+
}
55+
})
56+
}
57+
else {
58+
Linking.openURL(
59+
`https://play.google.com/store/apps/details?id=${playStoreId}`
60+
);
61+
}
3462
}
3563
};
3664

0 commit comments

Comments
 (0)