在AppStore、Google Play和Amazon Store中,应用程序可以应用多个IAP(应用内购买)。 以下是在iOS中获取应用程序中已应用IAP数量的示例代码:
Swift语言:
let receiptURL = Bundle.main.appStoreReceiptURL
let receiptData = try? Data(contentsOf: receiptURL!)
let receiptString = receiptData?.base64EncodedString(options: [])
let receiptBody = ["receipt-data" : receiptString!, "password" : "your_shared_secret_password"]
let receiptURLIOS = URL(string: "https://buy.itunes.apple.com/verifyReceipt")
var request = URLRequest(url: receiptURLIOS!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: receiptBody, options: [])
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseDictionary = responseJSON as? [String: Any],
let receipt = responseDictionary["receipt"] as? [String: Any],
let inAppPurchases = receipt["in_app"] as? [[String: Any]] {
let numberOfPurchases = inAppPurchases.count
print("Number of In-App Purchases: \(numberOfPurchases)")
}
}
task.resume()
在安卓中,可以使用Google Play的API来获取应用程序中已应用IAP数量。以下是一个示例代码:
Java语言:
//设置API凭据 GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("path/to/key_file.json")) .createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
//创建AndroidPublisher对象 AndroidPublisher publisher = new AndroidPublisher.Builder(GoogleNetHttpTransport.newTrustedTransport(),