购买时的应用内购买错误(SKPayment)

时间:2014-10-23 12:04:01

标签: ios objective-c xcode

我在我的应用中实施了应用内购买。 这是我的代码:

 #define kRemoveAdsProductIdentifier @"com.company.myApp.inAppPurchase"

-(void)fetchProducts {

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate = (id)self;
[productsRequest start];
}

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

products = response.products;
NSLog(@"Produkte wurden geladen");
}

-(void)request:(SKRequest *)request didFailWithError:(NSError *)error {

NSLog(@"%@",error);
}


-(void)buy:(SKProduct *)product {

SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue]addPayment:payment];
}


-(IBAction)removeAdvertisment:(id)sender {

SKProduct *prod = [products objectAtIndex:0];
[self buy:prod];

}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

for (SKPaymentTransaction *tx in transactions) {

    switch (tx.transactionState) {
        case SKPaymentTransactionStatePurchased:
            [[SKPaymentQueue defaultQueue]finishTransaction:tx];
            bannerImage.alpha = 0.0;
            _adRemoved35.alpha = 1.0;
            _adRemoved4.alpha = 1.0;
            _buttonAd35.alpha = 0.0;
            _buttonAd4.alpha = 0.0;
            [defaults setBool:YES forKey:@"isPro"];
            break;

        case SKPaymentTransactionStateFailed:
            [[SKPaymentQueue defaultQueue]finishTransaction:tx];
            NSLog(@"Error: %@",tx.error);
            break;

        case SKPaymentTransactionStateRestored:
            [[SKPaymentQueue defaultQueue]finishTransaction:tx];
            break;
        default:
            break;
    }
  }
}

在这行代码中出现错误:

SKPayment *payment = [SKPayment paymentWithProduct:product];

控制台:

prod = (ViewController *) 0x14a0dc00
products = (NSArray *) @"0 objects"

有人知道什么是错的吗? 谢谢你的回答。

我使用xCode 6.0.1,iPad 3和iOS 8.0.2

1 个答案:

答案 0 :(得分:0)

您是否检查过SKProductsRequest的响应?

您似乎没有获得有效的产品清单作为回复,因此

  

products = response.products;

设置错误的值 和

  

SKProduct * prod = [products objectAtIndex:0];    [自我购买:prod];

将错误的价值交给买:...