fix(ios): don't crash app init when Storefront region code is unavailable (#4291)

`getStorefrontRegionCode` rejects when `Storefront.current` is nil
(unsigned simulator, signed-out device, StoreKit not yet ready,
parental controls, etc.). The call sits in NativeAppService startup
before `prepareBooksDir`, so an unhandled rejection here aborts the
rest of init and surfaces as a top-level unhandledRejection in the
webview console.

Wrap the call in try/catch and treat failure as 'unknown region',
leaving `storefrontRegionCode` null so downstream region-gated
features degrade gracefully. Also guard against an empty resolve
shape with optional chaining on `res?.regionCode`.
This commit is contained in:
loveheaven
2026-05-26 00:28:26 +08:00
committed by GitHub
parent c5384b2a6b
commit 7324b2b2bf
@@ -492,9 +492,18 @@ export class NativeAppService extends BaseAppService {
}
if (this.isIOSApp) {
this.isOnlineCatalogsAccessible = this.distChannel !== 'appstore';
const res = await getStorefrontRegionCode();
if (res.regionCode) {
this.storefrontRegionCode = res.regionCode;
try {
const res = await getStorefrontRegionCode();
if (res?.regionCode) {
this.storefrontRegionCode = res.regionCode;
}
} catch (err) {
// Storefront.current is nil on simulators without a signed-in
// App Store account, and may also fail on real devices with no
// StoreKit configuration. Treat as "unknown region" — we leave
// storefrontRegionCode as null and let downstream features that
// depend on region degrade gracefully.
console.warn('[nativeAppService] getStorefrontRegionCode failed:', err);
}
}
await this.prepareBooksDir();