このコードを使用して、アプリ内購入を処理します。
#pragma mark StoreKit Delegate
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
{
// show wait view here
// statusLabel.text = @"Processing...";
}
break;
case SKPaymentTransactionStatePurchased:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock feature 2
// statusLabel.text = @"Done!";
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Complete"
message:@"You have unlocked Feature 2!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
NSError *error = nil;
[SFHFKeychainUtils storeUsername:@"IAPStoreSave" andPassword:@"whatever" forServiceName:kStoredData updateExisting:YES error:&error];
// apply purchase action - hide lock overlay and
[self.lockImage removeFromSuperview];
// do other thing to enable the features
}
break;
case SKPaymentTransactionStateRestored:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
// statusLabel.text = @"";
}
break;
case SKPaymentTransactionStateFailed:
{
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
// statusLabel.text = @"Purchase Error!";
}
break;
default:
{
}
break;
}
}
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
// remove wait view here
// statusLabel.text = @"";
SKProduct *validProduct = nil;
int count = [response.products count];
if (count>0) {
validProduct = [response.products objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.app.ID"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Not Available"
message:@"No products to purchase"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
}
}
-(void)requestDidFinish:(SKRequest *)request
{
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
}
そして、次のように開始します。
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.app.ID"]];
request.delegate = self;
[request start];
itunes connectで、入力した製品IDを使用してアプリ内を設定し、ステータスは「送信準備完了」です。また、「最初のアプリ内購入は、新しいアプリで送信する必要があります。アプリのバージョン。[バージョンの詳細]ページの[アプリ内購入]セクションからそれらを選択し、[バイナリをアップロードする準備ができました]をクリックします。」リクエストを開始すると、アラートNOPRODUCTAVAILABLEが常に表示されます。私は何が間違っているのですか?