我想发布一个具有自动续订订阅功能的 iOS 应用。虽然有大量关于此的信息,但很多已经过时,所以我将说明我到目前为止所取得的成就。
我在 Swift 2.0 中工作,因此任何 objective-c 代码都对我没有帮助。
我不会使用自己的服务器与 Apple 通信以验证收据,因此我认为我要么需要让应用程序直接与 Apple 服务器通信,要么我可以在设备上本地解析收据。
我已经能够使用以下代码在设备上找到收据(不确定是否有多个收据)
func checkForReceipt() { let receiptUrl = NSBundle.mainBundle().appStoreReceiptURL let fileExists = NSFileManager.defaultManager().fileExistsAtPath(receiptUrl!.path!) if fileExists { let receiptData = NSData(contentsOfURL: receiptUrl!) //Now what do I do to decode the data and validate the receipt } else{ requestReceipt() } }
但是,我不知道如何解码收据,然后我才能确定到期日期和其他验证步骤以确保它是有效收据。
我不得不说,对于开发人员来说非常重要和有用的东西却如此难以遵循和定位,这让我感到非常沮丧。非常感谢任何帮助,希望对其他人有用。
最佳答案
这是一个link我发现有帮助
如果我的代码不清楚可以引用一下
下面是我用来查看我的ar-iap订阅状态的功能代码
在下面进一步阅读有关每个相应 * 评论的一些额外信息
func checkForReceipt() {
let receiptUrl = NSBundle.mainBundle().appStoreReceiptURL
let fileExists = NSFileManager.defaultManager().fileExistsAtPath(receiptUrl!.path!)
if fileExists {
let receiptData = NSData(contentsOfURL: receiptUrl!)
let receiptToString = receiptData!.base64EncodedStringWithOptions([])
let dict = ["receipt-data" : receiptToString, "password" : "YOUR SHARED SECRET"] //**
do {
let request = try NSJSONSerialization.dataWithJSONObject(dict, options: []) as NSData!
let storeURL = NSURL(string:"https://sandbox.itunes.apple.com/verifyReceipt")! //***
let storeRequest = NSMutableURLRequest(URL: storeURL)
storeRequest.HTTPMethod = "POST"
storeRequest.HTTPBody = request
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let dataTask = session.dataTaskWithRequest(storeRequest, completionHandler: { (data: NSData?, response: NSURLResponse?, connection: NSError?) -> Void in
do {
let jsonResponse: NSDictionary = try (NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary)!
//****
let expDate: NSDate = self.expirationDateFromResponse(jsonResponse)!
print(expDate)
} catch {
//handle NSJSONSerialization errors
}
})
dataTask.resume()
} catch {
//handle NSJSONSerialization errors
}
} else {
requestReceipt()
}
}
** 您可以从您的 iTunes Connect 帐户获取共享 key : 转到 MyApps > "yourappname"> Features > View Shared Secret > Generate Shared Secret 然后将生成的密码插入字典的密码字段
*** 确保在生产时将 storeURL 更改为“https://buy.itunes.apple.com/verifyReceipt”
**** expirationDateFromResponse(jsonResponse: NSDictionary) -> NSDate?是一个函数,读取apple的json响应,返回ar iap的过期时间
关于ios - 如何为 iOS 应用程序实现 iOS 自动续订订阅收据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33862627/