0

我现在已经尝试了两天在 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

而且,是的,我是:

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

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

4

2 回答 2

2

该错误将出现在使您的AppShopper.

例如,

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

ARC 是如何知道你想留在身边的AppShopper?它没有,它会在之后立即释放它requestProductData。然后,当请求返回时,它会尝试调用它的委托方法,这些委托方法将不再存在。

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

于 2012-09-10T12:20:19.357 回答
0

首先,将这些函数添加到

在 AppShopper.m 中

//Checks if the device or user can make purchases
- (BOOL)canMakePurchases
{
    return [SKPaymentQueue canMakePayments];
}

您调用的文件,以这种方式启动inApp购买

AppShopper *shopper = [[AppShopper new];
if([shopper canMakePurchases])
{
    [shopper requestProductData];
}
于 2012-09-10T12:29:51.737 回答