gpt4 book ai didi

objective-c - 在应用内购买自动引用计算 : SKProductsRequestDelegate crashes

转载 作者:行者123 更新时间:2023-11-28 22:49:15 24 4
gpt4 key购买 nike

我这两天一直在尝试在 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. 具有适当的配置文件

感谢任何帮助,谢谢。

最佳答案

错误将出现在使您的 AppShopper 成为对象的任何对象中。

例如,

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

ARC 如何知道您想要保留您的 AppShopper?它不会,它将在 requestProductData 之后立即发布。然后,当请求返回时,它将尝试调用它的委托(delegate)方法,这些方法将不再存在。

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

关于objective-c - 在应用内购买自动引用计算 : SKProductsRequestDelegate crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com