通过In App Purchase删除广告

时间:2013-12-17 03:25:41

标签: ios in-app-purchase iad

我正在尝试使用In App Purchase删除广告,但它不起作用。当我打开我的应用程序时,横幅出现,当我点击横幅时,它说它正常工作。然而,几秒钟后它会消失,然后在几分钟后再次出现。只是保持在同一个屏幕上。我的代码是否正确?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
    bool saved = [saveapp boolForKey:k_Save];
    if (!saved) {
        /// not save code here
    } else {
        ///saved code here
        Label.text = @"item has been purchased";
    }

}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
    bool saved = [saveapp boolForKey:k_Save];
    if (!saved) {
        /// not save code here
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:1];
        [UIView commitAnimations];
    } else {
        ///saved code here
    }
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:0];
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

这是为了保存

    - (IBAction)PurchaseItem:(id)sender {
        _purchaseController = [[PurchasedViewController alloc] initWithNibName:nil bundle:nil];
        _purchaseController.productID = @"com.myname.test.iap1";
        [[SKPaymentQueue defaultQueue] addTransactionObserver:_purchaseController];
        [self presentViewController:_purchaseController animated:YES completion:NULL];
        [_purchaseController getProductID:self];
    }

    -(void)Purchased {
        Label.text = @"item has been purchased";
        iadBanner.hidden = YES;
        NSUserDefaults *saveapp = [NSUserDefaults standardUserDefaults];
        [saveapp setBool:TRUE forKey:k_Save];
        [saveapp synchronize];
    }



- (IBAction)BuyProduct:(id)sender {
    SKPayment *payment = [SKPayment paymentWithProduct:_product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}



- (IBAction)Restore:(id)sender {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    [self UnlockPurchase];
}

-(void)getProductID:(ViewController *)viewController {
    _homeViewController = viewController;
    if ([SKPaymentQueue canMakePayments]) {
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.productID]];
        request.delegate = self;
        [request start];
    } else
        _productDescription.text = @"Please enable in app purchase in your settings";
}

#pragma mark _
#pragma mark SKProductsRequestDelegate

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSArray *products = response.products;
    if (products.count != 0) {
        _product = products[0];
        _buyButton.enabled = YES;
        _productTitle.text = _product.localizedTitle;
        _productDescription.text = _product.localizedDescription;
    } else {
        _productTitle.text = @"Product Not Found";
    }
    products = response.invalidProductIdentifiers;
    for (SKProduct *product in products) {
        NSLog(@"Product not Found: %@", product);
    }
}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:[self UnlockPurchase];
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:NSLog(@"Transaction Failed");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            default:
                break;

        }
    }
}

-(void)UnlockPurchase {
    _buyButton.enabled = NO;
    [_buyButton setTitle:@"Purchased" forState:UIControlStateDisabled];
    [_homeViewController Purchased];
}

2 个答案:

答案 0 :(得分:0)

嗯,我看到的至少一个潜在问题是你试图隐藏:

 iadBanner.hidden = YES;

但我没有看到其他地方提到的财产。它合成了吗?在IB中陷入困境?

答案 1 :(得分:0)

使用ios7代替self.canDisplayBannerAds,它更容易,更直接

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    bool adsRemoved = [userDefaults boolForKey:@"removeAds"];
    self.canDisplayBannerAds=!adsRemoved;
}