2013-10-31 70 views
0

這一直使我瘋狂,但我試圖通過應用程序內購買來移除iAds。我的應用程序是完整的,除了這一部分,我不能爲我的生活弄清楚如何做到這一點。香港專業教育學院增加了網絡成癮的應用程序已經在故事板使用iAd的橫幅的方式,然後添加代碼如何通過應用程序內購買來移除iAds

-(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,內部審計部門工作在我的模擬器,但我希望有一個在 - 應用程序購買以消除這些。我已經通過iTunes中的歷程連接允許這一點,但我無法弄清楚在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; 
    } 
} 
} 

回答

0

您需要檢查用戶是否有必要買了IAP。如果他做到了,除去iAd的視圖(或setAlpha = 0)

UPDATE

用戶完成支付(如果您收到SKPaymentTransactionStatePurchased/SKPaymentTransactionStateRestored的那一刻,保存標誌爲NSUserDefaults,讓你將知道用戶是否已經購買了IAP,並相應地顯示/隱藏了iAd。

如果NSUserDefaults中沒有任何內容,表示:(1)用戶沒有購買IAP或(2)用戶已經購買了它但不知何故信息已在NSUserDefaults(例如用戶刪除&重新安裝應用程序)丟失。情況下,正確的方法是要求用戶購買IAP。如果(2)發生,App Store將自動恢復IAP,並且您將收到SKPaymentTransactionStateRestoredSKPaymentTransactionStateRestoredSKPaymentTransactionStatePurchased的代碼應該類似:設置NSUserDefaults標誌。

爲了避免混淆,一些應用程序提供了一個恢復按鈕,它調用[SKPaymentQueue restoreCompletedTransactions]併產生SKPaymentTransactionStateRestored如果一些IAP被用戶買走。如果用戶沒有購買任何IAP,則不做任何事情。

+0

但那是我不真正懂得如何編碼的部分 – user2933653

相關問題