产品报出的log:-[__NSCFConstantString

开发了一个交易应用,测试了一下,使用的产品名如:#define PRODUCT_IDENTIFIER @"com.companyname.appname.P1"

在模拟器运行之后,出现的log:

YES u can make payments2013-04-22 11:37:44.809 appname[2382:c07] -[__NSCFConstantString productIdentifier]: unrecognized selector sent to instance 0x1ac04

代码:

-(void)checkForPayments{
        if ([SKPaymentQueue canMakePayments])
        {
            printf("\n YES u can make payments");
   //   SKPayment *myPayment = [SKPayment paymentWithProductIdentifier:PRODUCT_IDENTIFIER];
        SKPayment *myPayment = [SKPayment paymentWithProduct:PRODUCT_IDENTIFIER];
      [[SKPaymentQueue defaultQueue] addPayment:myPayment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];              
    }

ChatGPT尝试为您解答,仅供参考
这个错误的原因是,你在 checkForPayments 方法中调用了 paymentWithProduct 方法,但是你传递给它的参数是一个字符串(即 PRODUCT_IDENTIFIER 宏定义的值),而 paymentWithProduct 方法需要的是一个 SKProduct 对象。


为了解决这个问题,你需要在调用 paymentWithProduct 方法之前,先使用 SKPaymentQueue 的 productsRequest 方法来请求你的产品信息,并在收到响应后获取对应的 SKProduct 对象。然后你就可以使用这个 SKProduct 对象来创建 SKPayment 对象了。

例如,你可以这样做:

- (void)checkForPayments {
    if ([SKPaymentQueue canMakePayments]) {
        NSSet *productIdentifiers = [NSSet setWithObject:PRODUCT_IDENTIFIER];
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        request.delegate = self;
        [request start];
    }
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    SKProduct *product = response.products.firstObject;
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}

在这里,你需要实现 SKProductsRequestDelegate 协议,并在收到产品信息响应后调用 paymentWithProduct 方法来创建 SKPayment 对象。