我一直在尝试让一个简单的消耗品 iOS 应用内购买工作。不幸的是,response.products 一直返回为空 0。我拥有开发人员会员资格,我创建了一个带有产品标识符“Getit”的 itunesconnect 应用内购买,并在 testflight 上测试了该应用程序。我有一个有效的付费应用程序协议,该协议已被 Apple 批准。该应用程序本身尚未提交审核,但我想 Apple 允许我们在首次提交我们的应用程序或协议之前测试该应用程序。
控制器.h
#import <StoreKit/StoreKit.h>
@interface ViewController : UIViewController <SKProductsRequestDelegate,SKPaymentTransactionObserver> {
SKProductsRequest *productsRequest;
}
@property (strong, nonatomic) SKProduct *product;
控制器.m
-(void)fetchAvailableProducts {
[SKPaymentQueue defaultQueue];
if ([self canMakePurchases]) {
SKProductsRequest *request = [[SKProductsRequest alloc]
initWithProductIdentifiers:
[NSSet setWithObject:@"Getit"]];
request.delegate = self;
[request start];
} else {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Unable to make purchases"
message:@"Purchases have been disabled on your device. We cannot proceed with the purchase."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}
}
- (BOOL)canMakePurchases {
return [SKPaymentQueue canMakePayments];
}
#pragma mark -
#pragma mark SKProductsRequestDelegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;
NSLog(@"%@", response.products);
if (products.count != 0)
{
_product = products[0];
SKPayment *payment = [SKPayment paymentWithProduct:_product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
} else {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Unable to make purchases"
message:@"We are unable to find any available items for purchase"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}
products = response.invalidProductIdentifiers;
for (SKProduct *product in products)
{
NSLog(@"Product not found: %@", product);
}
}
#pragma mark -
#pragma mark SKPaymentTransactionObserver
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self progrets];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@"Transaction Failed");
[spinner stopAnimating];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
default:
break;
}
}
}
-(void)progrets {
self.spinner.hidden = NO;
[spinner startAnimating];
[self performSelector:@selector(unlockFeature)
withObject:nil
afterDelay:0.1];
}
-(void)unlockFeature
{
NSLog(@"done");
}