【问题标题】:Inapp Purchase is succeeded but content is not unlock in iPhone app.. Why?应用内购买成功,但iPhone应用中的内容没有解锁。为什么?
【发布时间】:2011-10-02 04:58:17
【问题描述】:

在我的 iphone 应用程序中,我使用了应用程序内购买。所有的东西都在工作,内容在 Resource 文件夹中。交易成功后,我使用 Sql Query 在 Sqlite 数据库中插入数据。我已经在 App Store 中上传了这个应用程序,发现成功交易后,付款是从用户那里收取的,但内容没有插入数据库,也没有显示在应用程序中。请帮我。我对这一点感到压力。因此,我已从 App Store 中删除了我的应用程序。

在此代码中成功找到应用内购买列表后我正在使用方法

为了更好地理解,我把我的一些代码放在这里:

- (void)insertNewObject {

    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        // NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}


-(void)updateObject {
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {

        abort();
    }
}


#pragma mark -
#pragma mark Store request

- (void) requestProductData {
    request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:[arrayProductID objectAtIndex:b-2]]];//@"Scaringmirror11"
    request.delegate = self;
    [request start];
}

- (void) startPurchase {
    //int newB=b-2;
    SKPayment *payment = [SKPayment paymentWithProductIdentifier:[arrayProductID objectAtIndex:b-2]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}



#pragma mark Store delegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSArray *myProduct = response.products;
    if (myProduct == nil || [myProduct count] == 0) 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"Missing product from App store.\n"
                                                       delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    SKProduct *product;
    BOOL    existProduct = NO;
    for (int i=0; i<[myProduct count]; i++) {
        product = (SKProduct*)[myProduct objectAtIndex:0];
        if ([product.productIdentifier isEqualToString:[arrayProductID objectAtIndex:b-2]]) {
            existProduct = YES;

            //[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];
            //[[NSUserDefaults standardUserDefaults] synchronize];
            break;
        }
    }
    if (existProduct == NO) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"Missing product from App store.No match Identifier found.\n"
                                                       delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    [request autorelease];
    [self startPurchase];
}



#pragma mark Store delegate

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    NSArray *myProduct = response.products;
    if (myProduct == nil || [myProduct count] == 0) 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"Missing product from App store.\n"
                                                       delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    SKProduct *product;
    BOOL    existProduct = NO;
    for (int i=0; i<[myProduct count]; i++) {
        product = (SKProduct*)[myProduct objectAtIndex:0];
        if ([product.productIdentifier isEqualToString:[arrayProductID objectAtIndex:b-2]]) {
            existProduct = YES;

            //[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];
            //[[NSUserDefaults standardUserDefaults] synchronize];
            break;
        }
    }
    if (existProduct == NO) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"Missing product from App store.No match Identifier found.\n"
                                                       delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    [request autorelease];
    [self startPurchase];
}


#pragma mark observer delegate
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:

                [self completeTransaction:transaction];
                NSLog(@"Success");
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                NSLog(@"Failed");
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction {
    //[[NSUserDefaults standardUserDefaults] setBool:YES forKey:REGISTRATION_KEY];
    registered = YES;

    //NSData *receiptData = [transaction transactionReceipt];

    //NSString *str =[NSString string 
    //[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:@"proUpgradeTransactionReceipt" ];
    //[self registeredEnable];
    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    [sqlite insertIntoScaryImage:[arrayGetMoreScaryImage objectAtIndex:b] add:[arrayGetMoreScarySound objectAtIndex:b]];
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction {
    //[[NSUserDefaults standardUserDefaults] setBool:YES forKey:REGISTRATION_KEY];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:@"Purchase success."
                                                   delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

    //[sq];
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction {
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // Optionally, display an error here.
        NSString *stringError = [NSString stringWithFormat:@"Payment Cancelled\n\n%@", [transaction.error localizedDescription]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:stringError
                                                       delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];

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

请看一下并告诉我应该在数据库中的何处插入数据,以便在应用内购买成功后向用户提供数据。

提前致谢

【问题讨论】:

    标签: iphone objective-c app-store


    【解决方案1】:

    您应该在收到付款正常的确认后立即在付款观察器中实施您的解锁功能。 你对观察者的实现有点奇怪,我猜它不起作用。

    编辑:

    你的观察者类

    #import 基金会/Foundation.h #import StoreKit/StoreKit.h @interface InAppPurchaseObserver : NSObject { } - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions; @结尾 #import "InAppPurchaseObserver.h" @implementation InAppPurchaseObserver // 这里发送SKPaymentQueue的交易状态。 - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { for(SKPaymentTransaction *交易中的交易) { 开关(transaction.transactionState){ 案例 SKPaymentTransactionStatePurchasing: // 物品仍在购买过程中 NSLog(@"处理中!!!"); 休息; 案例 SKPaymentTransactionStatePurchased: // 商品购买成功! // --- 解锁功能或在此处下载内容 --- // 购买的物品 ID 可以通过 // transaction.payment.productIdentifier // 客户成功收到购买的内容后, // 从支付队列中移除完成的交易。 [[SKPaymentQueue defaultQueue] finishTransaction: 交易]; 休息; 案例 SKPaymentTransactionStateRestored: // 验证用户已经为此商品付款。 // 非常适合在该客户的所有设备上恢复项目。 // --- 解锁功能或在此处下载内容 --- // 购买的物品 ID 可以通过 // transaction.payment.productIdentifier // 客户在此设备上恢复购买的内容后, // 从支付队列中移除完成的交易。 [[SKPaymentQueue defaultQueue] finishTransaction: 交易]; 休息; 案例 SKPaymentTransactionStateFailed: // 购买被用户取消或发生错误。 NSLog(@"失败!!!"); if (transaction.error.code != SKErrorPaymentCancelled) { // 发生交易错误,所以通知用户。 NSLog(@"取消!!!"); } // 完成的交易应该从支付队列中移除。 [[SKPaymentQueue defaultQueue] finishTransaction: 交易]; 休息; } } } @结尾

    实现

    -(无效)requestProductData { inappObserver = [[InAppPurchaseObserver alloc] init]; request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:[arrayProductID objectAtIndex:b-2]]];//@"Scaringmirror11" request.delegate = self; [请求开始]; } -(无效)开始购买{ //int newB=b-2; SKPayment *payment = [SKPayment paymentWithProductIdentifier:[arrayProductID objectAtIndex:b-2]]; [[SKPaymentQueue defaultQueue] addTransactionObserver:inappObserver]; [[SKPaymentQueue defaultQueue] addPayment:payment]; }

    【讨论】:

    • 是的...它不工作。你的意思是在检查案例 SKPaymentTransactionStatePurchased 时触发 Query In Observer Delegate 方法吗?
    • 你能做的最好的就是创建一个单独的类,你只存储观察者委托。然后分配观察者并添加到应用内支付,非常简单。我将在 sec 中发布代码。
    • 嗨...我知道购买的商品 ID,因为这是在应用程序内...所以我可能需要再次从流程中获取它吗?
    • 不知道为什么。在大多数情况下,我在上面发布的解决方案都可以正常工作。观察者本身可以很好地处理所有事务。
    • 不,我的意思是我在 sqlite 中输入所有数据,并且我知道在某些购买时将输入数据库的数据 ID。因为每次购买我都需要将这些数据存储在 sqlite..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多