iphone - 如何通过 NSNotification 将消息从一个类传递到另一个类

标签 iphone ios nsnotifications

Ha-ii,我正在我的应用程序中进行应用内购买,当应用内内容购买成功时我需要禁用一个按钮,所以我将 NSuserdefault 放入我的代码中以确定是否成功,但是NSUserDefault仅在我刷新页面时有效,当内容成功消息出现时我没有启用按钮,当我返回另一个页面并返回时我被启用,我需要通过通知将实例消息传递给viewcontroller .我的代码是

- (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;
        }
    }
}

在我的 anothervierwcontroller 中,我想传递 Nsuserdefault 值的 Notification 值。 如何做到这一点。请帮助我。

最佳答案

在您的第一个 View vieDidLoad 中添加行 ..

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

(在 dealloc 中移除观察者)

然后当您在问题代码中购买成功时.. 发布通知

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

您可以传递对象..但我相信您应该将购买交易详细信息存储在NSUserdefautls..因为下次运行应用程序时需要它..

当您发布时,通知 ContentPurchased 方法在第一个 View 中被调用(您必须在 View Controller 中添加它)

关于iphone - 如何通过 NSNotification 将消息从一个类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9613407/

相关文章:

iphone - NSNotification 问题

android - 有没有办法检查安装我的应用程序的用户是否真的安装了我的应用程序?

ios - 将带有可见导航栏的 View Controller 弹出到带有隐藏导航栏的 View Controller 时,带有 interactivePopGestureRecognizer 的黑色区域

ios - 如何访问父 View Controller ?

ios - 通过 SafariViewController 安装 MDM 配置文件

ios - NSNotification 和控制流

ios - 添加观察在 Swift 4 中不起作用

iphone - "Cocoa Touch Application"来自 Xcode 3.1.1 的模板

ios - 从侧面菜单中选择项目时打开特定的 ViewController?

ios - 无论枚举大小写如何,如何快速获得枚举的关联值