2011-12-21 18 views
6

Tôi đang làm trong mua ứng dụng trên hai bộ điều khiển của tôi nói view1, view2.Sản phẩm tiêu hao có tác dụng lạ và yêu cầu trùng lặp .. Nguyên nhân có thể là gì?

khi tôi mua sản phẩm tiêu hao trong chế độ xem1 mọi thứ hoạt động tốt.

nhưng khi tôi mua một sản phẩm tiêu hao khác trên view2 sau khi giao dịch hoàn tất, nó sẽ kích hoạt yêu cầu cũ tôi đã thực hiện trên giao diện 1 chứ không phải yêu cầu view2 mới. Và ngược lại

Ngoài ra nếu tôi rebuy sản phẩm tương tự từ cùng một bộ điều khiển sau khi rebuying nó làm cho các yêu cầu mà tôi thực hiện trước khi nó ...

Example: 

Controller1: 

product1 > buy > someaction(say hit to the server with some params like "name= ABC") 
user enters name and is dynamic 


Again, 

if I rebuy it 
product1 > buy > someaction(say hit to the server with some params like "name= XYZ") 

Now when product is bought it hits the server with old params "name=ABC" and not "name=XYZ" what user enters again.. 

sản phẩm

**heres the code m using in my IAPhelper** 

     - (id)initWithProductIdentifiers:(NSSet *)productIdentifiers { 
     if ((self = [super init])) { 

      // Store product identifiers 
      _productIdentifiers = [productIdentifiers retain]; 

      // Check for previously purchased products 
      NSMutableSet * purchasedProducts = [NSMutableSet set]; 
      for (NSString * productIdentifier in _productIdentifiers) { 
       BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier]; 
       if (productPurchased) { 
        [purchasedProducts addObject:productIdentifier]; 
        NSLog(@"Previously purchased: %@", productIdentifier); 
       } 
       NSLog(@"Not purchased: %@", productIdentifier); 
      } 
      self.purchasedProducts = purchasedProducts; 

     } 
     return self; 
    } 

    - (void)requestProducts { 

     self.request = [[[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers] autorelease]; 
     _request.delegate = self; 
     [_request start]; 

    } 

    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { 

     NSLog(@"Received products results..."); 
     self.products = response.products; 
     self.request = nil;  

     [[NSNotificationCenter defaultCenter] postNotificationName:kProductsLoadedNotification object:_products];  
    } 

    - (void)recordTransaction:(SKPaymentTransaction *)transaction {  
     // TODO: Record the transaction on the server side...  
    } 

    - (void)provideContent:(NSString *)productIdentifier { 

     NSLog(@"Toggling flag for: %@", productIdentifier); 
     [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:productIdentifier]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
     // [_purchasedProducts addObject:productIdentifier]; 

     [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchasedNotification object:productIdentifier]; 

    } 

    - (void)completeTransaction:(SKPaymentTransaction *)transaction { 

     NSLog(@"completeTransaction..."); 

     [self recordTransaction: transaction]; 
     [self provideContent: transaction.payment.productIdentifier]; 
     [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

    } 

    - (void)restoreTransaction:(SKPaymentTransaction *)transaction { 

     NSLog(@"restoreTransaction..."); 

     [self recordTransaction: transaction]; 
     [self provideContent: transaction.originalTransaction.payment.productIdentifier]; 
     [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

    } 

    - (void)failedTransaction:(SKPaymentTransaction *)transaction { 

     if (transaction.error.code != SKErrorPaymentCancelled) 
     { 
      NSLog(@"Transaction error: %@", transaction.error.localizedDescription); 
     } 

     [[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchaseFailedNotification object:transaction]; 

     [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

    } 

    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
    { 
     for (SKPaymentTransaction *transaction in transactions) 
     { 
      switch (transaction.transactionState) 
      { 
       case SKPaymentTransactionStatePurchased: 
        [self completeTransaction:transaction]; 
        break; 
       case SKPaymentTransactionStateFailed: 
        [self failedTransaction:transaction]; 
        break; 
       case SKPaymentTransactionStateRestored: 
        [self restoreTransaction:transaction]; 
       default: 
        break; 
      } 
     } 
    } 

    - (void)buyProductIdentifier:(NSString *)productIdentifier { 

     NSLog(@"Buying in IAPHelper %@...", productIdentifier); 

     SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier]; 
     [[SKPaymentQueue defaultQueue] addPayment:payment]; 

    } 

    - (void)dealloc 
    { 
     [_productIdentifiers release]; 
     _productIdentifiers = nil; 
     [_products release]; 
     _products = nil; 
     [_purchasedProducts release]; 
     _purchasedProducts = nil; 
     [_request release]; 
     _request = nil; 
     [super dealloc]; 
    } 

    @end 


    **heres the code m using in my view1controller** 


     -(void)viewDidAppear:(BOOL)animated 
    { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:kProductPurchasedNotification object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(productPurchaseFailed:) name:kProductPurchaseFailedNotification object: nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productsLoaded:) name:kProductsLoadedNotification object:nil]; 

     Reachability *reach = [Reachability reachabilityForInternetConnection]; 
     NetworkStatus netStatus = [reach currentReachabilityStatus];  
     if (netStatus == NotReachable) {   
      NSLog(@"No internet connection!");   
     } else {   
      if ([InAppRageIAPHelper sharedHelper].products == nil) { 

       [[InAppRageIAPHelper sharedHelper] requestProducts]; 
       //   self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; 
       _hud.labelText = @"Loading comics..."; 
       //   [self performSelector:@selector(timeout:) withObject:nil afterDelay:30.0]; 

      }   
     } 
    } 
    - (void)productPurchased:(NSNotification *)notification { 


     [self removeLoader]; 
     [NSObject cancelPreviousPerformRequestsWithTarget:self]; 

     NSString *productIdentifier = (NSString *) notification.object; 
     NSLog(@"Purchased: %@", productIdentifier); 

     NSString *inaapstatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"inaapstatus"]; 

     if ([inaapstatus isEqualToString:@"addabeep"]) { 


      NSUserDefaults *inaapstatus = [NSUserDefaults standardUserDefaults]; 
      [inaapstatus setValue:@"reset" forKey:@"inaapstatus"]; 
      [[NSUserDefaults standardUserDefaults]synchronize]; 
      if ([productIdentifier isEqualToString:@"com.beepbXXXXXXXX"]) { 
       duration = @"30"; 


       NSUserDefaults *purchased = [NSUserDefaults standardUserDefaults]; 
       [purchased setValue:@"YES" forKey:@"purchased"]; 
       [[NSUserDefaults standardUserDefaults] synchronize]; 
       [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(purchased:) userInfo:nil repeats:NO]; 

      } 
      else { 
       duration = @"7"; 
       NSUserDefaults *purchased = [NSUserDefaults standardUserDefaults]; 
       [purchased setValue:@"YES" forKey:@"purchased"]; 
       [[NSUserDefaults standardUserDefaults] synchronize]; 
       [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(purchased:) userInfo:nil repeats:NO]; 

      } 
     } 
     else 
     { 

     }  
    //  [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(makeMyBeeps1Request:) userInfo:nil repeats:NO]; 

    } 
    -(void)purchased:(NSTimer *)timer 
    { 

     NSString *purchased =[[NSUserDefaults standardUserDefaults] objectForKey:@"purchased" ]; 
     if ([purchased isEqualToString:@"YES"]) { 
      [self doLogin]; 
      NSUserDefaults *purchased1 = [NSUserDefaults standardUserDefaults]; 
      [purchased1 setValue:@"NO" forKey:@"purchased"]; 
      [[NSUserDefaults standardUserDefaults] synchronize]; 
     } 

    } 
    - (void)productPurchaseFailed:(NSNotification *)notification { 
     [self removeLoader]; 
     [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
     // [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES]; 

     SKPaymentTransaction * transaction = (SKPaymentTransaction *) notification.object;  
     if (transaction.error.code != SKErrorPaymentCancelled) {  
      UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Oops error occcured!" 
                  message:transaction.error.localizedDescription 
                  delegate:nil 
                cancelButtonTitle:nil 
                otherButtonTitles:@"OK", nil] autorelease]; 

      [alert show]; 
      //[alert release]; 
     } 

    } 
    - (void)dismissHUD:(id)arg { 

     NSLog(@"dismissHUD"); 
     [self removeLoader]; 
    } 

    - (void)productsLoaded:(NSNotification *)notification { 

     [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
     // [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES]; 

     [self removeLoader]; 

    } 

    - (void)timeout:(id)arg { 


     UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Timeout!" 
                 message:@"Please try again later." 

                 delegate:nil 
               cancelButtonTitle:nil 
               otherButtonTitles:@"OK", nil] autorelease]; 

     [alert show]; 
     // [alert release]; 


     //[self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:3.0]; 

    } 
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
    - (void)viewDidLoad { 

     //self.hud = nil; 
     //  self.table.hidden = TRUE; 
     inappObserver = [[InAppPurchaseObserver alloc] init]; 

     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 setWithObjects:@"com.beepccccceek1",@"com.beepbcccccnth1", nil ]]; 
      prodRequest.delegate = self; 
      [prodRequest start]; 


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



     [super viewDidLoad]; 
     [self initializeView]; 

    } 




