Skip to content

Instantly share code, notes, and snippets.

@hartlco
Last active August 29, 2015 14:06
Show Gist options
  • Save hartlco/544e2a37cca7c7795926 to your computer and use it in GitHub Desktop.
Save hartlco/544e2a37cca7c7795926 to your computer and use it in GitHub Desktop.
Obtaining the Appstore receipt for iOS Apps
class AppStoreReceiptObtainer: NSObject, SKRequestDelegate {
let receiptUrl = NSBundle.mainBundle().appStoreReceiptURL
func obtainReceipt() {
var fileExists = NSFileManager.defaultManager().fileExistsAtPath(receiptUrl.path!)
if fileExists {
println("Appstore Receipt already exists")
return;
}
requestReceipt()
}
func requestReceipt() {
println("request a receipt")
let request = SKReceiptRefreshRequest(receiptProperties: nil)
request.delegate = self
request.start()
}
//MARK: SKRequestDelegate methods
func requestDidFinish(request: SKRequest!) {
println("request did finish")
var fileExists = NSFileManager.defaultManager().fileExistsAtPath(receiptUrl.path!)
if fileExists {
println("Appstore Receipt now exists")
return
}
println("something went wrong while obtaining the receipt, maybe the user did not successfully enter it's credentials")
}
func request(request: SKRequest!, didFailWithError error: NSError!) {
println("request did fail with error: \(error.domain)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment