Swift In App Purchase

时间:2015-09-05 23:14:42

标签: ios swift in-app-purchase

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

我通过提供给我的模板化源代码设置In App Purchases。在我的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];
    }
}

最终被召唤。我设置了一个断点,发现错误位于该代码的最后一行。

以断点状态显示的变量是:

  1. self =(MKStoreManager *)“参考编号”
  2. product =(SKProduct *)nil 和
  3. payment =(SKPayment *)“参考编号”
  4. 我似乎无法弄清楚这里的问题是什么。非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

我没有swift现在所以我给你Xcode代码请相应地转换语法并不那么困难。 所以这里是购买的应用程序的完整生命周期。 在您的头文件中调用这些代理并设置bool以检查您购买的状态

像那样     

#import <UIKit/UIKit.h> 
BOOL arefavorite_access;
BOOL areAdsRemoved;
@interface ViewController : UIViewController<SKProductsRequestDelegate, SKPaymentTransactionObserver>{
}

- (IBAction)purchase;
- (IBAction)restore;
- (IBAction)tapsRemoveAds:(id)sender;
@end

你需要在你的按钮选择器上触摸这个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];

}

如果这个答案可以帮到你,请投票并接受答案