Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.22

* Adds `sync()` and `countryCode()`.

## 0.3.21

* Adds Swift Package Manager compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import StoreKit

@available(iOS 15.0, macOS 12.0, *)
extension InAppPurchasePlugin: InAppPurchase2API {

// MARK: - Pigeon Functions

/// Wrapper method around StoreKit2's canMakePayments() method
Expand Down Expand Up @@ -146,6 +145,31 @@ extension InAppPurchasePlugin: InAppPurchase2API {
}
}

/// Wrapper method around StoreKit2's countryCode() method
/// https://developer.apple.com/documentation/storekit/storefront/countrycode
func countryCode(completion: @escaping (Result<String, Error>) -> Void) {
Task {
guard let currentStorefront = await Storefront.current else {
let error = PigeonError(
code: "storekit2_failed_to_fetch_country_code",
message: "Storekit has failed to fetch the country code.",
details: "Storekit has failed to fetch the country code.")
return completion(.failure(error))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

completion(.failure(error))
return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i changed it but whats the difference in practice?

Copy link
Contributor

@hellohuanlin hellohuanlin Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say we have a function that doesn't return:

func print(stuff: String) {
  ...
}

When we call it, we simply do:

if wannaPrint {
  print(stuff: "abc")
}

And if we want to early return, just do:

if wannaPrint {
  print(stuff: "abc")
  return
}

Now, what you were trying to do was:

if wannaPrint {
  return print(stuff: "abc")
}

For most languages, this code won't even compile. However, it compiles fine here in Swift, because Void is an actual type in Swift. So the above code is equivalent to:

if wannaPrint {
  let answer = print(stuff: "abc")
  return answer
}

Here the answer is of type Void, and of value Void(). While it's perfectly valid to write return Void(), I have never seen anyone writing it. People just do return for a function that returns void.

A fun fact, do you know thatVoid is a typealias of an empty tuple?

public typealias Void = ()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that makes sense.

}
return completion(.success(currentStorefront.countryCode))
}
}

func sync(completion: @escaping (Result<Void, Error>) -> Void) {
Task {
do {
try await AppStore.sync()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing completion on success.

} catch {
fatalError("Failed to sync to the AppStore: \(error)")
}
}
}

/// This Task listens to Transation.updates as shown here
/// https://developer.apple.com/documentation/storekit/transaction/3851206-updates
/// This function should be called as soon as the app starts to avoid missing any Transactions done outside of the app.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v22.7.3), do not edit directly.
// Autogenerated from Pigeon (v25.1.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon

import Foundation
Expand All @@ -18,9 +18,9 @@ import Foundation
final class PigeonError: Error {
let code: String
let message: String?
let details: Any?
let details: Sendable?

init(code: String, message: String?, details: Any?) {
init(code: String, message: String?, details: Sendable?) {
self.code = code
self.message = message
self.details = details
Expand Down Expand Up @@ -521,6 +521,8 @@ protocol InAppPurchase2API {
func startListeningToTransactions() throws
func stopListeningToTransactions() throws
func restorePurchases(completion: @escaping (Result<Void, Error>) -> Void)
func countryCode(completion: @escaping (Result<String, Error>) -> Void)
func sync(completion: @escaping (Result<Void, Error>) -> Void)
}

/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
Expand Down Expand Up @@ -676,6 +678,41 @@ class InAppPurchase2APISetup {
} else {
restorePurchasesChannel.setMessageHandler(nil)
}
let countryCodeChannel = FlutterBasicMessageChannel(
name:
"dev.flutter.pigeon.in_app_purchase_storekit.InAppPurchase2API.countryCode\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
countryCodeChannel.setMessageHandler { _, reply in
api.countryCode { result in
switch result {
case .success(let res):
reply(wrapResult(res))
case .failure(let error):
reply(wrapError(error))
}
}
}
} else {
countryCodeChannel.setMessageHandler(nil)
}
let syncChannel = FlutterBasicMessageChannel(
name: "dev.flutter.pigeon.in_app_purchase_storekit.InAppPurchase2API.sync\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
syncChannel.setMessageHandler { _, reply in
api.sync { result in
switch result {
case .success:
reply(wrapResult(nil))
case .failure(let error):
reply(wrapError(error))
}
}
}
} else {
syncChannel.setMessageHandler(nil)
}
}
}
/// Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.
Expand Down Expand Up @@ -713,7 +750,7 @@ class InAppPurchase2CallbackAPI: InAppPurchase2CallbackAPIProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(PigeonError(code: code, message: message, details: details)))
} else {
completion(.success(Void()))
completion(.success(()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class _MyAppState extends State<_MyApp> {
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
),
onPressed: () => _iapStoreKitPlatform.restorePurchases(),
onPressed: () => _iapStoreKitPlatformAddition.sync(),
child: const Text('Restore purchases'),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class InAppPurchaseStoreKitPlatform extends InAppPurchasePlatform {
/// See: https://developer.apple.com/documentation/storekit/skstorefront?language=objc
@override
Future<String> countryCode() async {
if (_useStoreKit2) {
return Storefront().countryCode();
}
return (await _skPaymentQueueWrapper.storefront())?.countryCode ?? '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
// found in the LICENSE file.

import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_interface.dart';
import '../in_app_purchase_storekit.dart';

import '../in_app_purchase_storekit.dart';
import '../store_kit_2_wrappers.dart';
import '../store_kit_wrappers.dart';

/// Contains InApp Purchase features that are only available on iOS.
class InAppPurchaseStoreKitPlatformAddition
extends InAppPurchasePlatformAddition {
/// Synchronizes your app’s transaction information and subscription status
/// with information from the App Store.
/// Storekit 2 only
Future<void> sync() {
return AppStore().sync();
}

/// Present Code Redemption Sheet.
///
/// Available on devices running iOS 14 and iPadOS 14 and later.
/// Available for StoreKit 1 and 2
Future<void> presentCodeRedemptionSheet() {
return SKPaymentQueueWrapper().presentCodeRedemptionSheet();
}
Expand Down
Loading