菜鸟教程小白 发表于 2022-12-13 13:48:39

objective-c - 为应用内购买实现一种以上的购买类型


                                            <p><p>这是根据建议使用 Apple 文档编辑的表单。
我用 2 个独立的 IBAction 创建了 2 个独立的按钮。</p>

<pre><code>UIButton *buyCredit1 = ;
    buyCredit1.frame = scrollViewFrame;
    ;
    buyCredit1.tag = 10;
    ;
    [buyCredit1 addTarget:self
                   action:@selector(purchase10credit:)
         forControlEvents:UIControlEventTouchUpInside];

UIButton *buyCredit2 = ;
    buyCredit2.frame = scrollViewFrame;
    ;
    buyCredit2.tag = 30;
    ;
    [buyCredit2 addTarget:self
                   action:@selector(purchase30credit:)
         forControlEvents:UIControlEventTouchUpInside];

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

    [ addPayment:payment];
    [ addTransactionObserver:self];
    [ addPayment:payment];
}
-(IBAction)purchase30credit:(id)sender{
    SKMutablePayment *payment = [ init];
    payment.productIdentifier = @&#34;Bundle.30.credits&#34;;

    [ addPayment:payment];
    [ addTransactionObserver:self];
    [ addPayment:payment];
}
-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = ;
    if (count &gt; 0) {
      validProduct = ;
    }
    else if (!validProduct) {
      NSLog(@&#34;No Products Available&#34;);
    }
}
</code></pre>

<p>这是 Apple 反射(reflect)的应用内购买文档</p>

<pre><code>-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = ;
    if (count &gt; 0) {
      validProduct = ;
    }
    else if (!validProduct) {
      NSLog(@&#34;No Products Available&#34;);
    }
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
      switch (transaction.transactionState)
      {
            case SKPaymentTransactionStatePurchased:
                ;
                break;
            case SKPaymentTransactionStateFailed:
                ;
                break;
            case SKPaymentTransactionStateRestored:
                ;
            default:
                break;
      }
    }
}

- (void) updateCredit: (NSString *)productIdentifier {
    //Adding to plist
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = ;
    NSString *path = ;

    NSMutableArray *currPlist = [ initWithContentsOfFile: path];

    NSString *lastEx = ;
    int lastScore = [ intValue];
    int numberOfTries = [ intValue];
    int totalScore = [ intValue];
    int avg = [ intValue];
    int credit = [ intValue];

    credit += 10;


    NSString *currentCredit = ;
    creditShow.text = currentCredit;

    NSMutableArray *updatePlist = [ init];
    ;
    ];
    ];
    ];
    ];
    ];
    ;
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    // Your application should implement these two methods.
    ;

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


- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{

    [ finishTransaction: transaction];
}


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

<p>在 - (void) updateCredit: (NSString *)productIdentifier 中,我如何创建 2 个单独的信用更新? 1 个积分 += 10(购买 0.99 美分,积分 += 30(1.99 美元)?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>检查 <code>purchase:</code> 中的 (id)sender 并确定按下了哪个按钮(您可以为按钮设置标签属性,例如 tag=1 表示 10 个积分,tag=5 表示50 学分)。然后您可以设置另一个应用内购买:</p>

<pre><code>UIButton *tempButton = (UIButton *)sender;
if (tempButton.tag == 1)
payment.productIdentifier = @&#34;Bundle.10.credits&#34;;
else
payment.productIdentifier = @&#34;Bundle.50.credits&#34;;
</code></pre>

<p>至于更新状态是推荐的方式:</p>

<pre><code>- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
      switch (transaction.transactionState)
      {
            case SKPaymentTransactionStatePurchased:
                ;
                break;
            case SKPaymentTransactionStateFailed:
                ;
                break;
            case SKPaymentTransactionStateRestored:
                ;
            default:
                break;
      }
    }
}


- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    // Your application should implement these two methods.
    ;
    ;

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


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


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

<p>应用内购买的正确实现见<a href="https://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/Introduction/Introduction.html" rel="noreferrer noopener nofollow">Apple official In-App Purchase Programming Guide.</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 为应用内购买实现一种以上的购买类型,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10611514/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10611514/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 为应用内购买实现一种以上的购买类型