Inapp purchase in iphone sdk
-
08-12-2019 - |
문제
i am having an application with inapp purchase,i completed the code side of inapp,there is a buy button in my viewcontroller ,when the user tap the this button it go through the payment process,if the payment received is sucess ,i just need to enable a button just beside the buy button.i get the alert that payment sucess ,if the payment is sucess i need to enable a button,that i was disabled earlier. in button click
if ([SKPaymentQueue canMakePayments]) {
// Yes, In-App Purchase is enabled on this device!
// Proceed to fetch available In-App Purchase items.
// Replace "Your IAP Product ID" with your actual In-App Purchase Product ID,
// fetched from either a remote server or stored locally within your app.
SKProductsRequest *prodRequest= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"com.mycompny.myproduct"]];
prodRequest.delegate = self;
[prodRequest start];
// Replace "Your IAP Product ID" with your actual In-App Purchase Product ID.
SKPayment *paymentRequest = [SKPayment paymentWithProductIdentifier: @"com.mycompny.myproduct"];
// Assign an Observer class to the SKPaymentTransactionObserver,
// so that it can monitor the transaction status.
[[SKPaymentQueue defaultQueue] addTransactionObserver:inappObserver];
// Request a purchase of the selected item.
[[SKPaymentQueue defaultQueue] addPayment:paymentRequest];
} else {
// Notify user that In-App Purchase is disabled via button text.
[inappButton setTitle:@"In-App Purchase is Disabled" forState:UIControlStateNormal];
inappButton.enabled = NO;
i need to enable a button named _btnunlockfeature
in inapppurchaseobserver.m
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for(SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// Item is still in the process of being purchased
break;
case SKPaymentTransactionStatePurchased:
// Item was successfully purchased!
// --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
// The purchased item ID is accessible via
// transaction.payment.productIdentifier
// After customer has successfully received purchased content,
// remove the finished transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
case SKPaymentTransactionStateRestored:
// Verified that user has already paid for this item.
// Ideal for restoring item across all devices of this customer.
// --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
// The purchased item ID is accessible via
// transaction.payment.productIdentifier
// After customer has restored purchased content on this device,
// remove the finished transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
case SKPaymentTransactionStateFailed:
// Purchase was either cancelled by user or an error occurred.
if (transaction.error.code != SKErrorPaymentCancelled) {
// A transaction error occurred, so notify user.
}
// Finished transactions should be removed from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
}
}
}
in the above code the
case SKPaymentTransactionStatePurchased:
// Item was successfully purchased!
// --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
// The purchased item ID is accessible via
// transaction.payment.productIdentifier
// After customer has successfully received purchased content,
// remove the finished transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
i think here i want to write the enabled code ,this code is in NSobject not in the same viewcontroller.please help me to do this.
해결책
//
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful
{
// remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];
if (wasSuccessful)
{
// send out a notification that we’ve finished the transaction
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Content successfully purchased" message:nil delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];
[alert release];
count = 1;
}
else
{
// send out a notification for the failed transaction
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo];
}
}
- (void)provideContent:(NSString *)productId
{
if ([productId isEqualToString:kInAppPurchaseProUpgradeProductId])
{
// enable the pro features
// Save the Value stating user Did purchase Sound Pack
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"SoundPack"];
[defaults synchronize];
}
}
I do it like this..Storing in the NSUserDefaults..and retrieving this in ViewController