iOS 苹果内购验证凭证请求地址

测试地址

swift 复制代码
https://sandbox.itunes.apple.com/verifyReceipt

正式地址:

swift 复制代码
https://buy.itunes.apple.com/verifyReceipt

示例

swift 复制代码
import Foundation

class InAppPurchaseManager {
    
    static let shared = InAppPurchaseManager()
    
    private let verifyURL = URL(string: "https://buy.itunes.apple.com/verifyReceipt")! // 苹果正式环境验证地址
    
    func verifyReceipt(receiptData: Data, completion: @escaping (Bool) -> Void) {
        let receiptString = receiptData.base64EncodedString(options: [])
        let parameters = ["receipt-data": receiptString]
        
        guard let jsonData = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
            completion(false)
            return
        }
        
        var request = URLRequest(url: verifyURL)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data,
                  let httpResponse = response as? HTTPURLResponse,
                  error == nil,
                  httpResponse.statusCode == 200 else {
                completion(false)
                return
            }
            
            do {
                // 解析服务器返回的数据
                let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                
                // 检查验证结果
                if let status = jsonResponse?["status"] as? Int, status == 0 {
                    // 验证成功
                    completion(true)
                } else {
                    // 验证失败
                    completion(false)
                }
            } catch {
                // JSON 解析失败
                completion(false)
            }
        }
        
        task.resume()
    }
}
相关推荐
用户19729591889112 小时前
WKWebView的重定向(objective_c)
前端·ios
lancoff16 小时前
#3 Creating Shapes in SwiftUI
ios·swiftui
lancoff17 小时前
#1 How to use Xcode in SwiftUI project
ios·swiftui
lancoff17 小时前
#2 Adding Text in SwiftUI
ios·swiftui
良逍Ai出海18 小时前
Build in Public|为什么我开始做一款相册清理 App(听说有竞品年收益40W)
ios·uni-app·ai编程·coding
笑尘pyrotechnic2 天前
LLDB进阶:使用命令行进行检查
ios·objective-c·cocoa·lldb
z***y8622 天前
Swift在iOS中的Xcode
ios·xcode·swift
AirDroid_cn2 天前
iOS 18 后台应用偷跑流量,如何限制?
macos·ios·cocoa
明君879972 天前
Flutter 图纸标注功能的实现:踩坑与架构设计
android·ios
江东小bug王2 天前
深入理解 UINavigationController:生命周期、动画优化与性能调优
ios