我现在已经尝试了两天时间才能在iOS应用中实现应用购买,并且遇到了同样的错误困扰着我。

每次尝试启动SKProductsRequest对象时,都会收到EXC_BAC_ACCESS错误。

我读过很多人也遇到同样的错误,但是这些解决方案似乎都不适合我。

设置NSZombieEnabled时,出现以下错误:

[AppShopper respondsToSelector:]: message sent to deallocated instance 0x1d9340


这是我的AppShopper.h:

#import <StoreKit/StoreKit.h>

#define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification"


@interface AppShopper : NSObject <SKProductsRequestDelegate>

@property (nonatomic, strong) SKProduct *product;
@property (nonatomic, strong) SKProductsRequest *request;

- (void) requestProductData;

@end


还有我的AppShopper.m:

#import "AppShopper.h"

@implementation AppShopper

#define productId @"XXX.ProductID.XXX"

@synthesize request = _request;
@synthesize product = _product;

- (void) request:(SKRequest *)request didFailWithError:(NSError *)error{
    printf("Error!\n");
    _request = nil;
    _product = nil;
}

- (void) requestDidFinish:(SKRequest *)request {
    printf("Finished request!\n");
}

- (void) requestProductData{
    printf("requestProductData\n");

    NSSet *productIdentifiers = [NSSet setWithObject:productId];

    self.request = [[SKProductsRequest alloc] initWithProductIdentifiers: productIdentifiers];

    self.request.delegate = self;
    [self.request start];

    printf("requestProductData End\n");
}

#pragma mark -
#pragma mark SKProductsRequestDelegate methods

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    printf("productsRequest\n");
    NSArray *products = response.products;

    self.product = [products count] == 1 ? [products objectAtIndex:0] : nil;
    if (self.product)
    {
        NSLog(@"Product title: %@" , self.product.localizedTitle);
        NSLog(@"Product description: %@" , self.product.localizedDescription);
        NSLog(@"Product price: %@" , self.product.price);
        NSLog(@"Product id: %@" , self.product.productIdentifier);
    }

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

    _request = nil;
    _product = nil;

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


@end


我尝试使用以下代码开始应用内购买:

AppShopper *shopper = [[AppShopper alloc] init];
[shopper requestProductData];


我的输出是:

requestProductData
requestProductData End

2012-09-10 19:43:30.210 MyApp[4327:707] *** -[AppShopper respondsToSelector:]: message sent to deallocated instance 0x1d9340


而且,是的,我是:


在物理设备上测试
在沙盒环境中与测试用户
具有适当的配置文件


任何帮助表示赞赏,谢谢。

最佳答案

该错误将位于使您的AppShopper成为对象的任何位置。

例如,

AppShopper *shopper = [AppShopper new];
... setup shopper here ...
[shopper requestProductData];


ARC如何知道您想保留AppShopper?它不会,它将在requestProductData之后立即释放它。然后,当请求返回时,它将尝试调用它的委托方法,该方法不再存在。

尝试将您的AppShopper作为强属性而不是局部变量存储,看看是否有帮助。

10-08 15:25