我现在已经尝试了两天在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
而且,我是:
感谢任何帮助,谢谢。
答案 0 :(得分:2)
该错误将出现在您AppShopper
的任何对象中。
例如,
AppShopper *shopper = [AppShopper new];
... setup shopper here ...
[shopper requestProductData];
ARC如何知道您希望保留AppShopper
?它没有,它将在requestProductData
之后立即释放它。然后,当请求返回时,它将尝试调用它的委托方法,这些方法不再存在。
尝试将AppShopper
存储为强属性而非本地变量,看看是否有帮助。
答案 1 :(得分: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];
}