Покупка Inapp в iphone SDK - PullRequest
       18

Покупка Inapp в iphone SDK

0 голосов
/ 07 марта 2012

У меня есть приложение с покупкой inapp, я заполнил сторону кода inapp, в моем viewcontroller есть кнопка покупки, когда пользователь нажимает эту кнопку, он проходит через процесс оплаты, если полученный платеж является успешным,Мне просто нужно активировать кнопку рядом с кнопкой покупки. Я получаю уведомление о том, что платеж успешно завершен, если платеж выполнен успешно, мне нужно активировать кнопку, которая была отключена ранее.нажмите кнопку

 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;

Мне нужно включить кнопку с именем _btnunlockfeature

в 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;
        }
    }
}

в приведенном выше коде

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];

Я думаю, что здесь я хочу написать включенный код, этот код находится в NSobject, а не в том же viewcontroller. Пожалуйста, помогите мне сделать это.

1 Ответ

0 голосов
/ 07 марта 2012
//
- (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];
    }
}

Я делаю это следующим образом .. Работа в NSUserDefaults .. и получение этого в ViewController

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...