恢复购买功能在ios中无法正常工作

时间:2013-05-07 12:40:26

标签: ios objective-c xcode in-app-purchase

我有一个应用程序,其中我有5个非消费类应用内购买。

我正在使用以下代码进行应用内购买。

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{

    //NSLog(@"into method1");
    purchasedItemIDs = [[NSMutableArray alloc] init];

    NSLog(@"received restored transactions: %i", queue.transactions.count);
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        NSString *productID = transaction.payment.productIdentifier;
        [purchasedItemIDs addObject:productID];
    }

    // [customSpinner hide:YES];

    if (queue.transactions.count==0)
    {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"FameFace" message:@"It seems you have not buy this Package" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];

    }

    else
    {

        NSLog(@"Data available");


       NSString *isInApp = [[NSUserDefaults standardUserDefaults]valueForKey:@"Package"];

        if ([isInApp isEqualToString:@"Pack1"])
        {

            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
            {
                // app already launched
                NSLog(@"Launching secondtime");
            }
            else
            {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
                NSLog(@"Launching first time");
                requestText= @"Animal";
                [self MakeQueryForUpdate];
                [self getdata];
            }




        }

        else if ([isInApp isEqualToString:@"Pack2"])
        {

            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
            {
                // app already launched
                NSLog(@"Launching secondtime");
            }
            else
            {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
                NSLog(@"Launching first time");
                requestText= @"Celebrity";
                [self MakeQueryForUpdate];
                [self getdata];
            }

        }

        else if ([isInApp isEqualToString:@"Pack3"])
        {
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
            {
                // app already launched
                NSLog(@"Launching secondtime");
            }
            else
            {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
                NSLog(@"Launching first time");
                requestText= @"Nature";
                [self MakeQueryForUpdate];
                [self getdata];
            }


        }
}

以下是交易成功时的代码。

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:

                break;

            case SKPaymentTransactionStatePurchased:
                //[self downloadFromURL:[NSURL URLWithString:@"http://www.iphonedevnation.com/tutorials/ForestGreen.mp3"]];
                // flgCheck = 1;
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                [self completeTransaction:transaction];
   NSString *isInApp = [[NSUserDefaults standardUserDefaults]valueForKey:@"Package"];
 if ([isInApp isEqualToString:@"Pack1"])
                {
                    //by manthan...7th  may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Animal";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }

                }

                else if ([isInApp isEqualToString:@"Pack2"])
                {

                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Celebrity";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }

                }

                else if ([isInApp isEqualToString:@"Pack3"])
                {
                    //by manthan... 7th may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Nature";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }
                }

这很好用。

我正在做的是当用户从In App Purchase购买包时,我在我的数据库中插入了一些图像。

但由于产品是非消费品,如果用户删除了应用并尝试恢复图像,则我的数据库不会更新。

任何帮助将不胜感激。

...谢谢

被修改

当用户尝试恢复购买时,我将代码放在下面的方法中。

由于我有一个恢复按钮并且在按下该按钮时,用户将先前购买可能是2,3或4的包(用户已购买的5个包中的任何一个,因为这些是非消费品)。

case SKPaymentTransactionStateRestored:
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                NSString *pk = [[NSUserDefaults standardUserDefaults]valueForKey:@"Package"];

                if ([pk isEqualToString:@"Pack1"])
                {
                    //by manthan...7th  may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Animal";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }

                }

                else if ([pk isEqualToString:@"Pack2"])
                {
                    //by manthan... 7th may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Celebrity";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }

                }

                else if ([pk isEqualToString:@"Pack3"])
                {
                    //by manthan... 7th may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Nature";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }
                }

当只有一次购买时,我只需在 SKPaymentTransactionStateRestored 中更新我的数据库。

但是当我有多种非消费品时,我该怎么办?

已修改

 if ([transaction.payment.productIdentifier isEqualToString:@"Pack1"])
                {
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Animal";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }
                }


                else if ([transaction.payment.productIdentifier isEqualToString:@"Pack2"])
                {
                    //by manthan... 7th may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Celebrity";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }

                }

                else if ([transaction.payment.productIdentifier isEqualToString:@"Pack3"])
                {
                    //by manthan... 7th may
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
                    {
                        // app already launched
                        NSLog(@"Launching secondtime");
                    }
                    else
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
                        NSLog(@"Launching first time");
                        requestText= @"Nature";
                        [self MakeQueryForUpdate];
                        [self getdata];
                    }
                }

1 个答案:

答案 0 :(得分:2)

您需要在SKPaymentTransactionStateRestored方法中使用updatedTransactions的大小写来执行此操作。

执行恢复此案例时。

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction * transaction in transactions)
   {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                //do your stuff
                break;
            case SKPaymentTransactionStateFailed:
                //do your stuff
                break;
            case SKPaymentTransactionStateRestored:
                //do your database stuff
            default:
                break;
        }
    };
}

参考

  

付款交易状态

     

交易状态。

enum { SKPaymentTransactionStatePurchasing,
SKPaymentTransactionStatePurchased, SKPaymentTransactionStateFailed,
SKPaymentTransactionStateRestored };

typedef NSInteger SKPaymentTransactionState;
  

常量

     

<强> SKPaymentTransactionStatePurchasing

The transaction is being processed by the App Store.
     

<强> SKPaymentTransactionStatePurchased

The App Store successfully processed payment. Your application should provide the content the user purchased.
     

<强> SKPaymentTransactionStateFailed

The transaction failed. Check the error property to determine what happened.
     

<强> SKPaymentTransactionStateRestored

This transaction restores content previously purchased by the user. Read the originalTransaction property to obtain information about the original purchase.

Available in iOS 3.0 and later.

Declared in SKPaymentTransaction.h.

检查SKPaymentTransaction了解更多