Skip to content

Instantly share code, notes, and snippets.

@wisaruthk
Created November 6, 2015 03:54
Show Gist options
  • Save wisaruthk/9268a8bcb81458a7587e to your computer and use it in GitHub Desktop.
Save wisaruthk/9268a8bcb81458a7587e to your computer and use it in GitHub Desktop.
iOS Receipt
// Initial Receipt
let receiptURL:NSURL? = NSBundle.mainBundle().appStoreReceiptURL
let data:NSData? = NSData(contentsOfURL: receiptURL!);
if(data == nil){
// No local receipt
print("No local receipt")
} else {
let temp = data!.base64EncodedStringWithOptions([])
let requestContent:NSDictionary = NSDictionary(object: temp, forKey: "receipt-data")
var requestData:NSData? = nil
do {
try requestData = NSJSONSerialization.dataWithJSONObject(requestContent,options: [])
} catch {
print("error \(error)")
}
let storeURL:NSURL! = NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt" )
let storeRequest:NSMutableURLRequest = NSMutableURLRequest(URL: storeURL)
storeRequest.HTTPMethod = "POST";
storeRequest.HTTPBody = requestData;
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(storeRequest, queue: queue, completionHandler: { (resp:NSURLResponse?, data:NSData?, error:NSError?) -> Void in
//
print("store request success with data \(data)");
var result:AnyObject? = nil
do {
try result = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
//try result! = NSJSONSerialization.JSONObjectWithData(data!,0) as! NSDictionary;
}catch {
print("error \(error)");
}
print("urlresp \(resp)");
print("result = \(result)");
print("error= \(error)");
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment