iphone - 在应用程序购买 iphone 中发送到已释放实例的消息

标签 iphone objective-c cocoa-touch ios4

我已经在我的应用中实现了应用内购买,但我目前面临严重错误,请查看下面的代码

@implementation Credits


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    if ([SKPaymentQueue canMakePayments]) {
        NSLog(@"PARENTAL CONTROL DISABLED");

        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.2sms.twosmsapp.credits.1"]];
        productsRequest.delegate = self;
        [productsRequest start];
    }
    else {
        NSLog(@"PARENTAL CONTROL ENABLED");
    }


    [super viewDidLoad];

}

-(IBAction)purchase100Credits{

    SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.2sms.twosmsapp.credits.1"];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

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

    SKProduct *validProduct = nil;
    int count = [response.products count];
    if(count > 0)
    {
        validProduct = [response.products objectAtIndex:0];
    }
    else {
        NSLog(@"NO PRODUCTS AVAILIABLE");
    }

}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

    for(SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:

                break;

            case SKPaymentTransactionStatePurchased:

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                if (transaction.error.code != SKErrorPaymentCancelled)
                {
                    NSLog(@"AN ERROR ENCOUNTERED");
                }

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        }
    }


}




- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [productsRequest release];
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    [super dealloc];

}

该按钮成功获取了 iTunes 商店的信息,但是当我离开 View 然后返回时,我收到了

-[Credits respondsToSelector:]: message sent to deallocated instance 0x1a8810

这让我很头疼!我相信这是一个内存管理问题,但我是新手,找不到问题:(

最佳答案

我有同样的错误,代码中不正确的一点是当您分配和启动 de products 请求时:

productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet     setWithObject:@"com.2sms.twosmsapp.credits.1"]];
    productsRequest.delegate = self;
    [productsRequest start];

并且在dealloc中只deallocate了productsRequest,在没有真正deallocation之前,productsRequest可以调用delegate,不存在的view -> ERROR

防止此错误的一件事是在发布前将委托(delegate)设置为 nil:

productsRequest.delegate = nil;
[productsRequest release];

这解决了我的问题。

希望这对您有所帮助。

关于iphone - 在应用程序购买 iphone 中发送到已释放实例的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5566395/

相关文章:

iphone - 是否有在 iOS 上使用自定义实时 EQ 播放音乐的框架?

iphone - 在 UIScrollview 中设置框架后 UIButton 没有响应

ios - 无法调用 UIWebView 上的所有功能

ios - 计算 n 边正多边形的顶点

iphone - 如何查看 nib/xib 文件的代码?

c# - 是否可以在 iPhone 和 OS X 上不使用 NIB 文件来创建窗口?

ios - 警告 : "This app will not work with future versions of iOS"

ios - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

objective-c - 将颜色从 colorWithRGBA 转换为 colorWithWhite

cocoa-touch - 如何只允许 180 度自转?