时间:2010-07-24 09:43:18

标签: iphone crash delegates in-app-purchase storekit

3 个答案:

答案 0 :(得分:51)

一个非常可能的解释是您设置为SKProductRequest对象的委托的对象是否已经被释放。

请求很可能需要几秒钟才能完成,这可能超过了委托对象的生命周期,因此您可能希望确保它能够存留足够长的时间。

答案 1 :(得分:3)

上述答案在技术上是正确的,但并不完整。正如Megastep所说,“您设置为SKProductsRequest代理的对象可能已被解除分配。”因此,您正在向已释放的对象发送消息。现在回答实际答案:

- (void)requestProUpgradeProductData {
    NSSet *productIdentifiers = //Your Product IDs go here

    productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];

    // we will release the request object in the delegate callback
}

#pragma mark -
#pragma mark SKProductRequest Delegate Methods

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse(SKProductsResponse *)response {
self.products = response.products;
//NSLog(@"%i",[products count]);
proUpgradeProduct = [products count] == 4 ? [[products objectAtIndex:0] retain] : nil;
if (proUpgradeProduct)
{
    //Do your stuff here...
}

for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
    //NSLog(@"Invalid product id: %@" , invalidProductId);
}

// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];

[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

所以基本上你可以看到上面你不需要发布productsRequest,因为它已经在委托回调方法中发布了。同样,您不需要调用productsRequest版本或在viewDidUnload / dealloc方法中将其设置为Nil,因为如果在调用回调方法之前关闭视图,则可能导致崩溃。

答案 2 :(得分:0)

我使用像委托这样的对象。所以在这个对象的deinit方法中我删除了委托a并且崩溃已经修复。

private var currentProductRequest: SKProductsRequest?

deinit {
    if let r = currentProductRequest {
        r.delegate = nil
        r.cancel()
        currentProductRequest = nil
    }
}