我正在努力做到这一点,以便如果我在应用程序上购买商品,可以隐藏我放置的iAd。
但是,当我进行测试时,只有我的还原按钮是可点击的,而我无法点击购买按钮。

我的应用程式错过了什么吗?

- (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];
}

- (IBAction)PurchaseItem:(id)sender {
    _purchaseController = [[PurchasedViewController alloc] initWithNibName:nil bundle:nil];
    _purchaseController.productID = @"com.myname.appname.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];
}




购买视图

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _buyButton.enabled = NO;
}

- (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];
}

最佳答案

您可以通过以下方式明确禁用它:_buyButton.enabled = NO;viewDidLoad上,并且仅在收到产品时将其设置为启用。只能通过首先点击按钮来进行操作。

10-07 13:46