应用内购买

时间:2012-08-14 14:53:37

标签: ios in-app-purchase storekit

我正在努力让整个应用内购买工作,但我遇到了一些问题。所以我能够用我的测试用户帐户“购买”这些功能,但我想一次又一次地测试。所以我做了一个删除方法,摆脱生成的钥匙串并退出应用程序,然后再次构建它,它又回到“免费”状态,我再次尝试购买应用程序,但这次它给出了我“你已经购买了这个项目,点击确定再次下载”所以我点击确定,但这次没有任何反应,没有功能解锁,wtf?

代码:

 -(void)deleteKeyChain:(id)sender {

NSError *error = nil;
NSLog(@"delete!!!!");
[SFHFKeychainUtils deleteItemForUsername:@"someUser" andServiceName:kStoredData            error:&error];

 }

-(void)doFeature:(id)sender {
[newPlayer pause];
if ([self IAPItemPurchased]) {

    // do the feature 2!
   //       featureLabel.text = @"Feature: 2";

} else {
    // not purchased so show a view to prompt for purchase
    askToPurchase = [[UIAlertView alloc]
                     initWithTitle:@"All Features"
                     message:@"Tap refresh anytime to read latest 5 emails. To read all      emails with no ads and to continue reading in the background, please purchase the full version of this app."
                     delegate:self
                     cancelButtonTitle:nil
                     otherButtonTitles:@"OK",@"Later on", nil];
    askToPurchase.delegate = self;
    [askToPurchase show];
    [askToPurchase release];
     }
   }


   #pragma mark StoreKit Delegate

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

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

        case SKPaymentTransactionStatePurchased:

            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            // remove wait view and unlock feature 2
    //        statusLabel.text = @"Done!";
            UIAlertView *tmp = [[UIAlertView alloc]
                                initWithTitle:@"Complete"
                                message:@"You now have the full version of Emails Aloud!!"
                                delegate:self
                                cancelButtonTitle:nil
                                otherButtonTitles:@"Ok", nil];
            [tmp show];
            [tmp release];


            NSError *error = nil;
            [SFHFKeychainUtils storeUsername:@"someUser" andPassword:@"pass"   forServiceName:kStoredData updateExisting:YES error:&error];

            // apply purchase action  - hide lock overlay and
         //   [feature2Btn setBackgroundImage:nil forState:UIControlStateNormal];

            // do other thing to enable the features

            break;

        case SKPaymentTransactionStateRestored:
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            // remove wait view here
            //    statusLabel.text = @"";
            break;

        case SKPaymentTransactionStateFailed:

            if (transaction.error.code != SKErrorPaymentCancelled) {
                NSLog(@"Error payment cancelled=%d",transaction.error.code);
            }
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            // remove wait view here
         //   statusLabel.text = @"Purchase Error!";
            break;

        default:
            break;
        }
      }
    }






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

   // remove wait view here
   // statusLabel.text = @"";

SKProduct *validProduct = nil;
int count = [response.products count];

if (count>0) {
    validProduct = [response.products objectAtIndex:0];
   [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    SKPayment *payment = [SKPayment    paymentWithProductIdentifier:@"com.myapp.shit"];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];


} else {
    UIAlertView *tmp = [[UIAlertView alloc]
                        initWithTitle:@"Not Available"
                        message:@"No products to purchase"
                        delegate:self
                        cancelButtonTitle:nil
                        otherButtonTitles:@"Ok", nil];
    [tmp show];
    [tmp release];
}


 }

  -(void)requestDidFinish:(SKRequest *)request
 {
[request release];
 }

-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
 NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
}
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (alertView==askToPurchase) {
    if (buttonIndex==0) {
        // user tapped YES, but we need to check if IAP is enabled or not.
        if ([SKPaymentQueue canMakePayments]) {

            SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.myapp.shit"]];

            request.delegate = self;
            [request start];


        } else {
            UIAlertView *tmp = [[UIAlertView alloc]
                                initWithTitle:@"Prohibited"
                                message:@"Parental Control is enabled, cannot make a  purchase!"
                                delegate:self
                                cancelButtonTitle:nil
                                otherButtonTitles:@"Ok", nil];
            [tmp show];
            [tmp release];
           }
       }
    }

}

注意:someUser / pass不是任何人的用户名/密码。这只是一个文本,我选择将应用程序购买中的用户注册到他们的设备中。

1 个答案:

答案 0 :(得分:1)

如果您已使用iTunes Connect用户的用户/密码购买了应用程序,则需要查找SKPaymentTransactionStateRestored而不是SKPaymentTransactionStatePurchased

SKPaymentTransactionStatePurchased每个用户只发生一次非易耗品。尝试在iTunes Connect上创建更多测试用户。