Skip to content

Commit 817b650

Browse files
authored
[in_app_purchase_storekit] Add show manage subscriptions path (flutter#12454)
Adds a new `showManageSubscriptions` method which wraps around storekit's [showManageSubscriptions(in:)](https://developer.apple.com/documentation/storekit/appstore/showmanagesubscriptions(in:)) Fixes flutter/flutter#159664 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent e01c343 commit 817b650

12 files changed

Lines changed: 147 additions & 1 deletion

File tree

‎packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.4.14
2+
3+
* Adds `showManageSubscriptions` to `InAppPurchaseStoreKitPlatformAddition`, which presents the
4+
App Store sheet for managing subscriptions. Requires StoreKit 2 and iOS 15+.
5+
16
## 0.4.13+3
27

38
* Removes unused handling of the deprecated `SKDownload` hosted content

‎packages/in_app_purchase/in_app_purchase_storekit/darwin/in_app_purchase_storekit/Sources/in_app_purchase_storekit/StoreKit2/InAppPurchasePlugin+StoreKit2.swift‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,41 @@ extension InAppPurchasePlugin: InAppPurchase2API {
409409
#endif
410410
}
411411

412+
/// Wrapper method around StoreKit2's showManageSubscriptions(in:) method
413+
/// https://developer.apple.com/documentation/storekit/appstore/showmanagesubscriptions(in:)
414+
/// Presents the App Store sheet that lets the user manage their subscriptions.
415+
func showManageSubscriptions(completion: @escaping (Result<Void, Error>) -> Void) {
416+
#if os(iOS)
417+
Task { @MainActor in
418+
guard let windowScene = self.registrar?.viewController?.view.window?.windowScene else {
419+
let error = PigeonError(
420+
code: "storekit2_missing_key_window_scene",
421+
message: "Failed to fetch key window scene",
422+
details: "registrar.viewController.view.window.windowScene returned nil."
423+
)
424+
completion(.failure(error))
425+
return
426+
}
427+
do {
428+
try await AppStore.showManageSubscriptions(in: windowScene)
429+
completion(.success(()))
430+
} catch {
431+
completion(.failure(error))
432+
}
433+
}
434+
#elseif os(macOS)
435+
// StoreKit does not provide showManageSubscriptions on macOS. Subscriptions are
436+
// managed through the App Store app instead.
437+
completion(
438+
.failure(
439+
PigeonError(
440+
code: "storekit2_unsupported_platform",
441+
message: "Managing subscriptions is not supported on macOS",
442+
details: nil
443+
)))
444+
#endif
445+
}
446+
412447
/// Wrapper method around StoreKit2's sync() method
413448
/// https://developer.apple.com/documentation/storekit/appstore/sync()
414449
/// When called, a system prompt will ask users to enter their authentication details

‎packages/in_app_purchase/in_app_purchase_storekit/darwin/in_app_purchase_storekit/Sources/in_app_purchase_storekit/StoreKit2/StoreKit2Messages.g.swift‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ protocol InAppPurchase2API {
10041004
func countryCode(completion: @escaping (Result<String, Error>) -> Void)
10051005
func sync(completion: @escaping (Result<Void, Error>) -> Void)
10061006
func presentOfferCodeRedeemSheet(completion: @escaping (Result<Void, Error>) -> Void)
1007+
func showManageSubscriptions(completion: @escaping (Result<Void, Error>) -> Void)
10071008
}
10081009

10091010
/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
@@ -1271,6 +1272,24 @@ class InAppPurchase2APISetup {
12711272
} else {
12721273
presentOfferCodeRedeemSheetChannel.setMessageHandler(nil)
12731274
}
1275+
let showManageSubscriptionsChannel = FlutterBasicMessageChannel(
1276+
name:
1277+
"dev.flutter.pigeon.in_app_purchase_storekit.InAppPurchase2API.showManageSubscriptions\(channelSuffix)",
1278+
binaryMessenger: binaryMessenger, codec: codec)
1279+
if let api = api {
1280+
showManageSubscriptionsChannel.setMessageHandler { _, reply in
1281+
api.showManageSubscriptions { result in
1282+
switch result {
1283+
case .success:
1284+
reply(wrapResult(nil))
1285+
case .failure(let error):
1286+
reply(wrapError(error))
1287+
}
1288+
}
1289+
}
1290+
} else {
1291+
showManageSubscriptionsChannel.setMessageHandler(nil)
1292+
}
12741293
}
12751294
}
12761295

‎packages/in_app_purchase/in_app_purchase_storekit/example/lib/main.dart‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class _MyAppState extends State<_MyApp> {
147147
_buildProductList(),
148148
_buildConsumableBox(),
149149
_buildCodeRedemptionButton(),
150+
_buildShowManageSubscriptionsButton(),
150151
_buildRestoreButton(),
151152
],
152153
),
@@ -419,6 +420,29 @@ class _MyAppState extends State<_MyApp> {
419420
);
420421
}
421422

