0

これは私を夢中にさせていますが、iAds を削除するためにアプリ内購入をしようとしています。私のアプリは、この 1 つの部分を除いて完全であり、これを行う方法を一生理解することはできません。ストーリーボードでiAdバナーを使用してからコードを追加することで、iAdをアプリに追加しました

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}

- (void)bannerView:(ADBannerView *) banner didFailToReceiveAdWithError:error{

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}

私の .m では、iAd はシミュレーターで動作しますが、これらを削除するためにアプリ内購入が必要です。これを許可するために iTunes Connect でプロセスを既に実行しましたが、これを実装するために必要な Xcode でのコーディングがわかりません

これを実装してみました

#define kRemoveAdsProductIdentifier @"put your product id (the one that we just made in iTunesConnect) in here"

- (void)tapsRemoveAds{
NSLog(@"User requests to remove ads");

if([SKPaymentQueue canMakePayments]){
    NSLog(@"User can make payments");

    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
    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{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

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

 NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
    if(SKPaymentTransactionStateRestored){
        NSLog(@"Transaction state -> Restored");
        //called when the user successfully restores a purchase
        [self doRemoveAds];
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        break;
    }

}

}

  - (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");
            break;
        case SKPaymentTransactionStateRestored:
            NSLog(@"Transaction state -> Restored");
            //add the same code as you did from SKPaymentTransactionStatePurchased here
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        case SKPaymentTransactionStateFailed:
            //called when the transaction does not finnish
            if(transaction.error.code != SKErrorPaymentCancelled){
                NSLog(@"Transaction state -> Cancelled");
                //the user cancelled the payment ;(
            }
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
    }
}
}
4

1 に答える 1

0

ユーザーが IAP を購入したかどうかを確認する必要があります。完了している場合は、iAd ビューを削除します (または setAlpha = 0)。

アップデート

SKPaymentTransactionStatePurchasedユーザーが支払いを完了した瞬間 ( /を受け取る場所) にSKPaymentTransactionStateRestoredフラグを保存してNSUserDefaults、ユーザーが IAP を購入したかどうかを確認し、それに応じて iAd を表示/非表示にします。

に何もない場合はNSUserDefaults、(1) ユーザーが IAP を購入していない、または (2) ユーザーが購入したが、何らかの理由で情報が失われたNSUserDefaults(ユーザーがアプリを削除して再インストールしたなど) ことを意味します。どちらの場合も、ユーザーに IAP の購入を依頼するのが正しい方法です。(2) が発生した場合、App Store は自動的に IAP を復元し、SKPaymentTransactionStateRestored. とのコードは似SKPaymentTransactionStateRestoredSKPaymentTransactionStatePurchasedいるはずです:NSUserDefaultsフラグを設定します。

混乱を避けるために、一部のアプリには[復元]ボタンが用意されています。このボタンは、ユーザーが IAP を購入した場合に呼び出し[SKPaymentQueue restoreCompletedTransactions]て生成します。SKPaymentTransactionStateRestoredユーザーが IAP を購入していない場合、これは何もしません。

于 2013-10-31T22:14:02.400 に答える