1

我正在尝试将 Beeblex 的新应用内购买验证添加到我的应用中,但是我正在努力从块内传递返回值。

这是我现在拥有的代码,如您所见,我设置了一个 BOOL 值,然后在验证块中设置了 BOOL 并在最后返回它。但是在块完成之前调用最后的返回,所以我需要从块内返回 BOOL?

- (BOOL)verifyTransaction:(SKPaymentTransaction *)transaction
{
    if (![BBXIAPTransaction canValidateTransactions]) {
        return YES; // There is no connectivity to reach the server
    }

    BOOL __block toReturn = YES;
    BBXIAPTransaction *bbxTransaction = [[BBXIAPTransaction alloc] initWithTransaction:transaction];
    bbxTransaction.useSandbox = YES;
    [bbxTransaction validateWithCompletionBlock:^(NSError *error) {
        if (bbxTransaction.transactionVerified) {
            if (bbxTransaction.transactionIsDuplicate) {
                // The transaction is valid, but duplicate - it has already been sent to Beeblex in the past.
                NSLog(@"Transaction is a duplicate!");
                [FlurryAnalytics logEvent:@"Transaction duplicate!"];
                toReturn = NO;
            } else {
                // The transaction has been successfully validated and is unique
                NSLog(@"Transaction valid data:%@",bbxTransaction.validatedTransactionData);
                [FlurryAnalytics logEvent:@"Transaction verified"];
                toReturn = YES;
            }
        } else {
            // Check whether this is a validation error, or if something went wrong with Beeblex
            if (bbxTransaction.hasConfigurationError || bbxTransaction.hasServerError || bbxTransaction.hasClientError) {
                // The error was not caused by a problem with the data, but is most likely due to some transient networking issues
                NSLog(@"Transaction error caused by network, not data");
                [FlurryAnalytics logEvent:@"Transaction network error"];
                toReturn = YES;
            } else {
                // The transaction supplied to the validation service was not valid according to Apple
                NSLog(@"Transaction not valid according to Apple");
                [FlurryAnalytics logEvent:@"Transaction invalid!!"];
                toReturn = NO;
            }
        }
    }];

    NSLog(@"toReturn: %@",toReturn ? @"Yes" : @"No");
    return toReturn;
}

如果我只是放在return = NO;块内,我会收到不兼容的块指针类型的编译器警告,并且控制可能会到达非空块的末尾。

4

2 回答 2

4

Beeblex 开发人员在这里。-validateWithCompletionBlock 中的代码:异步执行(在后台,它需要与我们的服务器通信,因此在我们等待互联网执行它的操作时完全阻止您的应用程序是没有意义的)。

因此,您需要重新考虑验证收据的方法。现在你,一般的工作流程是:

  1. 完成购买
  2. 呼叫 Beeblex
  3. 等待回复
  4. 检查布尔值

但是#3 会立即返回,所以这永远不会奏效。你应该考虑做这样的事情:

  1. 完成购买
  2. 显示“请稍候……”视图或类似的内容,告知用户您正在解锁他们购买的任何商品。
  3. 呼叫 Beeblex
  4. 在块内,确定验证是否成功,然后从那里解锁内容。
  5. 闲置直到被块调用

这是一个快速而肮脏的例子。我没有编译它,所以它可能有一些错误,但它应该为您提供预期使用模式背后的想法。

- (void) showWaitView {
    // Display a wait view
}


- (void) hideWaitView {
    // Hide the wait view
}


- (void) completeValidationWithValidateReceiptData:(NSDictionary *) receipt isRecoverableError(BOOL) isRecoverableError {
    [self hideWaitView];

    if (receipt) {
        // Unlock the content, tell the user
    } else {
        if (isRecoverableError) {
            // Probably a network error of some kind. Tell user they need to be connected,
            // and ask them to do it again.
        } else {
            // Keep the content locked, tell the user something went wrong
        }
    }
}

