购买一次IAP耗材会触发两次

时间:2013-10-04 00:52:26

标签: iphone ios ipad cocos2d-iphone in-app-purchase

“尚未解决......”

我的iap耗材包有问题。问题是,当我买一个,它重复购买,另一个没有请求它。警报视图也显示为双倍。

奇怪的是:这个错误只发生在我制作应用程序的ipa时,而不是在调试时!我不明白为什么......

以下是我在主菜单中实现的productPurchase方法,其中我放置了IAP按钮:

- (void)productPurchased:(NSNotification *)notification {

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

    NSString * productIdentifier = notification.object;

    NSString* plistPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"XXXXX.plist"];

    if(plistPath != nil){
        int playerSilverCoins = [defaults integerForKey:@"userSilverCoins"];
        int playerGoldCoins = [defaults integerForKey:@"userGoldenCoins"];

        NSArray *coinsItems = [NSArray arrayWithContentsOfFile:plistPath];
        for (NSDictionary *coinItem in coinsItems) {
            if ([[coinItem objectForKey:@"productId"] isEqualToString:productIdentifier]) {
                playerSilverCoins = playerSilverCoins + [[coinItem objectForKey:@"silverCoins"] integerValue];
                playerGoldCoins = playerGoldCoins + [[coinItem objectForKey:@"goldenCoins"] integerValue];

                [defaults setInteger:playerSilverCoins forKey:@"userSilverCoins"];
                [defaults setInteger:playerGoldCoins forKey:@"userGoldenCoins"];
                [defaults synchronize];

                NSLog(@"product bought: %@" , productIdentifier);

                NSString *message = nil;
                if([[coinItem objectForKey:@"goldenCoins"] integerValue] == 0)
                    message = [NSString stringWithFormat:@"You have bought %d silver coins", [[coinItem objectForKey:@"silverCoins"] integerValue]];
                else
                message = [NSString stringWithFormat:@"You have bought %d golden coins and %d silver coins", [[coinItem objectForKey:@"goldenCoins"] integerValue],[[coinItem objectForKey:@"silverCoins"] integerValue]];

                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"XXXXX" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];

                NSNumber* achievementContributor = [defaults objectForKey:@"AchievementContributor"];
                if ([achievementContributor intValue] != 1 ) {

                    [[GameCenterAchievement sharedGameCenterAchievement] checkAchievements:_kContributor];
                    [defaults setObject:[NSNumber numberWithInt:1] forKey:@"AchievementContributor"];
                    [[GKAchievementHandler defaultHandler] notifyAchievementTitle:@"Contributor"
                                                                       andMessage:@"Buy a coin pack"];
                }


                break;
            }
        }
    }
}

我不知道那里是否存在问题,或者我的IAP课程中我实现了交易,恢复等。如果有人需要更多代码,只需要求它。谢谢!

1 个答案:

答案 0 :(得分:0)

确保您只添加观察者。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];

添加侦听器乘法次数将导致多次调用回调方法。 如果你正在重复使用你的类 - 记得在你解除它之后删除Observer - 它不会自己做它。