2012-03-07 136 views
-2

我有應用程式內購買的應用程序,我完成應用程式內的代碼方面,還有在我的ViewController購買按鈕,當用戶敲擊這個按鈕,它經歷了支付過程中,如果收到付款是廣告獲得成功,我只是需要啓用剛剛買旁邊的按鈕button.i得到付款sucess,如果支付是廣告獲得成功,我需要啓用按鈕報警,我早前禁用。 按鈕點擊應用程式內購買iPhone的SDK

if ([SKPaymentQueue canMakePayments]) { 
        // Yes, In-App Purchase is enabled on this device! 
        // Proceed to fetch available In-App Purchase items. 

        // Replace "Your IAP Product ID" with your actual In-App Purchase Product ID, 
        // fetched from either a remote server or stored locally within your app. 
        SKProductsRequest *prodRequest= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"com.mycompny.myproduct"]]; 
        prodRequest.delegate = self; 
        [prodRequest start]; 
        // Replace "Your IAP Product ID" with your actual In-App Purchase Product ID. 
        SKPayment *paymentRequest = [SKPayment paymentWithProductIdentifier: @"com.mycompny.myproduct"]; 

        // Assign an Observer class to the SKPaymentTransactionObserver, 
        // so that it can monitor the transaction status. 
        [[SKPaymentQueue defaultQueue] addTransactionObserver:inappObserver]; 

        // Request a purchase of the selected item. 
        [[SKPaymentQueue defaultQueue] addPayment:paymentRequest]; 

       } else { 
        // Notify user that In-App Purchase is disabled via button text. 
        [inappButton setTitle:@"In-App Purchase is Disabled" forState:UIControlStateNormal]; 
        inappButton.enabled = NO; 

我需要在上面的代碼,以便在inapppurchaseobserver.m

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { 
    for(SKPaymentTransaction *transaction in transactions) { 
     switch (transaction.transactionState) { 

      case SKPaymentTransactionStatePurchasing: 
       // Item is still in the process of being purchased 
       break; 

      case SKPaymentTransactionStatePurchased: 
       // Item was successfully purchased! 

       // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE --- 
       // The purchased item ID is accessible via 
       // transaction.payment.productIdentifier 

       // After customer has successfully received purchased content, 
       // remove the finished transaction from the payment queue. 
       [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
       break; 

      case SKPaymentTransactionStateRestored: 
       // Verified that user has already paid for this item. 
       // Ideal for restoring item across all devices of this customer. 

       // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE --- 
       // The purchased item ID is accessible via 
       // transaction.payment.productIdentifier 

       // After customer has restored purchased content on this device, 
       // remove the finished transaction from the payment queue. 
       [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
       break; 

      case SKPaymentTransactionStateFailed: 
       // Purchase was either cancelled by user or an error occurred. 

       if (transaction.error.code != SKErrorPaymentCancelled) { 
        // A transaction error occurred, so notify user. 
       } 
       // Finished transactions should be removed from the payment queue. 
       [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
       break; 
     } 
    } 
} 

名爲_btnunlockfeature

按鈕

case SKPaymentTransactionStatePurchased: 
        // Item was successfully purchased! 

        // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE --- 
        // The purchased item ID is accessible via 
        // transaction.payment.productIdentifier 

        // After customer has successfully received purchased content, 
        // remove the finished transaction from the payment queue. 
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

我想我在這裏要編寫啓用的代碼,此代碼在NSobject中不在sam中e viewcontroller.please幫助我做到這一點。

+4

所以,你已經成功地編寫應用程序內購買的代碼和您的應用程序的所有的休息,但你無法弄清楚如何在視圖中啓用按鈕? – 2012-03-07 11:16:33

+0

什麼的問題button.enabled = YES/NO?!感謝上帝,它尚未棄用! – 2012-03-07 13:30:00

+0

@Malek_Jundi你說的正確的事,我知道如何使一個按鈕,但problewm是我只是想notfy的是,如果購買sucess,它要啓用該按鈕,我把nsuserdefault布爾,但我只能當頁面正在重新加載。 – stackiphone 2012-03-07 13:54:27

回答

0
// 
- (void)completeTransaction:(SKPaymentTransaction *)transaction 
{ 
    [self recordTransaction:transaction]; 
    [self provideContent:transaction.payment.productIdentifier]; 
    [self finishTransaction:transaction wasSuccessful:YES]; 

} 


- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful 
{ 
    // remove the transaction from the payment queue. 
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil]; 
    if (wasSuccessful) 
    { 

     // send out a notification that we’ve finished the transaction 
     [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo]; 

      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Content successfully purchased" message:nil delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
      [alert show]; 
      [alert release]; 
      count = 1; 

    } 
    else 
    { 
     // send out a notification for the failed transaction 
     [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo]; 
    } 


} 



- (void)provideContent:(NSString *)productId 
{ 
    if ([productId isEqualToString:kInAppPurchaseProUpgradeProductId]) 
    { 
     // enable the pro features 
     // Save the Value stating user Did purchase Sound Pack 
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     [defaults setBool:YES forKey:@"SoundPack"]; 
     [defaults synchronize]; 
    } 
} 

我不喜歡它this..Storing在NSUserDefaults的 ..和檢索這的ViewController

+0

在哪裏把上面的代碼? – stackiphone 2012-03-07 11:21:28

+0

如果你想只允許一鍵..只是保存在BOOL NSUserDefaults的......而當另一種觀點認爲loads..check BOOL ..如果當前負載的button..otherwise..dont加載它.. – Shubhank 2012-03-07 12:37:37