确保 iOS 应用程序与后端之间的通信成功
Ensure that communication between iOS application and back-end succeeded
简单问题:如何确保iOS应用程序与后端通信成功?
详情:对于我们手机应用中的大部分API调用,如果应用与服务端通信失败,可以"OK" (出于网络原因或其他原因),我们可以使用重试按钮向用户显示一条消息,例如重新加载他的新闻或帖子提要。
然而,在某些情况下,我们想要真正确信应用程序和后端之间的通信永远不会失败,并且此通信的数据可能是 "lost"。
为此,我们以应用内购买 (IAP) 为例:
- 在我的应用中,IAP 信息如下所示:
struct InAppPurchase{
// The id of the purchase
let transactionId: String
// The id of the user who purchased
let userId: String
// The IAP product involved in the purchase (10 or 30 in-app coins for example)
let productId: String
}
- 每次用户进行购买时,我都想将此信息发送到后端,以便在我的应用程序中保存购买历史记录。
- 我不想错过应用程序和服务器之间的任何通信(如果用户向我发送反馈说他为我的应用程序中的某些项目付款,我想通过检查历史记录来确保他真的买了)
- 如果购买成功但调用发送信息到服务器失败(网络错误或其他),此信息不能丢失,必须尽快发送。
我在想这个方法:
- 像这样用
UserDefault
或 Keychain
创建待定购买数组
var pendingPurchases = [InAppPurchase]()
当用户购买商品时,该商品存储在pendingPurchases
数组中。
应用通过发送这个IAP数组与后端通信
如果我从后端收到答案代码 200 SUCCESS
,我可以清除 pendingPurchases
数组。
如果我收到另一个错误代码,我会尝试再次发送此呼叫(限制为连续 3 个,如果是因为网络不好,可能不需要尝试发送)目前请求无限期)直到收到 200 SUCCESS
每次应用程序打开或从后台切换到前台时,我都会检查 pendingPurchases
是否为空。如果不为空,我将请求发送到服务器。
您如何看待这种做法?您如何管理这种您不想成为的数据?"lost"?
您要做的是等待 "finish the transaction",直到您从服务器收到 200
响应。如果您没有完成交易,Apple 会将其保存在付款队列中,并在每次应用程序启动时继续将其发送给您。这将使您有机会在需要时处理对服务器的任何重试。
Finishing a transaction tells StoreKit that you’ve completed
everything needed for the purchase. Unfinished transactions remain in
the queue until they’re finished, and the transaction queue observer
is called every time your app is launched so your app can finish the
transactions. Your app needs to finish every transaction, regardless
of whether the transaction succeeded or failed.
Complete all of the following actions before you finish the
transaction:
- Persist the purchase.
- Download associated content.
- Update your app’s UI to let the user access the product.
要完成交易,请调用付款队列上的 finishTransaction: 方法。
SKPaymentTransaction *transaction = <# The current payment #>;
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
您可以在下面的 Apple 文档中阅读有关完成交易的更多信息:
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/DeliverProduct.html
简单问题:如何确保iOS应用程序与后端通信成功?
详情:对于我们手机应用中的大部分API调用,如果应用与服务端通信失败,可以"OK" (出于网络原因或其他原因),我们可以使用重试按钮向用户显示一条消息,例如重新加载他的新闻或帖子提要。
然而,在某些情况下,我们想要真正确信应用程序和后端之间的通信永远不会失败,并且此通信的数据可能是 "lost"。
为此,我们以应用内购买 (IAP) 为例:
- 在我的应用中,IAP 信息如下所示:
struct InAppPurchase{
// The id of the purchase
let transactionId: String
// The id of the user who purchased
let userId: String
// The IAP product involved in the purchase (10 or 30 in-app coins for example)
let productId: String
}
- 每次用户进行购买时,我都想将此信息发送到后端,以便在我的应用程序中保存购买历史记录。
- 我不想错过应用程序和服务器之间的任何通信(如果用户向我发送反馈说他为我的应用程序中的某些项目付款,我想通过检查历史记录来确保他真的买了)
- 如果购买成功但调用发送信息到服务器失败(网络错误或其他),此信息不能丢失,必须尽快发送。
我在想这个方法:
- 像这样用
UserDefault
或Keychain
创建待定购买数组
var pendingPurchases = [InAppPurchase]()
当用户购买商品时,该商品存储在
pendingPurchases
数组中。应用通过发送这个IAP数组与后端通信
如果我从后端收到答案代码
200 SUCCESS
,我可以清除pendingPurchases
数组。如果我收到另一个错误代码,我会尝试再次发送此呼叫(限制为连续 3 个,如果是因为网络不好,可能不需要尝试发送)目前请求无限期)直到收到
200 SUCCESS
每次应用程序打开或从后台切换到前台时,我都会检查
pendingPurchases
是否为空。如果不为空,我将请求发送到服务器。
您如何看待这种做法?您如何管理这种您不想成为的数据?"lost"?
您要做的是等待 "finish the transaction",直到您从服务器收到 200
响应。如果您没有完成交易,Apple 会将其保存在付款队列中,并在每次应用程序启动时继续将其发送给您。这将使您有机会在需要时处理对服务器的任何重试。
Finishing a transaction tells StoreKit that you’ve completed everything needed for the purchase. Unfinished transactions remain in the queue until they’re finished, and the transaction queue observer is called every time your app is launched so your app can finish the transactions. Your app needs to finish every transaction, regardless of whether the transaction succeeded or failed.
Complete all of the following actions before you finish the transaction:
- Persist the purchase.
- Download associated content.
- Update your app’s UI to let the user access the product.
要完成交易,请调用付款队列上的 finishTransaction: 方法。
SKPaymentTransaction *transaction = <# The current payment #>;
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
您可以在下面的 Apple 文档中阅读有关完成交易的更多信息: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/DeliverProduct.html