gpt4 book ai didi

ios - 使用 IBAction/按钮进行应用内购买

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:53 26 4
gpt4 key购买 nike

我正在使用 Ray Wenderlich 教程创建 IAP (http://www.raywenderlich.com/23266/),一切正常,但我不想在我的应用程序上使用表格 View ,我想使用只需一个简单的 IBAction 按钮即可进行购买。

所以基本上这就是它在 TableView 上的工作方式。首先识别产品:

 + (RageIAPHelper *)sharedInstance {
static dispatch_once_t once;
static RageIAPHelper * sharedInstance;
dispatch_once(&once, ^{
NSSet * productIdentifiers = [NSSet setWithObjects:
@"com.companyname.10coins",
@"com.companyname.20coins",
nil];
sharedInstance = [[self alloc] initWithProductIdentifiers:productIdentifiers];
});
return sharedInstance;
}

然后触发 Action :

 - (void)buyButtonTapped:(id)sender {

UIButton *buyButton = (UIButton *)sender;
SKProduct *product = _products[buyButton.tag];

NSLog(@"Buying %@...", product.productIdentifier);
[[RageIAPHelper sharedInstance] buyProduct:product]; }


- (void)buyProduct:(SKProduct *)product {

NSLog(@"Buying %@...", product.productIdentifier);

SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

所以我试图制作一个简单的按钮来触发操作,如下所示:

 - (IBAction)button10Coins:(id)sender {

SKPayment * payment = [SKPayment paymentWithProduct:@"com.companyname.10coins"];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

但我收到警告“不兼容的指针类型”。

代码启动后效果很好,我可以完成购买,唯一的问题是正确创建 IBAction。有什么想法吗?

谢谢!!!

最佳答案

如果您完成了所有连接工作并且在代码下方添加框架肯定会起作用:

define kStoredData @"inapp purchase对象"(这个用于对象声明)

- (void) requestProductData
{
if(countphotoval==2)
{
phonetext.text=@"";
countrycode.text=@"";
nametext.text=@"";
Emailtext.text=@"";
photocounter=0;
image1.image=[UIImage imageNamed:@"image-box.png"];
image2.image=[UIImage imageNamed:@"image-box.png"];
labelimage.text=@"Image";
addbuttonforpicker.userInteractionEnabled=true;
addbuttonforpicker2.userInteractionEnabled=false;
countphotoval=0;
}
request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"object of inapp purchase"]];
request.delegate = self;

[request start];


}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *myProduct = response.products;


// populate UI

}


-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
NSLog(@"transaction array-->%@",transaction.description);
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:

// show wait view here
//statusLabel.text = @"Processing...";
break;

case SKPaymentTransactionStatePurchased:

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[NSThread detachNewThreadSelector:@selector(startActivityindicatore) toTarget:self withObject:nil];
[self fordataupload];

break;

case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;

case SKPaymentTransactionStateFailed:

if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Error payment cancelled");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Please provide correct Userid and Password" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
// [self dismissModalViewControllerAnimated:YES];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
// statusLabel.text = @"Purchase Error!";
break;

default:
break;
}
}
}


- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Something has went wrong" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
//If you want to save the transaction
// [self recordTransaction: transaction];

//Provide the new content
// [self provideContent: transaction.originalTransaction.payment.productIdentifier];

//Finish the transaction
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrats!!" message:@"Your Transaction Is Completed" delegate:self cancelButtonTitle:@"Thanx!" otherButtonTitles:nil];
[alert show];
[alert release];

//If you want to save the transaction
// [self recordTransaction: transaction];

//Provide the new content
//[self provideContent: transaction.payment.productIdentifier];

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}
-(void)requestDidFinish:(SKRequest *)request1
{
[self stopActivityindicatore];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"VirtualBinocularsContest1"];
NSLog(@"quality --->%d",payment.quantity);
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
[request release];
}

如果它对你有用,请通知..:)

关于ios - 使用 IBAction/按钮进行应用内购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com