如何从iOS 11的iOS收据中获取original_application_version(第一个购买的版本号)? [英] How to get the original_application_version (the first purchased version number) out of iOS Receipt in iOS 11?
问题描述
我有一个付费的iOS应用.
I have a paid iOS App.
我需要从Apple AppStore收据中获取original_application_version
号(用户购买的第一个版本).
I need to get the original_application_version
number (the first version purchased by the user) from the Apple AppStore Receipt.
要获取收据,当我的应用加载时,我使用checkReceiptFromAppStore()
函数:
To get the receipt, when my app loads, I use checkReceiptFromAppStore()
function:
func checkReceiptFromAppStore() {
let receipt = self.getReceipt()
print("receipt Data is: \(receipt)") // prints this: receipt Data is: Optional(5141 bytes)
}
getReceipt()
函数如下:
func getReceipt() -> Data? {
if Bundle.main.appStoreReceiptURL != nil {
print("app receipt: \(Bundle.main.appStoreReceiptURL)")
do {
let receiptData = try Data(contentsOf: Bundle.main.appStoreReceiptURL!)
return receiptData
} catch {
print("error converting receipt to Data: \(error.localizedDescription)")
}
}
return nil
}
我观看了 WWDC 2017 Advanced StoreKit 有关在应用内购买的视频和收据验证以及 WWDC 2013有关使用收据的视频,请阅读其他内容 与我的问题有关的资源(此,此,此,此,此和 https://developer. apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html
I've watched WWDC 2017 Advanced StoreKit video about In App purchases and receipt validation and WWDC 2013 Video about using Receipts, read different resources related to my problem (this, this, this, this, this, this, and this...), but I still don't understand what to do next to get the "original_application_version" from the App Store Receipt. I need only this field and don't understand why is it so hard to get it. I've read this too: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html
我认为运行print("receipt Data is:(receipt)")时,收据并不为零,它会打印以下内容:收据数据为:可选(5141字节)
I think that the receipt is not nil as long at when I run print("receipt Data is: (receipt)") it prints this: receipt Data is: Optional(5141 bytes)
我可以假设我应该解析收据以获取该字段.我可以使用Decodable做到吗?有没有最简单的方法来获取此original_application_version
字段?如果没有收据验证,是否可以这样做?
I can suppose that I should parse the receipt to get that field. Can I do it using Decodable? Is there the easiest way to get this original_application_version
field? Is it possible to do this without a receipt validation?
我只需要获取original_application_version
字段即可检测用户购买的第一个版本的数量.如果您知道用于获取第一个购买的版本号的其他解决方案,我将很高兴听到他们的声音.
I need to get the original_application_version
field only to detect the number of the first version bought by the user. If you know any other solutions to get the first purchased version number, I'd be glad to hear them.
我正在用Xcode 9,Swift 4,iOS 11开发
I'm developing in Xcode 9, Swift 4, iOS 11
任何答案表示感谢,谢谢.
Any answers appreciated, thanks.
推荐答案
应用程序收据中的所有收据字段均为二进制格式.您应使用任何一种应用程序收据解码器,以获取original_application_version.在使用应用收据之前先对其进行验证是一件好事.例如,您可以使用RMStore框架( RMStore ).它包含收据验证器和解码器.示例Obj-C来源:
All receipt fields are in binary format within application receipt. You should use any kind of app receipt decoder in order to get original_application_version. It is always good thing to validate app receipt before using its contents. For example, you can use RMStore framework (RMStore). It contains receipt validator as well as decoder. Example Obj-C source:
RMAppReceipt *appReceipt = RMAppReceipt.bundleReceipt;
if (appReceipt != nil &&
[appReceipt.originalAppVersion length] > 0 &&
![appReceipt.originalAppVersion isEqualToString:@"1.0"]) {
//Process your original app version
} else {
//Probably it is sandbox mode or app install via Xcode
//Or maybe you should force app receipt refresh
}
这篇关于如何从iOS 11的iOS收据中获取original_application_version(第一个购买的版本号)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!