2012-03-07 127 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