IAP导致SIGABRT

时间:2011-12-13 04:38:02

标签: ios in-app-purchase

这是我第一次使用应用程序购买,我不确定它是做错了什么。 SKProductsRequest工作正常,但在尝试购买时,该应用程序崩溃 SIGABRT

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  MKStoreObserver *observer = [[MKStoreObserver alloc] init];
  [[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
  return YES;
}

ViewController.m

- (void)viewDidLoad
{
  if ([SKPaymentQueue canMakePayments])
  {
    [self requestProductData];
  }
  else {
 }
 [super viewDidLoad];
}

- (IBAction)buyButton:(id)sender
{
  SKPayment *payment = [SKPayment paymentWithProduct:kMyFeatureIdentifier];
  [[SKPaymentQueue defaultQueue] addPayment:payment];

  //When this button is touched the app crashes
}

- (void)requestProductData
{
   SKProductsRequest *request = [[SKProductsRequest alloc]     initWithProductIdentifiers:kMyFeatureIdentifier]];

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

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
 NSArray *myProduct = response.products;

  if (myProduct.count) {

    SKProduct *thisProduct = [myProduct objectAtIndex:0];


    productTitle.text = [NSString stringWithFormat:@"%@", thisProduct.localizedTitle];

    productPrice.text = [NSString stringWithFormat:@"For only $%@", thisProduct.price];

  }
}

MKStoreObserver.m

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
   for (SKPaymentTransaction *transaction in transactions)
   {   

    switch ( transaction.transactionState ) 

    {

        case SKPaymentTransactionStatePurchased: 

            [ self completeTransaction: transaction ];               

            break;

        case SKPaymentTransactionStateFailed:   

            [ self failedTransaction: transaction ];

            break;

        case SKPaymentTransactionStateRestored:  

            [ self restoreTransaction: transaction ];

            break;

        default:                 

            break;
   }    
  }
}

- (void)completeTransaction: (SKPaymentTransaction *)transaction
{
 [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)failedTransaction: (SKPaymentTransaction *)transaction
{
 if (transaction.error.code != SKErrorPaymentCancelled)
 {
   // Optionally, display an error here.
 }
 [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)restoreTransaction: (SKPaymentTransaction *)transaction
{
  [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

当按下buyButton时,应用程序崩溃

此?

  

GNU gdb 6.3.50-20050815(Apple version gdb-1708)(2011年9月16日星期五06:56:50 UTC)版权所有2004 Free Software Foundation,Inc.GDB   是GNU通用公共许可证所涵盖的免费软件   欢迎更改和/或在某些情况下分发它的副本   条件。输入“show copying”查看条件。有   绝对不保证GDB。输入“show warranty”了解详情。   此GDB配置为“--host = i386-apple-darwin

     

- target = arm-apple-darwin“.tty / dev / ttys000 target remote-mobile /tmp/.XcodeGDBRemote-4242-20切换到remote-macosx协议mem   0x1000 0x3fffffff缓存mem 0x40000000 0xffffffff无mem 0x00000000   0x0fff无[切换到进程7171线程0x1c03] [切换到   进程7171线程0x1c03] sharedlibrary apply-load-rules all   2011-12-12 21:39:11.082 inAppTest [171:707] - [__ NSCFSet   productIdentifier]:无法识别的选择器发送到实例0x134e70   2011-12-12 21:39:11.091 inAppTest [171:707] ***由于终止应用程序   未捕获的异常'NSInvalidArgumentException',原因:' - [__ NSCFSet   productIdentifier]:无法识别的选择器发送到实例0x134e70'

     

***第一次抛出调用堆栈:(0x344298bf 0x346791e5 0x3442cacb 0x3442b945 0x34386680 0x3251b2ed 0x2c9d 0x34383435 0x377ed9eb   0x377ed9a7 0x377ed985 0x377ed6f5 0x377ee02d 0x377ec50f 0x377ebf01   0x377d24ed 0x377d1d2d 0x30c04df3 0x343fd553 0x343fd4f5 0x343fc343   0x3437f4dd 0x3437f3a5 0x30c03fcd 0x37800743 0x2a41 0x29d8)终止   叫抛出异常(gdb)

2 个答案:

答案 0 :(得分:0)

您在哪里定义kMyFeatureIdentifier?如果尚未定义,则可能会导致此错误。

答案 1 :(得分:0)

如果查看崩溃日志,您会看到以下行: - [__ NSCFSet productIdentifier]:无法识别的选择器发送到实例0x134e70 这意味着已经要求某种NSSet对象为名为productIdentifier的属性提供一个值,它显然没有。

问题很可能是paymentWithProduct:方法需要交给SKProduct对象。您似乎给它一个NSSet对象。在它尝试查询它认为是其productIdentifier的SKProduct之前,它没有注意到这一点。我可以看到你在代码中的其他地方使用相同的常量标识符和initWithProductIdentifiers:方法,它们期望一个集合。这可能就是你迷茫的原因。

我建议你将kMyFeatureIdentifier变成一个实际的标识符而不是一个集合,因为这就是它的名字所暗示的,然后用[NSSet setWithObject:kMyFeatureIdentifier]提供initWithProductIdentifiers