Reputation: 906
I am trying to enable a In built function through a non consumable purchase. I could retrieve my product and make a purchase but I got a issue in persisting the purchase. When I purchase the product,my button (disabled initially) in view controller has to be enabled and also I kept a label changing its name "purchased".After purchase my label text changes and button enabled, but when I stop and run back Xcode the label state only persists not my button it goes back to disabled state.
** view controller **
-(void)Purchased {
Label.text = @"item has been purchased";
_newview.enabled=YES;
NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
[saveapp setBool:TRUE forKey:k_Save];
[saveapp synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
bool saved = [saveapp boolForKey:k_Save];
if (!saved) {
/// not save code here
NSLog(@"wooo");
} else {
///saved code here
Label.text = @"item has been purchased";
}
// Do any additional setup after loading the view, typically from a nib.
}
** purchased view controller **
-(void)UnlockPurchase {
_buyButton.enabled = NO;
[_buyButton setTitle:@"Purchased" forState:UIControlStateDisabled];
[_homeViewController Purchased];
}
I called this "UnlockPurchase" in updated transactions delegate function. Now the worst case here is after purchase this "_buyButton" state gets back soon to enabled.
Is this steps correct or wrong ? How to make the purchase to persist ? Please help !
Upvotes: 0
Views: 211
Reputation: 7667
try to put the same code (to enable the button) in your viewDidLoad method, too…
…
bool saved = [saveapp boolForKey:k_Save];
….
} else {
///saved code here
Label.text = @"item has been purchased”;
//Disable BuyButton
//Enable your DesiredButton
}
UPD
add SKPaymentTransactionStateRestored
to your method:
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
...
case SKPaymentTransactionStateRestored:[self UnlockPurchase];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
....
Upvotes: 1