423+
Widget _buildShowManageSubscriptionsButton() {
424+
if (_loading) {
425+
return Container();
426+
}
427+
428+
return Padding(
429+
padding: const EdgeInsets.all(4.0),
430+
child: Row(
431+
mainAxisAlignment: MainAxisAlignment.end,
432+
children: <Widget>[
433+
TextButton(
434+
style: TextButton.styleFrom(
435+
backgroundColor: Theme.of(context).colorScheme.primary,
436+
foregroundColor: Colors.white,
437+
),
438+
onPressed: () => _iapStoreKitPlatformAddition.showManageSubscriptions(),
439+
child: const Text('Manage subscriptions'),
440+
),
441+
],
442+
),
443+
);
444+
}
445+
422446
Widget _buildRestoreButton() {
423447
if (_loading) {
424448
return Container();

‎packages/in_app_purchase/in_app_purchase_storekit/example/shared/RunnerTests/InAppPurchaseStoreKit2PluginTests.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,19 @@ final class InAppPurchase2PluginTests: XCTestCase {
787787
waitForExpectations(timeout: 1.0)
788788
}
789789

790+
func testShowManageSubscriptionsFailsGracefullyWhenNoWindow() {
791+
let expectation = self.expectation(
792+
description: "Should fail gracefully when without key window")
793+
794+
plugin.registrar = nil
795+
796+
plugin.showManageSubscriptions { result in
797+
if case .failure = result {
798+
expectation.fulfill()
799+
}
800+
}
801+
802+
waitForExpectations(timeout: 1.0)
803+
}
804+
790805
}

‎packages/in_app_purchase/in_app_purchase_storekit/lib/src/in_app_purchase_storekit_platform_addition.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class InAppPurchaseStoreKitPlatformAddition extends InAppPurchasePlatformAdditio
2929
return SKPaymentQueueWrapper().presentCodeRedemptionSheet();
3030
}
3131

32+
/// Presents the App Store sheet that lets the user manage their
33+
/// subscriptions.
34+
///
35+
/// Available on devices running iOS 15 and iPadOS 15 and later.
36+
/// StoreKit 2 only; StoreKit 1 has no equivalent API. Not supported on macOS,
37+
/// where subscriptions are managed through the App Store app instead.
38+
Future<void> showManageSubscriptions() {
39+
return AppStore().showManageSubscriptions();
40+
}
41+
3242
/// Retry loading purchase data after an initial failure.
3343
///
3444
/// If no results, a `null` value is returned.

‎packages/in_app_purchase/in_app_purchase_storekit/lib/src/sk2_pigeon.g.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,20 @@ class InAppPurchase2API {
11721172

11731173
_extractReplyValueOrThrow(pigeonVar_replyList, pigeonVar_channelName, isNullValid: true);
11741174
}
1175+
1176+
Future<void> showManageSubscriptions() async {
1177+
final pigeonVar_channelName =
1178+
'dev.flutter.pigeon.in_app_purchase_storekit.InAppPurchase2API.showManageSubscriptions$pigeonVar_messageChannelSuffix';
1179+
final pigeonVar_channel = BasicMessageChannel<Object?>(
1180+
pigeonVar_channelName,
1181+
pigeonChannelCodec,
1182+
binaryMessenger: pigeonVar_binaryMessenger,
1183+
);
1184+
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(null);
1185+
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
1186+
1187+
_extractReplyValueOrThrow(pigeonVar_replyList, pigeonVar_channelName, isNullValid: true);
1188+
}
11751189
}
11761190

11771191
abstract class InAppPurchase2CallbackAPI {

‎packages/in_app_purchase/in_app_purchase_storekit/lib/src/store_kit_2_wrappers/sk2_appstore_wrapper.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ final class AppStore {
2626
Future<void> presentOfferCodeRedeemSheet() {
2727
return hostApi2.presentOfferCodeRedeemSheet();
2828
}
29+
30+
/// Dart wrapper for StoreKit2's showManageSubscriptions(in:)
31+
/// Presents the App Store sheet that lets users manage their subscriptions.
32+
/// https://developer.apple.com/documentation/storekit/appstore/showmanagesubscriptions(in:)
33+
Future<void> showManageSubscriptions() {
34+
return hostApi2.showManageSubscriptions();
35+
}
2936
}

‎packages/in_app_purchase/in_app_purchase_storekit/pigeons/sk2_pigeon.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ abstract class InAppPurchase2API {
269269

270270
@async
271271
void presentOfferCodeRedeemSheet();
272+
273+
@async
274+
void showManageSubscriptions();
272275
}
273276

274277
@FlutterApi()

‎packages/in_app_purchase/in_app_purchase_storekit/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: in_app_purchase_storekit
22
description: An implementation for the iOS and macOS platforms of the Flutter `in_app_purchase` plugin. This uses the StoreKit Framework.
33
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_storekit
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
5-
version: 0.4.13+3
5+
version: 0.4.14
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)