OStack程序员社区-中国程序员成长平台

标题: ios - 应用内购买后,viewWillAppear会多次加载购买的商品 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:51
标题: ios - 应用内购买后,viewWillAppear会多次加载购买的商品

我在应用内购买方面遇到了麻烦。离开MasterViewController(列出并购买IAP物品的地方)后,我来到ViewController,它在viewDidLoad中检查购买的物品,但是我也有viewWillAppear检查购买的物品并在加载ViewController时立即解锁那些购买的物品。

问题是,每次重新加载或刷新ViewController时,都将其离开,然后再返回,则viewWillAppear会检查购买的物品并再次解锁物品,在我的情况下,购买的物品来自javascript文件,而这恰好会加载JS多次,使具有相同IAP项的应用一遍又一遍。

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

如何获得IAP仅加载一次的信息?

我的viewWillAppear看起来像这样:

- (void)viewWillAppearBOOL)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"ortalLevel" 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"ortalNames" 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]);
}



Best Answer-推荐答案


我认为您应该做的是使每个人最多被调用一次。您应该使用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"ortalLevel" withExtension"js"] encoding:NSUTF8StringEncoding error:nil];
    [viewWeb stringByEvaluatingJavaScriptFromString:jsString];    
}

还记得在VC.m中包含define语句

关于ios - 应用内购买后,viewWillAppear会多次加载购买的商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998793/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4