为什么这些NSNotifications重复出现



我正在做一个应用内购买部分,该部分有一个通知设置,可以将通知从observer类传递到main类。我有这个代码

- (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
                break;
            case SKPaymentTransactionStatePurchased:
                // Item was successfully purchased!
                // --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
                // The purchased item ID is accessible via 
                // transaction.payment.productIdentifier

                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataComplete object:nil] ;
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletehindi object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletetamil object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletetelugu object:nil];
                 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DataCompletekannada object:nil];
                // After customer has successfully received purchased content,
                // remove the finished transaction from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

            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.
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_Datacomplteing object:nil];
                if (transaction.error.code != SKErrorPaymentCancelled) {
}
                // Finished transactions should be removed from the payment queue.
                [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;
        }
    }
}

我将此通知传递给我的主类,但我收到了重复的通知。通知每次都会重复,并且我没有从与通知名称相对应的observer类中获得正确的通知。我该如何解决这个问题。

编辑:我有五个类似的警觉性视图:

if
{
}
elseif
{
}
else if
{
}
else if(alertView == kannadaPurchasedAlert)
{
    if (buttonIndex==0)
    {                        
        NSString *connectionstring =  [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
        if ([connectionstring length]==0) {
            proAlertView *alert = [[proAlertView alloc]initWithTitle:nil message:@"you are not connected to the internet" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alert setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:1.0]];
            [alert show];
            [alert release];
   }
   else
   { 
        [NSThread detachNewThreadSelector: @selector(spinBegininapp) toTarget:self withObject:nil];
        // Replace "Your IAP Product ID" with your actual In-App Purchase Product ID.
        SKPayment *paymentRequest = [SKPayment paymentWithProductIdentifier: @"com.touch.MyApp.MyApp.Kannada"]; 
        // Assign an Observer class to the SKPaymentTransactionObserver,
        // so that it can monitor the transaction status.
        [[SKPaymentQueue defaultQueue] addTransactionObserver:inappObserver];
        // Request a purchase of the selected item.
        [[SKPaymentQueue defaultQueue] addPayment:paymentRequest];
        // Register observer for when download of data is complete
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDataCompletekannada:) name:NOTIF_DataCompletekannada object:nil]; 
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDataCompleting:) name:NOTIF_Datacomplteing object:nil]; 
        amtk = 0.0;                         
   } 
}

然后在.h文件中我放了

extern NSString * const NOTIF_DataComplete;
extern NSString * const NOTIF_Datacomplteing;
extern NSString * const NOTIF_FIRSTLAUNCH;
extern NSString * const NOTIF_DataCompletehindi;
extern NSString * const NOTIF_DataCompletetamil;
extern NSString * const NOTIF_DataCompletetelugu;
extern NSString * const NOTIF_DataCompletekannada;

您的NOTIF_DataCompleteXXX在哪里声明?如果你不给它们分配任何字符串,它们都是相同的"空名称"通知,每个通知都会触发所有观察者。

问题可能在于添加Observer的位置。这些是累积的,这意味着每次调用它时,都会在通知中心注册一个新的回调。如果在一个方法中只调用了一次,请确保该方法只调用一次。例如,viewDidAppear:方法可能在视图控制器的生命周期中被调用多次。最好在viewDidLoad中设置观察者,然后在dealloc方法中释放它们。

最新更新