在iphone sdk购买Inapp

时间:2012-03-07 10:50:51

标签: iphone ios ios4 in-app-purchase in-app-billing

我有一个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

的按钮

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

在上面的代码中

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.please帮助我这样做。

1 个答案:

答案 0 :(得分:0)

//
- (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中检索此内容