[英]In-app purchase, show products but nothing happens
我試圖在一個應用程序中實現IAP,但我仍然遇到困難。 我遵循了各種教程,但所有教程都已過時且充滿了錯誤。 我找到的唯一可行的是這一個 :
但是我遇到了問題,3個產品出現在我的桌面視圖上,但是當我點擊其中一個產品時沒有任何反應......細胞變成藍色,這就是全部...我錯過了什么?
或者那個教程是不完整的?
如何進行購買嘗試?
這是我的代碼:
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
[productDetailsList addObjectsFromArray: response.products];
[productDisplayTableView reloadData];
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.productDetailsList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *GenericTableIdentifier = @"GenericTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: GenericTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: GenericTableIdentifier];
}
NSUInteger row = [indexPath row];
SKProduct *thisProduct = [productDetailsList objectAtIndex:row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ - %@", thisProduct.localizedTitle, thisProduct.price]];
return cell;
}
- (void)viewDidLoad
{
productDetailsList = [[NSMutableArray alloc] init];
productIdentifierList = [[NSMutableArray alloc] init];
for (short item_count=1; item_count <= 5; item_count++) {
[productIdentifierList addObject:[NSString stringWithFormat:@"com.mycompany.myapp.%d", item_count]];
}
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIdentifierList]];
request.delegate = self;
[request start];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
你需要有以下幾點:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([SKPaymentQueue canMakePayments])
{
SKProduct *selectedProduct = [self.productDetailsList objectAtIndex:indexPath.row];
SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
Apple為應用程序購買提供了一個體面的分步指南 。
運行IAP的主要方法涉及幾種不同的方法,但在實施IAP時需要遵循幾個不同的步驟。
第一個要求是協議。 請在頭文件中包含以下每個協議。
您需要請求方法:
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
if(response.products.count > 0)
{
SKProduct* product;
for(int i = 0; i<response.products.count; i++)
{
product = [response.products objectAtIndex:i];
if([product.productIdentifier isEqualToString:@"product identifier"])
{
self.currentProduct = product;
[self beginPaymentWithProduct:product];
}
}
}
}
我使用if語句來跟蹤正在購買的產品。 如果您有多個產品,則需要在每個產品標識符的for循環中使用if語句。 稍后使用此功能可解鎖購買完成后的任何內容。
您還需要beginPayment方法:
- (void)beginPaymentWithProduct:(SKProduct*)product
{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
您還需要付款處理方法。 我不會在這里發布所有內容,因為這會占用太多空間,但我會給你原型。
-(void)requestDidFinish:(SKRequest *)request;
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error;
- (void)recordTransaction:(SKPaymentTransaction *)transaction;
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful;
- (void)completeTransaction:(SKPaymentTransaction *)transaction;
- (void)restoreTransaction:(SKPaymentTransaction *)transaction;
- (void)failedTransaction:(SKPaymentTransaction *)transaction;
對於表中應該購買的每個按鈕,他們需要在didSelectRowAtIndex...
方法上執行與此類似的方法:
- (void)buyCoins:(id)sender
{
if([self canMakePurchases])
{
ualRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:[NSArray arrayWithObjects: @"product identifier", nil]]];
[ualRequest setDelegate:self];
[ualRequest start];
}
}
此方法將成功運行產品請求。 如果你有所有這些組件,你應該沒有問題。
我在幾個應用程序中成功使用了此代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.