【问题标题】:How to do in-app purchase in ios5?如何在ios5中进行应用内购买?
【发布时间】:2012-06-22 10:57:45
【问题描述】:

我已使用以下代码进行应用内购买。

- (void)viewDidLoad {
if ([SKPaymentQueue canMakePayments]) {
    NSLog(@"Can Buy Product");

    SKProductsRequest *productRequest=[[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithObject:@"com.mycompany.myproduct.productpack"]];
    productRequest.delegate=self;
    [productRequest start];
}
else {

    NSLog(@"Product Can't be purchased");
}
}

-(IBAction)purchasePack1 {

SKPayment *payment=[SKPayment paymentWithProductIdentifier:@"com.mycompany.myproduct.productpack"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];

}


-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

validProduct=nil;
int count=[response.products count];
if (count>0) {
    NSLog(@"Product Avail");

    validProduct=[response.products objectAtIndex:0];
}
else {
    NSLog(@"No Product avail");
    [purchaseBtn setHidden:TRUE];
}
}


-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

for (SKPaymentTransaction *transaction in transactions) {
    switch (transaction.transactionState) {
        case SKPaymentTransactionStatePurchasing:
            NSLog(@"Purchasing");   
            [activityIndicatorObj setHidden:FALSE];
            [activityIndicatorObj startAnimating];
            break;

        case SKPaymentTransactionStatePurchased:

            UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Congrats" message:@"Thanks For Purchasing " delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alt show];
            [alt release];
            NSLog(@"Purchased");
            [activityIndicatorObj stopAnimating];
            [activityIndicatorObj setHidden:TRUE];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;

            case SKPaymentTransactionStateFailed:

            if (transaction.error.code!=SKErrorPaymentCancelled) {

                NSLog(@"Cancelled");

            }
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

            UIAlertView *alt1=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Some Error Encountered!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
                [alt1 show];
                [alt1 release];
            NSLog(@"Failed");
            [activityIndicatorObj stopAnimating];
            [activityIndicatorObj setHidden:TRUE];
            break;

            case SKPaymentTransactionStateRestored:
            NSLog(@"Restored");
            [activityIndicatorObj stopAnimating];
            [activityIndicatorObj setHidden:TRUE];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;



        default:
            break;
    }
}
}

当我单击购买按钮时,我收到一条警告消息,提示“您已经购买了此产品但尚未下载,是否要下载。?” 当我单击“是”时,我没有得到 App 的任何响应,并且我确定该产品尚未下载。谁能告诉我这个问题的建议。提前致谢。

【问题讨论】:

  • 加载视图时是否打印“产品效用”文本?
  • paymentWithProductIdentifier: 自 iOS 5 起已弃用。您应该改用 paymentWithProduct:。

标签: objective-c in-app-purchase xcode4.3 ios5


【解决方案1】:

我没有看到 - (void)provideContent:(NSString *)productId 的任何实现 - 你错过了那部分吗?在任何情况下,当 transactionState 切换到 SKPaymentTransactionStatePurchased 时,您都应该收到通知。

如果您还没有购买 in-app,您可能想看看 Ray Wenderlich tut,它将帮助您建立一个应用内管理器课程。我认为您已将应用内逻辑放在视图控制器中 - 将该部分留在单独的类中会很好。所以一个应用内经理/助手:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2010-12-17
    • 1970-01-01
    • 2019-12-12
    • 2018-04-15
    相关资源
    最近更新 更多