希望这些信息足以帮助我解决问题。

我正在通过提供给我的模板化源代码设置“应用内购买”。在我的MKStoreManager.m文件中,为我的应用内购买设置了FeatureID(例如:static NSString *featureAId = @"com.logannat.myfirstgameTier1";)

在游戏中单击实际按钮时,一种方法

- (void) buyFeature:(SKProduct*) product
{
    if ([SKPaymentQueue canMakePayments])
    {
        SKPayment *payment = [SKPayment paymentWithProduct:product];
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
}

最终被打电话。我设置了一个断点,并发现错误在该代码的最后一行。

在断点状态下显示的变量是:
  • self =(MKStoreManager *)“参考编号”
  • 产品=(SKProduct *)无
  • 支付=(SKPayment *)“参考编号”

  • 我似乎无法弄清楚问题出在哪里。任何帮助将不胜感激,谢谢。

    最佳答案

    我现在没有很快,所以我给您Xcode代码,请相应地转换语法,它并不那么困难。
    因此,这是购买的应用程序的完整生命周期。
    在头文件中调用这些代表,并设置布尔值以检查您购买的状态

    像那样


    #import <UIKit/UIKit.h>
    BOOL arefavorite_access;
    BOOL areAdsRemoved;
    @interface ViewController : UIViewController<SKProductsRequestDelegate, SKPaymentTransactionObserver>{
    }
    
    - (IBAction)purchase;
    - (IBAction)restore;
    - (IBAction)tapsRemoveAds:(id)sender;
    @end
    

    您需要在按钮选择器的touchupinside上激活这个tapsRemoveAds方法,然后它才能开始工作。

    - (IBAction)tapsRemoveAds:(id)sender{
        UIButton*btn=(UIButton*)sender;
        NSString *featureAId = @"com.logannat.myfirstgameTier1";
    
        NSLog(@"User requests to remove ads");
        if([SKPaymentQueue canMakePayments]){
            NSLog(@"User can make payments");
            SKProductsRequest *productsRequest;
            if (btn.tag==1) {
                productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject: featureAId]];
                _protype= featureAId;
            }
    
            productsRequest.delegate = self;
            [productsRequest start];
    
            }
            else{
                NSLog(@"User cannot make payments due to parental controls");
                //this is called the user cannot make payments, most likely due to parental controls
            }
    
    }
    

    产品索取方法

    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
        SKProduct *validProduct = nil;
        int count = [response.products count];
        if(count > 0){
            validProduct = [response.products objectAtIndex:0];
            NSLog(@"Products Available!");
            [self purchase:validProduct];
        }
        else if(!validProduct){
            NSLog(@"No products available");
            //this is called if your product id is not valid, this shouldn't be called unless that happens.
        }
    }
    
    - (IBAction)purchase:(SKProduct *)product{
        SKPayment *payment = [SKPayment paymentWithProduct:product];
    
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
    
    - (IBAction) restore{
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }
    
    - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
        NSLog(@"%@",queue );
        NSLog(@"Restored Transactions are once again in Queue for purchasing %@",[queue transactions]);
    
        NSMutableArray *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];
            NSLog (@"product id is %@" , productID);
            _protype=productID;
            [self doRemoveAds];
    
            // here put an if/then statement to write files based on previously purchased items
            // example if ([productID isEqualToString: @"youruniqueproductidentifier]){write files} else { nslog sorry}
        }
        if(queue.transactions.count==0){
    
            [Utilities showOKAlertWithTitle:@"Restore purchase" message:@"you have no product available for restoration"];
    
        }
    
    }
    
    
    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
        for(SKPaymentTransaction *transaction in transactions){
            switch(transaction.transactionState){
                case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
                    //called when the user is in the process of purchasing, do not add any of your own code here.
                    break;
                case SKPaymentTransactionStatePurchased:
                    //this is called when the user has successfully purchased the package (Cha-Ching!)
                    [self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                    NSLog(@"Transaction state -> Purchased");
                    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
                    break;
                case SKPaymentTransactionStateRestored:
                    NSLog(@"Transaction state -> Restored");
    
                    _protype=transaction.payment.productIdentifier;
                    [self doRemoveAds];
                    //add the same code as you did from SKPaymentTransactionStatePurchased here
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
                    break;
                case SKPaymentTransactionStateFailed:
                    //called when the transaction does not finish
                    if(transaction.error.code == SKErrorPaymentCancelled){
    
                        NSLog(@"Transaction state -> Cancelled");
                        //the user cancelled the payment ;(
    
                    }
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
                    break;
            }
        }
    }
    
    
    - (void)doRemoveAds{
        //   // [self.bannerView setAlpha:0];
    
         NSString *featureAId = @"com.logannat.myfirstgameTier1";
     NSString *featureAIdnumber2 = @"com.logannat.myfirstgameTier2";
        if([_protype isEqualToString:featureAId]){
    
            arefavorite_access = YES;
            [[NSUserDefaults standardUserDefaults] setBool:arefavorite_access forKey:frareFavoriteRemoved];
            [[NSUserDefaults standardUserDefaults] synchronize];
    
        }
    
        else if([_protype isEqualToString: featureAIdnumber2]){
    
            areAdsRemoved = YES;
            [[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:frareAdsRemoved];
            [[NSUserDefaults standardUserDefaults] synchronize];
    
        }
    
        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
       // [self showpopUp:self];
    
    }
    

    如果此答案对您有帮助,请投票并接受答案

    关于ios - 快速的应用内购买,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32418540/

    10-09 16:12
    查看更多