我已在沙盒中测试过应用内购买。它工作得很完美。现在我的应用程序是实时的,当我点击购买按钮时,它直接进入应用程序购买。它不是要求用户名/密码或现有用户。此外,我甚至都没有从设备登录。
-(void)requestForProductWithCompletionHandler
{
if ([SKPaymentQueue canMakePayments])
{
SKProductsRequest *_productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kProductIdentifier]];
_productsRequest.delegate = self;
[_productsRequest start];
}
}
-(void)purchaseProduct:(SKProduct *)product
{
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
if (!hasAddObserver) {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
hasAddObserver = YES;
}
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
#pragma mark - PRODUCT REQUEST DELEGATE
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
if (response.products.count > 0) {
SKProduct *_product = [response.products objectAtIndex:0];
NSLog(@"GOT THE PRODUCT");
// [[[UIAlertView alloc] initWithTitle:APPNAME message:@"Product Loaded" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
[self purchaseProduct:_product];
}
else
{
[btnReport setEnabled:YES];
[[[UIAlertView alloc] initWithTitle:APPNAME message:@"Invalid product" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
NSLog(@"%@",error);
[btnReport setEnabled:YES];
[[[UIAlertView alloc] initWithTitle:APPNAME message:@"Unable to load product" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}
#pragma mark - TRANSACTION OBSERVER
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
[self showTransactionAsInProgress:transaction deferred:NO];
break;
case SKPaymentTransactionStateDeferred:
[self showTransactionAsInProgress:transaction deferred:YES];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
// For debugging
// NSLog(@"Unexpected transaction state %@", @(transaction.transactionState));
break;
}
}
}
答案 0 :(得分:0)
您是否尝试将事务观察器添加到AppDelegate而不是视图控制器中? Apple的最佳实践描述了如何/为何这样做。
https://developer.apple.com/library/ios/technotes/tn2387/_index.html