Realinstomp
Realinstomp

Reputation: 532

IAP needs to refresh View Controller view

I have ViewController1 and ViewController2.

If you go from ViewController1 to ViewController2, on ViewController2 you can buy an IAP that will show a button on ViewController1.

VC1 --> VC2, buy --> VC1 (should show button that purchase unlocks)

The problem I'm having is that the button is not showing immediately on ViewController1 as soon as I go back from ViewController2.

It only works once I've gone back to ViewController1 then go forward to ViewController2 again then go back to ViewController1.

VC1 --> VC2, buy --> VC1 (button 3 still hidden) --> VC2 --> VC1 (button three showing)

I'm assuming that refreshes the view somehow, so I'm trying to figure out what can refresh it immediately when I go back to ViewController1, instead of having to go back and forth to refresh it.

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"Purchased3"]) {
        buttonThree.hidden = TRUE;
        labelThree.hidden = TRUE;
        NSLog(@"Trying to talk to VC2, hides button/label 3 because there's not purchases");

    } else {
        //buttonThree.hidden = TRUE;
        buttonThree.hidden = FALSE;
        labelThree.hidden = FALSE;
        NSLog(@"Trying to talk to VC2, doesn't hide button/label 3 because there has been a purchase");
    }

Thanks for the help, will post more code if needed, or clarify if needed!

VC1 --> VC2, buy --> VC1 (button 3 still hidden) --> VC2 --> VC1 (button three showing)

Upvotes: 0

Views: 117

Answers (2)

ppalancica
ppalancica

Reputation: 4277

The problem may be that storing the value in NSUserDefaults takes some time, and may not be set when you go back to the ViewController1.

You can solve the issues in 3 ways:

1) When the IAP product is successfully purchased use the [NSNotificationCenter defaultCenter] API to notify the ViewController1 that it should update itself.

2) You can set a global int property in AppDelegate.h, so you can easily see the newest value instantly (even if it not set in NSUserDefaults yet).

3) Use the Delegation pattern.

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

One option is to use NSNotificationCenter by adding your UIViewController as observer as follow in viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateView)
                                             name:@"IAPUpdate"
                                           object:nil];

and add that method to view controller

-(void)updateView{
    // add your updating view stuff here
}

Then in your UIViewController where you make a purchase, after purchase, tell the first view controller using notification like this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"IAPUpdate" object:self];

Upvotes: 1

Related Questions