gpt4 book ai didi

iOS 应用内购买 - 自动续订订阅

转载 作者:可可西里 更新时间:2023-11-01 01:59:18 25 4
gpt4 key购买 nike

亲爱的,

我有一个关于 iOS in app purchase 的问题,具体针对“自动更新订阅”我开发的应用程序包含免费功能和付费功能:基于“每月订阅和用户免费试用 1 周”的付费功能

所有过程都很好:我们将获取产品,用户可以购买,我们将验证收据(我知道食谱包含“is_trial..”和“Purchase_date”的标志......)......

请注意:我在购买后或恢复后调用“验证配方”

但我有以下问题:我怎么知道以下内容:

1-如果用户第二次打开应用,我怎么知道用户是否还在试用期?

2- 如果用户在 1 个月后打开应用程序:我怎么知道订阅是否过期......(所以如果订阅过期我会再次调用购买......)

,是每次用户打开应用程序时我都会调用“验证食谱”的解决方案,所以我可以知道用户是否在付费期间或付费期后(过期)......

或者我会在本地保存购买日期(例如:用户默认)&当用户打开应用程序时我会将当前日期与购买日期进行比较,我真的不认为这是一个真实的答案,因为用户可能会更改设备日期...... .

请多多指教……

下面的代码:

现在打开应用程序时: 我要求产品:

SKPaymentQueue.default().add(self)
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")


let productID: NSSet = NSSet(objects: "test.test1.test2.11")



let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}

收到产品时:

  func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("product request")
let myProduct = response.products
for product in myProduct {


list.append(product)
}
}

现在当用户按下按钮时:

for product in self.list {
let prodID = product.productIdentifier
if(prodID == "test.test1.test2.11") {
self.p = product
self.buyProduct()

}
}

下面的方法将调用

 func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add payment")

for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction

switch trans.transactionState {
case .purchased:
receiptValidation()
queue.finishTransaction(trans)

break
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
}
}

&我将验证收据:

func receiptValidation() {
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {

do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)

let receiptString = receiptData.base64EncodedString(options: [])
let dict = ["receipt-data" : receiptString, "password" : "xxxxxxxxxxx"] as [String : Any]

do {
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)

if let sandboxURL = Foundation.URL(string:"https://sandbox.itunes.apple.com/verifyReceipt") {
var request = URLRequest(url: sandboxURL)
request.httpMethod = "POST"
request.httpBody = jsonData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { data, response, error in
if let receivedData = data,
let httpResponse = response as? HTTPURLResponse,
error == nil,
httpResponse.statusCode == 200 {
do {
if let jsonResponse = try JSONSerialization.jsonObject(with: receivedData, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<String, AnyObject> {

} else { print("Failed to cast serialized JSON to Dictionary<String, AnyObject>") }
}
catch { print("Couldn't serialize JSON with error: " + error.localizedDescription) }
}
}
task.resume()
} else { print("Couldn't convert string into URL. Check for special characters.") }
}
catch { print("Couldn't create JSON with error: " + error.localizedDescription) }
}
catch { print("Couldn't read receipt data with error: " + error.localizedDescription) }
}
}

最佳答案

强烈建议您存储用户付费状态并在服务器端进行收据验证。这样你就可以在你的服务器上保存关于用户的元数据、他们的订阅到期、免费试用状态等,并在启动时刷新它。然后,您可以根据用户的开始/结束日期设置对 Apple 服务器的智能轮询以进行收据验证/过期检查(也应在服务器端完成)。这将保持他们的高级或非高级状态,并允许您触发必要的客户端 UI。

关于iOS 应用内购买 - 自动续订订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com