iPhone - SKProductsRequest 和 "message sent to deallocated instance"

标签 iphone

我在实现 InAppPurchase 时遇到了麻烦。我的购买实现是在模态视图 Controller (AppUpgradeViewController)中实现的,我从另一个模态视图中呈现它。我这样做是这样的:

AppUpgradeViewController * appUpgradeViewController = [[AppUpgradeViewController alloc] init];
appUpgradeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
appUpgradeViewController.delegate = self;
[self presentModalViewController:appUpgradeViewController animated:YES];
[appUpgradeViewController release];

然后,在我的升级 View 中,我执行以下操作:

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
NSSet *productIdentifiers = [NSSet setWithObject:kInAppPurchaseProUpgradeProductId];
self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
self.productsRequest.delegate = self;
[productsRequest start];

然后我就实现了

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

我在哪里:

[self.productsRequest release];

然后我还有其他需要的方法。

问题是当我显示模态并快速关闭它时,几秒钟后我在控制台上看到以下内容(我打开了 NSZombieEnabled):

*** -[AppUpgradeViewController respondsToSelector:]: message sent to deallocated instance 0x2e91f0

我认为这与该产品请求有关,但我不知道如何调试或修复它。似乎请求的答案在它被解雇(并释放)后到达该 Controller ,但我不知道如何防止它在解雇/释放后接收消息。 感谢您的帮助!

最佳答案

我也遇到了同样的问题。不是使用模态视图,而是使用导航 Controller 堆栈上的推送 View 。当我在通过 SKProductsRequest 加载产品信息之前快速导航回来时,它还会向我抛出一个 消息发送到已释放实例 异常。我通过在 SKProductsRequest 对象上调用 cancel 方法(请参阅 reference )解决了这个问题。

除此之外,我还将委托(delegate)设置为nil

这是我的产品请求:

NSSet *productIdentifiers = [NSSet setWithObject:@"MY_IDENTIFIER"];
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];

这就是我在 dealloc 方法中调用的方法来取消它。

productsRequest.delegate = nil;
[productsRequest cancel];
productsRequest = nil;

我还从 SKPaymentQueue 中删除了观察者,如 this answer for another question 中所述。 .

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

关于iPhone - SKProductsRequest 和 "message sent to deallocated instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4782214/

相关文章:

iphone - 创建twitter应用时"Website: Not a valid URL format"

ios - 使用现有公钥的 RSA 加密

iphone - 通过 NSRegularExpression 获取 Javascript 函数之间的特定值?

iphone - NSSound 在 iPhone 上未声明?

iphone - 当有一个时为零值

iphone - 我们可以直接访问核心数据的SQLite数据库吗

ios - UITableViewCell 中的 UITextField 唯一性

iphone - 将 Static-TableView-Cells 添加到 UIViewcontroller 的最佳方法?

ios - 通过 iPhone 音频插孔读取数据

iphone - 如何知道 StatusBar 大小何时发生变化 (iPhone)