iOS 未找到收据

iOS Receipt not found

我正在 Swift 3.0 应用程序中实施应用程序内购买,因此我需要获取应用程序收据以根据 iTunes 商店进行验证。这是我收到收据的方式:

func getReceipt() -> Data? {
    if Bundle.main.appStoreReceiptURL != nil {
        print("app receipt: \(Bundle.main.appStoreReceiptURL)")
        do {
            let receiptData = try Data(contentsOf: Bundle.main.appStoreReceiptURL!)
            print(receiptData)
            return receiptData
        } catch {
            print("error converting receipt to Data: \(error.localizedDescription)")
        }
    }
    return nil
}

我的收据控制台输出 URL 是:

app receipt: Optional(file:///Users/dustinspengler/Library/Developer/XCPGDevices/433E8E8F-B781-4ADC-A92D-5CABC28E94D6/data/Containers/Data/Application/C25BE9B6-FB64-4D49-9CF2-9DA371060A7B/StoreKit/receipt)

然后无法将收据转换为 Data 并且 catch 语句打印:

error converting receipt to Data: The file “receipt” couldn’t be opened because there is no such file.

我在游乐场、模拟器和真实设备上 运行 时得到完全相同的输出所以 这是否意味着考虑到用户有还没有进行应用内购买? 阅读 Apple 的 documentation 时,我的印象是无论之前的购买如何,它们总是被创建。

此答案基于应用程序在您本地计算机上 运行 的上下文。如果该应用在应用商店上架,即使它是免费的,您下载该应用时也会收到一张收据。

@Paulw11 回答:

There is no receipt until the user makes a purchase. For an app downloaded from the App Store (even a free one). This is a purchase, so there will be a receipt. For a debug build from Xcode there is no receipt until an in-app purchase is made.