从SKProductsRequest获取SKProduct

时间:2014-11-25 20:43:55

标签: objective-c in-app-purchase sprite-kit skproduct

您好我正在尝试将应用内购买添加到我的应用中,但每次我调用方法“买”我都会收到错误“无效的产品标识符:( null)”。 如何在不收到这些错误的情况下调用该方法/如何使购买方法中的产品与响应中的产品相同?

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *productIdentifiers = @[@"product1"];   
}

- (void)fetchProducts {
    NSSet *identifierSet = [NSSet setWithArray:productIdentifier];
    SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers: identifierSet];
    request.delegate = self;
    [request start];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSLog(@"Inside productsRequest");
    for(SKProduct *product in response.products) {
        NSLog(@"%@", product);
    }
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Error %@", error);
}

-(void)unlock {
    SKProduct *product = [[SKProduct alloc] init];
    [self buy:product];
}


- (void)buy:(SKProduct *)product {
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}


- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for(SKPaymentTransaction *tx in transactions) {
        switch(tx.transactionState) {

            case SKPaymentTransactionStatePurchased:
                NSLog(@"Unlock %@", tx.payment.productIdentifier);
                [[SKPaymentQueue defaultQueue] finishTransaction:tx];
                break;

            case SKPaymentTransactionStateFailed:
                NSLog(@"Error: %@", tx.error);
                [[SKPaymentQueue defaultQueue] finishTransaction:tx];
                break;

            case SKPaymentTransactionStateRestored:
                NSLog(@"Unlock %@", tx.originalTransaction.payment.productIdentifier);
                [[SKPaymentQueue defaultQueue] finishTransaction:tx];
                break;
        }
    }
}

0 个答案:

没有答案