亲爱的,
我对应用程序购买中的 iOS 有疑问,具体针对“自动续订订阅”我开发的应用程序包含免费功能和付费功能:基于“每月订阅和用户免费 1 周试用”的付费功能
所有过程都很好:我们将获取产品&用户可以购买&我们将验证收据(我知道配方包含“is_trial..”和“Purchase_date”的标志......)......
请注意:我在购买或恢复后称为“验证配方”
但我有以下问题:我怎么知道以下内容:
1-如果用户第二次打开应用程序,我怎么知道用户是否仍处于试用期?
2-如果用户在 1 个月后打开应用程序:我如何知道订阅是否过期......(所以如果订阅过期,我会再次拨打购买电话......)
, 是每次用户打开应用时我都会调用“验证配方”的解决方案,这样我就可以知道用户是在付费期间还是在付费期(过期)之后……</p>
或者我将在本地保存购买日期(例如:用户默认)&当用户打开应用程序时,我会将当前日期与购买日期进行比较,我真的不认为这是一个真实的答案,因为用户可能会更改设备日期......。
请指教……</p>
代码如下:
所以现在当打开应用程序时:我请求产品:
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) }
}
}