gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:03:53 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com