Last active
August 29, 2015 14:26
-
-
Save martarodriguezm/4da76c43193f11a32eb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <StoreKit/StoreKit.h> | |
@interface RestorePurchases : UIViewController <SKProductsRequestDelegate, SKPaymentTransactionObserver, SKRequestDelegate, UIActionSheetDelegate> | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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