**view2 controller** 

#pragma mark - View lifecycle 
-(void)viewDidAppear:(BOOL)animated 
{ 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:kProductPurchasedNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(productPurchaseFailed:) name:kProductPurchaseFailedNotification object: nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productsLoaded:) name:kProductsLoadedNotification object:nil]; 
    Reachability *reach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [reach currentReachabilityStatus];  
    if (netStatus == NotReachable) {   
     NSLog(@"No internet connection!");   
    } else {   
     if ([InAppRageIAPHelper sharedHelper].products == nil) { 

      [[InAppRageIAPHelper sharedHelper] requestProducts]; 
      //   self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; 
         [self performSelector:@selector(timeout:) withObject:nil afterDelay:30.0]; 

     }   
    } 

} 
- (void)viewDidLoad 
{ 
    inappObserver = [[InAppPurchaseObserver alloc] init]; 

    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 setWithObjects:@"com.beepbccccecc1",@"com.beepcccp.month1",@"com.beeccccep.free", nil ]]; 
     prodRequest.delegate = self; 
     [prodRequest start]; 

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

    [super viewDidLoad]; 
    [self initializeView]; 
    [[UIApplication sharedApplication] setStatusBarHidden:NO]; 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    self.mTableView = nil; 
    self.numberofbeeps = nil; 
    self.segmentedControl = nil; 

} 
- (void)productPurchased:(NSNotification *)notification { 


    [self removeLoader]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 

    NSString *productIdentifier = (NSString *) notification.object; 
    NSLog(@"Purchased: %@", productIdentifier); 

    NSString *inaapstatus =[[NSUserDefaults standardUserDefaults] objectForKey:@"inaapstatus"]; 

    if ([inaapstatus isEqualToString:@"expired"]) { 


     NSUserDefaults *inaapstatus = [NSUserDefaults standardUserDefaults]; 
     [inaapstatus setValue:@"reset" forKey:@"inaapstatus"]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
    if ([productIdentifier isEqualToString:@"com.beepbccccnth1"]) { 
     duration = @"30"; 


     NSUserDefaults *purchased = [NSUserDefaults standardUserDefaults]; 
     [purchased setValue:@"YES" forKey:@"purchased"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
     [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(purchased:) userInfo:nil repeats:NO]; 

    } 
    else { 
     duration = @"7"; 
     NSUserDefaults *purchased = [NSUserDefaults standardUserDefaults]; 
     [purchased setValue:@"YES" forKey:@"purchased"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
     [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(purchased:) userInfo:nil repeats:NO]; 

    } 
    } 
    else 
    { 

    } 
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(makeMyBeeps1Request:) userInfo:nil repeats:NO]; 


} 

-(void)purchased:(NSTimer *)timer 
{ 
NSString *purchased =[[NSUserDefaults standardUserDefaults] objectForKey:@"purchased" ]; 
    if ([purchased isEqualToString:@"YES"]) { 

     [self durationRequest]; 
     NSUserDefaults *purchased = [NSUserDefaults standardUserDefaults]; 
     [purchased setValue:@"NO" forKey:@"purchased"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 

} 
- (void)productPurchaseFailed:(NSNotification *)notification { 
    [self removeLoader]; 
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    // [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES]; 

    SKPaymentTransaction * transaction = (SKPaymentTransaction *) notification.object;  
    if (transaction.error.code != SKErrorPaymentCancelled) {  
     UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Oops error occcured!" 
                 message:transaction.error.localizedDescription 
                 delegate:nil 
               cancelButtonTitle:nil 
               otherButtonTitles:@"OK", nil] autorelease]; 

     [alert show]; 
     //[alert release]; 
    } 

} 
- (void)dismissHUD:(id)arg { 

    NSLog(@"dismissHUD"); 
    [self removeLoader]; 
} 

- (void)productsLoaded:(NSNotification *)notification { 

    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
    // [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES]; 

    [self removeLoader]; 

} 

- (void)timeout:(id)arg { 


    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Timeout!" 
                message:@"Please try again later." 

                delegate:nil 
              cancelButtonTitle:nil 
              otherButtonTitles:@"OK", nil] autorelease]; 

    [alert show]; 
    // [alert release]; 


    //[self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:3.0]; 

} 

Any help is appreciated thnx a lot guys

+0

nhận dạng sản phẩm được sử dụng một cách chính xác nhưng không được nêu ngay tại đây. – iPhoneDev

Trả lời

1

Tôi không khuyên bạn nên có nhiều đại diện mua hàng đang hoạt động cùng một lúc. Cố gắng hủy kích hoạt đại biểu đầu tiên trong view1 sau khi tất cả quá trình thành công và sau đó thực hiện giao dịch mua ở chế độ xem2, xem điều đó có xảy ra lần nữa không

+0

tôi đang sử dụng chúng tại các sự kiện khác nhau ... thnx – iPhoneDev

0

Rất tiếc, tôi không thể có thời gian để cập nhật câu trả lời của mình.

tôi giải quyết nó bằng removingObserver của thông báo được đăng chỉ sau khi yêu cầu thông báo hoàn thành ...

Hy vọng nó sẽ giúp người ..

Các vấn đề liên quan