stackiphone
stackiphone

Reputation: 1245

How to pass messages from one class to another through NSNotification

Ha-ii ,am doing an in-app purchase in my application,i need to disable a button when the in-app content purchase is success,so i put the NSuserdefault to my code to identify weather its success or not,but NSUserDefault works only when i refresh the page,i didnt get the button enabled when the content success message is come ,i get enabled when i go back to another page and came back,my need is to pass instance message to the viewcontroller through notification.my code is

- (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];
                 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Transsucess"];
                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.
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Transsucess"];
                }
                // Finished transactions should be removed from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: 

transaction];
                break;
        }
    }
}

in my anothervierwcontroller i want to pass the Notification value insted of Nsuserdefault value. How to do this.please help me.

Upvotes: 0

Views: 259

Answers (1)

Shubhank
Shubhank

Reputation: 21805

in your first view vieDidLoad add the line ..

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

( do remove the observer in dealloc)

then when you have successful purchase in your question code.. Post the notification

[[NSNotificationCenter defaultCenter] postNotificationName:@"Purchased"  object:nil userInfo:nil];

You can pass an object..but i believe you should store the purchase transaction detail in NSUserdefautls..since it will be needed in the next runs of the app..

When you post the notification ContentPurchased method is called in first view ( you have to add that in the view controller)

Upvotes: 2

Related Questions