Search code examples
ios5in-app-purchasembprogresshud

Having issue updating hud ivar during in-app purchase


I have recently set-up an in-app purchase mechanism in my app. During the purchase i would like to update an hud (i am using the mbprogresshud) according to two kinds of events: i start a purchase and i receive a validation of the purchase. The problem i am facing is that the hud is never updated with the one i want when the purchase is done (custom view):

  1. When i click on the purchase button:
-(IBAction)buyButtonTapped:(id)sender {

  self.hud = [[SCLProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];

    self.hud.labelText = @"Connecting...";
    self.hud.minSize = CGSizeMake(100 , 100);
    [self.hud show:YES];
  ...
}
  1. When i receive a notification that the purchase was successful:
 -(void)productPurchased:(NSNotification *)notification {     
self.hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark_icon.png"]];     
self.hud.mode = SCLProgressHUDModeCustomView;
self.hud.labelText = @"Thanks for your purchase!";        
... 
}

Setting the self.hud.customView property in the last method will trigger a [self setNeedsLayout]; and [self setNeedsDisplay] within the hud class but still i don't observe any change.

Any possible idea of what i am doing wrong here?


Solution

  • As mentioned on the mbprogress hud readme, update of the UI when tasks are performed on the main thread required a slight delay to take effect. What happen in my case is that the hud was a strong property of a popover controller that i dismiss immediately so i didn't get a chance to see the update happening. I now dismiss the controller in the completion block of:

    -(void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(void (^)())completion

    And my code snippet look like like this for the dismiss:

    [_hud showAnimated:YES whileExecutingBlock:^(void){
                                              [self.successPurchaseSoundEffect play];
                                              _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark_icon.png"]];
                                              _hud.mode = SCLProgressHUDModeCustomView;
                                              _hud.labelText = @"Thanks!";
    
                                              // We need to pause the background thread to let the music play and the hud be updated
                                              sleep(1);}
                              completionBlock:^(void){
                                              [self.delegate dismissPurchaseInfoController:self];
    }];