Skip to content

Instantly share code, notes, and snippets.

@raypendergraph
Created November 10, 2017 13:42
Show Gist options
  • Save raypendergraph/b9da40ecce2cbe8b69a8067d4a56ab23 to your computer and use it in GitHub Desktop.
Save raypendergraph/b9da40ecce2cbe8b69a8067d4a56ab23 to your computer and use it in GitHub Desktop.
Swift print Apple app receipt verification
import UIKit
import CloudKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
func main() {
let filePath = Bundle.main.path(forResource: "sandboxReceipt", ofType: "")
// get the contentData
let base64 = FileManager.default.contents(atPath: filePath!)!.base64EncodedString()
let requestContents = ["receipt-data": base64]
guard let jsonData = try? JSONSerialization.data(withJSONObject: requestContents) else { return }
//print(try? JSONSerialization.jsonObject(with: jsonData))
let storeURL = URL(string: "https://sandbox.itunes.apple.com/verifyReceipt")
var request = URLRequest(url: storeURL!)
request.httpMethod = "POST"
request.httpBody = jsonData
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
print(request.)
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
if let error = error {
print(error.localizedDescription)
return
}
guard let data = data else {
print("No data")
return
}
do {
guard let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary else {
return
}
print(json)
} catch {
print(error.localizedDescription)
}
}
task.resume()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment