Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.