- (void) validateReceipt:(SKPaymentTransaction *) transaction {
    if (![BBXIAPTransaction canValidateTransactions]) {
        [self completeValidationWithValidateReceiptData:Nil isRecoverableError:YES];
        return;
    }

    BBXIAPTransaction *bbxTransaction = [[BBXIAPTransaction alloc] initWithTransaction:transaction];
    bbxTransaction.useSandbox = YES;

    [bbxTransaction validateWithCompletionBlock:^(NSError *error) {
        if (bbxTransaction.transactionVerified) {
            if (bbxTransaction.transactionIsDuplicate) {
                // The transaction is valid, but duplicate - it has already been sent to Beeblex in the past.
                [FlurryAnalytics logEvent:@"Transaction duplicate!"];
                [self completeValidationWithValidateReceiptData:Nil isRecoverableError:NO];
            } else {
                // The transaction has been successfully validated and is unique
                [FlurryAnalytics logEvent:@"Transaction verified"];
                [self completeValidationWithValidateReceiptData:bbxTransaction.validatedTransactionData isRecoverableError:NO];
            }
        } else {
            // Check whether this is a validation error, or if something went wrong with Beeblex
            if (bbxTransaction.hasConfigurationError || bbxTransaction.hasServerError || bbxTransaction.hasClientError) {
                // The error was not caused by a problem with the data, but is most likely due to some transient networking issues
                [FlurryAnalytics logEvent:@"Transaction network error"];
                [self completeValidationWithValidateReceiptData:Nil isRecoverableError:YES];
            } else {
                // The transaction supplied to the validation service was not valid according to Apple
                [FlurryAnalytics logEvent:@"Transaction invalid!!"];
                [self completeValidationWithValidateReceiptData:Nil isRecoverableError:NO];
            }
        }
    }];
}
于 2012-07-25T18:42:46.347 回答
3

<3 块

- (void)verifyTransaction:(SKPaymentTransaction *)transaction completionHandler:(void (^)(BOOL flag))completionHandler
{
    if (![BBXIAPTransaction canValidateTransactions]) {
        completionHandler(YES); // There is no connectivity to reach the server
    }

    BBXIAPTransaction *bbxTransaction = [[BBXIAPTransaction alloc] initWithTransaction:transaction];
    bbxTransaction.useSandbox = YES;
    [bbxTransaction validateWithCompletionBlock:^(NSError *error) {
        if (bbxTransaction.transactionVerified) {
            if (bbxTransaction.transactionIsDuplicate) {
                // The transaction is valid, but duplicate - it has already been sent to Beeblex in the past.
                NSLog(@"Transaction is a duplicate!");
                [FlurryAnalytics logEvent:@"Transaction duplicate!"];
                completionHandler(NO);
            } else {
                // The transaction has been successfully validated and is unique
                NSLog(@"Transaction valid data:%@",bbxTransaction.validatedTransactionData);
                [FlurryAnalytics logEvent:@"Transaction verified"];
                completionHandler(YES);
            }
        } else {
            // Check whether this is a validation error, or if something went wrong with Beeblex
            if (bbxTransaction.hasConfigurationError || bbxTransaction.hasServerError || bbxTransaction.hasClientError) {
                // The error was not caused by a problem with the data, but is most likely due to some transient networking issues
                NSLog(@"Transaction error caused by network, not data");
                [FlurryAnalytics logEvent:@"Transaction network error"];
                completionHandler(YES);
            } else {
                // The transaction supplied to the validation service was not valid according to Apple
                NSLog(@"Transaction not valid according to Apple");
                [FlurryAnalytics logEvent:@"Transaction invalid!!"];
                completionHandler(NO);
            }
        }
    }];
}

然后使用

[instance verifyTransaction:transaction completionHandler:^(BOOL flag) {
    if (flag) {
        // YOUR CODE HERE
    }
}];

代替

if ([instance verifyTransaction:transaction]) {
    // YOUR CODE HERE
}
于 2012-11-22T14:39:01.603 回答