2

これは、Apple のドキュメントを使用してアドバイスに従って編集されたフォームです。2 つの個別の IBAction を持つ 2 つの個別のボタンを作成しました。

UIButton *buyCredit1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buyCredit1.frame = scrollViewFrame;
    [buyCredit1 setTitle:@"A bundle of 10 credits - 99¢" forState:UIControlStateNormal];
    buyCredit1.tag = 10;
    [scrollView addSubview:buyCredit1];
    [buyCredit1 addTarget:self
                   action:@selector(purchase10credit:)
         forControlEvents:UIControlEventTouchUpInside];

UIButton *buyCredit2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buyCredit2.frame = scrollViewFrame;
    [buyCredit2 setTitle:@"A bundle of 30 credits - 1.99¢" forState:UIControlStateNormal];
    buyCredit2.tag = 30;
    [scrollView addSubview:buyCredit2];
    [buyCredit2 addTarget:self
                   action:@selector(purchase30credit:)
         forControlEvents:UIControlEventTouchUpInside];

-(IBAction)purchase10credit:(id)sender{
    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = @"Bundle.10.credits";

    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(IBAction)purchase30credit:(id)sender{
    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = @"Bundle.30.credits";

    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if (count > 0) {
        validProduct = [response.products objectAtIndex:0];
    }
    else if (!validProduct) {
        NSLog(@"No Products Available");
    }
}

これは、アプリ内購入について Apple が反映したドキュメントです。

-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if (count > 0) {
        validProduct = [response.products objectAtIndex:0];
    }
    else if (!validProduct) {
        NSLog(@"No Products Available");
    }
}
- (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];
            default:
                break;
        }
    }
}

- (void) updateCredit: (NSString *)productIdentifier { 
    //Adding to plist
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; 

    NSMutableArray *currPlist = [[NSMutableArray alloc] initWithContentsOfFile: path];

    NSString *lastEx = [currPlist objectAtIndex:0];
    int lastScore = [[currPlist objectAtIndex:1] intValue];
    int numberOfTries = [[currPlist objectAtIndex:2] intValue];
    int totalScore = [[currPlist objectAtIndex:3] intValue];
    int avg = [[currPlist objectAtIndex:4] intValue];
    int credit = [[currPlist objectAtIndex:5] intValue];

    credit += 10;


    NSString *currentCredit = [NSString stringWithFormat:@"%d credits",credit];
    creditShow.text = currentCredit;

    NSMutableArray *updatePlist = [[NSMutableArray alloc] init];
    [updatePlist addObject:lastEx];
    [updatePlist addObject:[NSNumber numberWithInt:lastScore]];
    [updatePlist addObject:[NSNumber numberWithInt:numberOfTries]];
    [updatePlist addObject:[NSNumber numberWithInt:totalScore]];
    [updatePlist addObject:[NSNumber numberWithInt:avg]];
    [updatePlist addObject:[NSNumber numberWithInt:credit]];
    [updatePlist writeToFile: path atomically:YES]; 
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    // Your application should implement these two methods.
    [self updateCredit:transaction.payment.productIdentifier];

    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void) restoreTransaction: (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) updateCredit: (NSString *)productIdentifier で、2 つの個別のクレジット更新を作成するにはどうすればよいですか? 1 クレジット += 10 (0.99¢ の購入とクレジット += 30 ($1.99 の場合)?

4

1 に答える 1

3

(id)sender を確認しpurchase:、どのボタンが押されたかを判断します (ボタンのタグ プロパティを設定できます。たとえば、10 クレジットの場合は tag=1、50 クレジットの場合は tag=5 です)。次に、別のアプリ内購入を設定できます。

UIButton *tempButton = (UIButton *)sender;
if (tempButton.tag == 1)
payment.productIdentifier = @"Bundle.10.credits";
else
payment.productIdentifier = @"Bundle.50.credits";

推奨される方法である状態の更新に関しては:

- (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];
            default:
                break;
        }
    }
}


- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    // Your application should implement these two methods.
    [self recordTransaction:transaction];
    [self provideContent:transaction.payment.productIdentifier];

    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    [self recordTransaction: transaction];
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


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

アプリ内購入の正しい認識については、Apple の公式アプリ内購入プログラミング ガイドを参照してください。

于 2012-05-16T03:23:20.037 に答える