Inapp购买成功但iPhone应用程序内容未解锁..为什么?

时间:2011-10-02 04:58:17

标签: iphone objective-c app-store

在我的iphone应用程序中,我使用过In App购买。所有的东西都在工作,内容在资源文件夹中。成功事务后,我使用Sql Query在Sqlite数据库中插入数据。我已经在App Store中上传了这个应用程序,并发现在成功交易后,付款是从用户处获得的,但内容未插入数据库而未显示在应用程序中。请帮我。我在这一点上强调。由于这个原因,我删除了App Store的应用程序。

在成功查找In App Purchase列表后,在此代码中我使用方法

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

- (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];
}

请看一下,告诉我应该在数据库中插入数据,这样我就可以在应用程序购买成功后向用户提供数据。

先谢谢

1 个答案:

答案 0 :(得分:0)

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

编辑:

您的观察员班级

#import Foundation/Foundation.h
#import StoreKit/StoreKit.h


@interface InAppPurchaseObserver : NSObject  {

}

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

@end

#import "InAppPurchaseObserver.h"


@implementation InAppPurchaseObserver

// The transaction status of the SKPaymentQueue is sent here.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for(SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {

            case SKPaymentTransactionStatePurchasing:
                // Item is still in the process of being purchased
                NSLog(@"Processing!!!");
                break;

            case SKPaymentTransactionStatePurchased:
                // Item was successfully purchased!

                // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
                // The purchased item ID is accessible via 
                // transaction.payment.productIdentifier



                // After customer has successfully received purchased content,
                // remove the finished transaction from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;

            case SKPaymentTransactionStateRestored:
                // Verified that user has already paid for this item.
                // Ideal for restoring item across all devices of this customer.

                // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
                // The purchased item ID is accessible via 
                // transaction.payment.productIdentifier


                // After customer has restored purchased content on this device,
                // remove the finished transaction from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;

            case SKPaymentTransactionStateFailed:
                // Purchase was either cancelled by user or an error occurred.
                NSLog(@"Failed!!!");


                if (transaction.error.code != SKErrorPaymentCancelled) {
                    // A transaction error occurred, so notify user.
                    NSLog(@"Cancelled!!!");
                }
                // Finished transactions should be removed from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;
        }
    }
}

@end

实施


- (void) requestProductData {

    inappObserver = [[InAppPurchaseObserver alloc] init];

    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] addTransactionObserver:inappObserver];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}