应用内购买后,viewWillAppear 会多次加载已购买的项目

After In-App Purchase, viewWillAppear Loads The Purchased Item Multiple Times

我的应用内购买有问题。离开 MasterViewController(列出和购买 IAP 物品的地方)后,我来到 ViewController,它在 viewDidLoad 中检查购买的物品,但我也有一个 viewWillAppear 检查购买的物品并解锁加载 ViewController 时购买的物品。

问题是,每次 ViewController 重新加载或刷新、离开然后返回时,viewWillAppear 都会检查购买的物品并再次解锁物品,在我的例子中,购买的物品来自javascript 个文件,这恰好加载了 JS 多次,一遍又一遍地用相同的 IAP 项目使应用程序混乱。

例如,如果您购买了 'Bookmarks',则每次重新加载 ViewController 时,您都会一遍又一遍地获得另一个 'Bookmarks' 工具。

如何才能让 IAP 只加载一次?

我的 viewWillAppear 看起来像这样:

- (void)viewWillAppear:(BOOL)animated {
// Check To See If The Uniques IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_UNIQUES_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Uniques" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows uniques?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_UNIQUES_KEY]);

// Check To See If The PL Numbers IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"PortalLevel" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows portal levels?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY]);

// Check To See If The PN IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTALS_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"PortalNames" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows portal names?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTALS_KEY]);

// Check To See If The Bookmarks IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_BOOKMARKS_KEY] isEqualToString: @"YES"]){ // NEW CODE
    // Code to show bookmarks

} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"bookmarks" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    }
NSLog(@"Shows bookmarks?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_BOOKMARKS_KEY]);

// Check To See If The Poke A Bowl Ad Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_ADS_KEY] isEqualToString: @"No"]){ // NEW CODE
    // Code to show ads
    _PokeABowlAd.hidden = NO;

} else {
    _PokeABowlAd.hidden = YES;
}
NSLog(@"Shows ads?: %@", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_ADS_KEY]);
}

我认为你应该做的是让每个人最多被调用一次。您应该使用 NSUserDefaults 来完成此操作。做这样的事情:

将此放入您的 AppDelegate.m:

//One new key for each IAP - put at the top of the AppDelegate.m and VC.m
#define IAP_FIRST_USED @"IAP 1 Used"
...
#define IAP_LAST_USED @"IAP X Used"

// In applicationDidFinishLaunchingWithOptions, should be the first method listed

[[NSUserDefaults standardUserDefaults] setObject: @"No" forKey: IAP_FIRST_USED];
...
[[NSUserDefaults standardUserDefaults] setObject: @"No" forKey: IAP_LAST_USED];
[[NSUserDefaults standardUserDefaults] synchronize];

然后在 VC.m 中将 viewWillAppear 修改为如下所示(这是第一个 IAP 的示例):

// Check To See If The PL Numbers IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: @"YES"]){ // NEW CODE


   // NEW CODE BEGINS HERE

   if ([[[NSUserDefaults standardUserDefaults] objectForKey:IAP_FIRST_USED] isEqualToString: @"No"]){
   [[NSUserDefaults standardUserDefaults] setObject: @"Yes" forKey: IAP_FIRST_USED];
   [[NSUserDefaults standardUserDefaults] synchronize];  
   // Code to show bookmarks



   // End of code to show bookmarks
   // NEW CODE ENDS HERE

   }
} else {
    NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"PortalLevel" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    
}

还要记得在 VC.m

中包含 define 语句