Skip to content

Instantly share code, notes, and snippets.

@martarodriguezm
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martarodriguezm/4da76c43193f11a32eb8 to your computer and use it in GitHub Desktop.
Save martarodriguezm/4da76c43193f11a32eb8 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface RestorePurchases : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver, SKRequestDelegate, UIActionSheetDelegate>
@end
- (IBAction)removeAds:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Remove Ads", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
destructiveButtonTitle:NSLocalizedString(@"Buy Remove Ads", nil)
otherButtonTitles:NSLocalizedString(@"Restore previous purchase", nil), nil];
[actionSheet showInView:self.view];
}
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
//Buy
} else if(buttonIndex == 1) {
//Restore purchases
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
NSLog(@"received restored transactions: %lu", (unsigned long)queue.transactions.count);
if(queue.transactions.count > 0) {
for (SKPaymentTransaction *transaction in queue.transactions) {
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if (receipt) {
NSString *receiptDataString = [receipt base64EncodedStringWithOptions:0];
NSLog(@"Purchase reference:%@",receiptDataString);
//Do whatever with the purchase
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
//Restore succeeded
} else {
//Show error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment